opencv寻找轮廓2--drawContours

继续继续,opencv自带绘制轮廓的函数,属于highgui模块,不知道在arm平台能不能用,以后尝试吧。

void drawContours(InputOutputArray image, InputArrayOfArrays contours, int contourIdx, const Scalar& color, int thickness=1, int lineType=8, InputArray hierarchy=noArray(), int maxLevel=INT_MAX, Point offset=Point() )

函数总共9个参数:

    InputOutputArray image:目标图像,用来显示轮廓。

    InputArrayOfArrays contours:所有输入的轮廓。每个轮廓存储为点vector。

    int contourIdx:想要绘制轮廓的索引,如果是负值,将绘制所有的轮廓。

    const Scalar& color:绘制轮廓的颜色。

    int thickness=1:绘制轮廓线的宽度。如果为负值(例如thickness=CV_FILLED),那么填充轮廓内部。 

    int lineType:线型。

   InputArray hierarchy:可选参数,当想绘制子轮廓时有用,参考下一个参数maxLevel。

    int maxLevel:绘制轮廓的层级。如果是0,那么只绘制最外层轮廓,即树根轮廓。如果是1,那么还会增加绘制第一级子轮廓,等等。该参数只能在上一个参数hierarchy使能时有用。

   Point offset:等同函数findContours中的offset。


以下是opencv2.3.2的API Referance中提供例子:

#include "cv.h"
#include "highgui.h"

using namespace cv;

int main( int argc, char** argv )
{
    Mat src;
    // the first command-line parameter must be a filename of the binary
    // (black-n-white) image
    if( argc != 2 || !(src=imread(argv[1], 0)).data)
        return -1;

    Mat dst = Mat::zeros(src.rows, src.cols, CV_8UC3);

    src = src > 1;
    namedWindow( "Source", 1 );
    imshow( "Source", src );

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    findContours( src, contours, hierarchy,
        CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );

    // iterate through all the top-level contours,
    // draw each connected component with its own random color
    int idx = 0;
    for( ; idx >= 0; idx = hierarchy[idx][0] )
    {
        Scalar color( rand()&255, rand()&255, rand()&255 );
        drawContours( dst, contours, idx, color, CV_FILLED, 8, hierarchy );
    }

    namedWindow( "Components", 1 );
    imshow( "Components", dst );
    waitKey(0);
}

其中

vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
给contours和hierarchy分配空间,用来存储。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值