1. 程式人生 > >OpenCV之findContours函式解讀

OpenCV之findContours函式解讀

轉載自:http://blog.csdn.net/u012062327

參考:opencv documentation


findContours()函式原型:

void findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point()) 

Parameters: 
image – Source, an 8-bit single-channel image.

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



hierarchy – Optional output vector, containing information about the image topology. It has as many elements as the number of contours. For each i-th 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 contour and the parent contour, respectively. If for the contour i there 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 boundaries of the holes. If there is another contour inside a hole of a connected component, it is still put at the top level. 
CV_RETR_TREE retrieves all of the contours and reconstructs a full hierarchy of nested contours. This full hierarchy is built and shown in the OpenCV contours.c demo. 

method – Contour approximation method 
CV_CHAIN_APPROX_NONE stores absolutely all the contour points. That is, any 2 subsequent points (x1,y1) and (x2,y2) of the contour will be either horizontal, vertical or diagonal neighbors, that is, max(abs(x1-x2),abs(y2-y1))==1. 
CV_CHAIN_APPROX_SIMPLE compresses horizontal, vertical, and diagonal segments and leaves only their end points. For example, an up-right rectangular contour is encoded with 4 points. 
CV_CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS applies one of the flavors of the Teh-Chin chain approximation algorithm. 
 
offset – Optional offset by which every contour point is shifted. This is useful if the contours are extracted from the image ROI and then they should be analyzed in the whole image context.  



引數說明
image輸入影象必須為一個2值單通道影象
contours引數為檢測的輪廓陣列,每一個輪廓用一個point型別的vector表示
hiararchy引數和輪廓個數相同,每個輪廓contours[ i ]對應4個hierarchy元素hierarchy[ i ][ 0 ] ~hierarchy[ i ][ 3 ],分別表示後一個輪廓、前一個輪廓、父輪廓、內嵌輪廓的索引編號,如果沒有對應項,該值設定為負數。
mode表示輪廓的檢索模式
CV_RETR_EXTERNAL表示只檢測外輪廓
CV_RETR_LIST檢測的輪廓不建立等級關係
CV_RETR_CCOMP建立兩個等級的輪廓,上面的一層為外邊界,裡面的一層為內孔的邊界資訊。如果內孔內還有一個連通物體,這個物體的邊界也在頂層。
CV_RETR_TREE建立一個等級樹結構的輪廓。具體參考contours.c這個demo
method為輪廓的近似辦法
CV_CHAIN_APPROX_NONE儲存所有的輪廓點,相鄰的兩個點的畫素位置差不超過1,即max(abs(x1-x2),abs(y2-y1))==1
CV_CHAIN_APPROX_SIMPLE壓縮水平方向,垂直方向,對角線方向的元素,只保留該方向的終點座標,例如一個矩形輪廓只需4個點來儲存輪廓資訊
CV_CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS使用teh-Chinl chain 近似演算法
offset表示代表輪廓點的偏移量,可以設定為任意值。對ROI影象中找出的輪廓,並要在整個影象中進行分析時,這個引數還是很有用的。

findContours後會對輸入的2值影象改變,所以如果不想改變該2值影象,需建立新mat來存放,findContours後的輪廓資訊contours可能過於複雜不平滑,可以用approxPolyDP函式對該多邊形曲線做適當近似
contourArea函式可以得到當前輪廓包含區域的大小,方便輪廓的篩選
findContours經常與drawContours配合使用,用來將輪廓繪製出來。
drawContours函式原型:
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() ) 

image表示目標影象;
contours表示輸入的輪廓組,每一組輪廓由點vector構成;
contourIdx指明畫第幾個輪廓,如果該引數為負值,則畫全部輪廓;
color為輪廓的顏色;
thickness為輪廓的線寬,如果為負值或CV_FILLED表示填充輪廓內部;
lineType為線型;
hierarchy輪廓結構資訊;
maxLevel 繪畫輪廓的最大值,只有hierarchy存在是才有用

    drawContours得到了複雜輪廓往往不適合特徵的檢測,這裡再介紹一個點集凸包絡的提取函式convexHull,輸入引數就可以是contours組中的一個輪廓,返回外凸包絡的點集還可以得到輪廓的外包矩形,使用函式boundingRect,如果想得到旋轉的外包絡矩形,使用函式minAreaRect,返回值為RotatedRect;也可以得到輪廓的外包絡圓,對應的函式為minEnclosingCircle;想得到輪廓的外包絡橢圓,對應的函式為fitEllipse,返回值也是RotatedRect,可以用ellipse函式畫出對應的橢圓
    如果想獲得一點與多邊形封閉輪廓的資訊,可以呼叫pointPolygonTest函式,這個函式返回值為該點距離輪廓最近邊界的距離,為正值為在輪廓內部,負值為在輪廓外部,0表示在邊界上。