我是靠谱客的博主 闪闪仙人掌,最近开发中收集的这篇文章主要介绍OpenCV FindContours使用1.画轮廓2.由轮廓填充,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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;
}


最后

以上就是闪闪仙人掌为你收集整理的OpenCV FindContours使用1.画轮廓2.由轮廓填充的全部内容,希望文章能够帮你解决OpenCV FindContours使用1.画轮廓2.由轮廓填充所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部