c# opencv 轮廓检测_OpenCV 矩形轮廓检测

本文介绍了如何在C#中利用OpenCV的findContours函数进行轮廓检测。通过遍历轮廓点并使用floodFill进行填充,详细解析了轮廓检测的主要步骤和函数用法,包括轮廓检索模式和轮廓近似方法。此外,还展示了如何处理和显示检测到的轮廓。
摘要由CSDN通过智能技术生成

基础介绍

OpenCV里提取目标轮廓的函数是findContours,它的输入图像是一幅二值图像,输出的是每一个连通区域的轮廓点的集合:vector>。外层vector的size代表了图像中轮廓的个数,里面vector的size代表了轮廓上点的个数。

轮廓进行填充的时候我会有下面2步骤:

a)依次遍历轮廓点,将点绘制到img上

void drawMaxAreaLine(cv::Mat &dst, const std::vector<:point>maxAreaPoints)

{int step =dst.step;

auto data=dst.data;for (int i = 0; i < maxAreaPoints.size(); ++i)

{*(data + maxAreaPoints[i].x + maxAreaPoints[i].y * step) = 255;

}

}

b)使用floodFill以及一个种子点进行填充

floodFill(savedGrayMat, Point(currentFrameEdge[0].x + 2, currentFrameEdge[0].y + 2), 255);

主要函数用法

C++:void findContours(InputOutputArray image,

OutputArrayOfArrays contours, int mode, int method, Point offset=Point())

Python:cv2.findContours(image,

mode, method[, contours[, hierarchy[, offset]]]) →

contours, hierarchy

C:int cvFindContours(CvArr* image,

CvMemStorage* storage, CvSeq** first_contour, int header_size=sizeof(CvContour), int mode=CV_RETR_LIST, intmethod=CV_CHAIN_APPROX_SIMPLE, CvPoint offset=cvPoint(0,0) )

Python:cv.FindContours(image,

storage, mode=CV_RETR_LIST, method=CV_CHAIN_APPROX_SIMPLE, offset=(0, 0)) → contoursParameters:

image – Source, an 8-bit single-channel image. Non-zero pixels are treated as 1’s. Zero pixels remain 0’s, so the image is treated asbinary .

You can use compare() , inRange() , threshold() , adaptiveThreshold() , Canny() ,

and others to create a binary image out of a grayscale or color one. The function modifies the image while extracting the contours. If mode

equals to CV_RETR_CCOMP orCV_RETR_FLOODFILL,

the input can also be a 32-bit integer image of labels (CV_32SC1).

contours – Detected contours. Each contour is stored as a vector of points.

hierarchy – Optional

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值