1 轮廓检测
图像目标中的形状检测是图像识别的重要的技术之一,对物体进行检测并提取,首先要做的就是提取物体的轮廓信息,然后在通过点集特征选择相应的算法进行处理,最后得到物体的形状信息。轮廓形状是我们看到物体最开始的印象,轮廓提取的原理是通过对图像进行二值化,利用边缘点连接的层次差别,提取位于数结构特征高的区域点集构成的集合,这部分最可能是物体的轮廓。
主要用到的函数findContours()对物体轮廓进行检测。
void findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())
- 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 as binary . 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 imagewhile extracting the contours.
- contours – Detected contours. Each contour is stored as a vector of points.
- hiararchy – Optional output vector containing information about the image topology. It has as many elements as the number of contours. For each contour contours[i] , the elements hierarchy[i][0] , hiearchy[i][1] ,hiearchy[i][2] , and hiearchy[i][3] are set to 0-based indices in contours of the next and previous contours at the same hierarchical level: the first child contourand the parent contour, respectively. If for a contour ithere are no next, previous, parent, or nested contours, the corresponding elements of hierarchy[i] will be negative.
- mode –
Contour retrieval mode.
- CV_RETR_EXTERNAL retrieves only the extreme outer contours. It sets hierarchy[i][2]=hierarchy[i][3]=-1 for all the contours.
- CV_RETR_LIST retrieves all of the contours without establishing any hierarchical relationships.
- CV_RETR_CCOMP retrieves all of the contours and organizes them into a two-level hierarchy. At the top level, there are external boundaries of the components. At the second level, there are bound