OpenCV FindContours使用

FindContours实现了论文Topological structural analysis of digitized binary images by border following

使用例子

1.画轮廓

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;

RNG rng(12345);
/**
 * @function main
 */
int main( int argc, char** argv )
{
    int len=200;
    Mat binImage = Mat::zeros(cvSize(len,len), CV_8UC1);
    Rect rect1;
    rect1.x = len/4;
    rect1.y = len/4;
    rect1.width = len/2;
    rect1.height = len/2;
    binImage(rect1).setTo(255);

    Rect rect2;
    rect1.x = len/4+len/8;
    rect1.y = len/4+len/8;
    rect1.width = len/4;
    rect1.height = len/4;
    binImage(rect1).setTo(0);

    /// Find contours
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    findContours( binImage, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE );

    /// Draw contours
    Mat drawing = Mat::zeros( binImage.size(), CV_8UC3 );
    for( size_t i = 0; i< contours.size(); i++ )
    {
        Scalar color = Scalar( rng.uniform(0, 256), rng.uniform(0,256), rng.uniform(0,256) );
        drawContours( drawing, contours, (int)i, color, 2, LINE_8, hierarchy, 0 );
    }
    imshow("binImage", binImage);
    /// Show in a window
    imshow( "Contours", drawing );
    waitKey();
    return 0;
}


2.由轮廓填充

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;

RNG rng(12345);
/**
 * @function main
 */
int main( int argc, char** argv )
{
    int len=800;
    Mat binImage = Mat::zeros(cvSize(len,len), CV_8UC1);
    Rect rect1;
    rect1.x = len/4;
    rect1.y = len/4;
    rect1.width = len/2;
    rect1.height = len/2;
    binImage(rect1).setTo(255);

    Rect rect2;
    rect2.x = len/4+len/8;
    rect2.y = len/4+len/8;
    rect2.width = len/4;
    rect2.height = len/4;
    binImage(rect2).setTo(0);
    Rect rect3;
    rect3.x = len/4+len/8+len/16;
    rect3.y = len/4+len/8+len/16;
    rect3.width = len/8;
    rect3.height = len/8;
    binImage(rect3).setTo(255);

//    Rect rect2;
//    rect2.x = len/4+len/16;
//    rect2.y = len/4+len/16;
//    rect2.width = len/32;
//    rect2.height = len/32;
//    binImage(rect2).setTo(0);

//    Rect rect3;
//    rect3.x = len/2;
//    rect3.y = len/2;
//    rect3.width = len/32;
//    rect3.height = len/32;
//    binImage(rect3).setTo(0);
    /// Find contours
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    findContours( binImage, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE );

    //cout<<""
    /// Draw contours
    Mat drawing = Mat::zeros( binImage.size(), CV_8UC1 );
//    for( size_t i = 0; i< contours.size(); i++ )
//    {
//        Scalar color = Scalar( rng.uniform(0, 256), rng.uniform(0,256), rng.uniform(0,256) );
//        drawContours( drawing, contours, (int)i, color, 2, LINE_8, hierarchy, 0 );
//    }
     cv::drawContours(drawing, contours, -1, cv::Scalar::all(255),CV_FILLED);
    imshow("binImage", binImage);
    /// Show in a window
    imshow( "Contours", drawing );
    waitKey();
    return 0;
}


沧海飞帆 CSDN认证博客专家 火星工程师
热爱多传感器融合slam、机器人、人工智能相关技术。立志于让机器人更智能,为人类移民火星做铺垫。让科技使生活更幸福,让科技改变世界。
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页