1. 程式人生 > >【OpenCV】cvFindContours引數詳解

【OpenCV】cvFindContours引數詳解

其引數列表為:

CvArr* image: 待提取輪廓的影象

(注意:該影象是經過二值化後的影象,二值化可以使用cvThreshold()函式)

CvMemStorage*  storage: 儲存空間
CvSeq**  first_contour:輪廓序列

Int mode: 模式預設情況下是CV_RETR_LIST,共有4種模式:

  • CV_RETR_EXTERNAL: 只提取最最最外面的輪廓
  • CV_RETR_LIST: 把所有輪廓都儲存下來,放在一張列表裡,可以用下標來訪問,但是不會計算輪廓之間的層次關係,也就是說並不知道輪廓之間是什麼關係
  • CV_RETR_CCOMP: gives contours and organises them into outer and inner contours. Every contour is either the outline of an object, or the outline of an object inside another object (i.e. hole). The hierarchy is adjusted accordingly. This can be useful if (say) you want to find all holes.(感覺英文的理解起來比很多部落格上給的中文理解要好)
  • CV_RETR_TREE: calculates the full hierarchy of the contours. So you can say that object1 is nested 4 levels deep within object2 and object3 is also nested 4 levels deep.

使用CV_RETR_CCOMP時,可以用cvDrawContours()來繪製輪廓,可以看到給外部輪廓和hole的輪廓用不同顏色,就一目瞭然了。

以下是cvThreshold, cvFindContours和cvDrawContours的程式碼:

CVAPI(double)  cvThreshold( const CvArr*  src, CvArr*  dst,
                            double  threshold, double  max_value,
                            int threshold_type );
/* Retrieves outer and optionally inner boundaries of white (non-zero) connected
   components in the black (zero) background */
CVAPI(int)  cvFindContours( CvArr* image, CvMemStorage* storage, CvSeq** first_contour,
                            int header_size CV_DEFAULT(sizeof(CvContour)),
                            int mode CV_DEFAULT(CV_RETR_LIST),
                            int method CV_DEFAULT(CV_CHAIN_APPROX_SIMPLE),
                            CvPoint offset CV_DEFAULT(cvPoint(0,0)));

/* Draws contour outlines or filled interiors on the image */
CVAPI(void)  cvDrawContours( CvArr *img, CvSeq* contour,
                             CvScalar external_color, CvScalar hole_color,
                             int max_level, int thickness CV_DEFAULT(1),
                             int line_type CV_DEFAULT(8),
                             CvPoint offset CV_DEFAULT(cvPoint(0,0)));