基础介绍
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