OpenCV函數cvFindContours

轉載地址 http://blog.csdn.net/augusdi/article/details/9000893


提取輪廓在OpenCV裏有一個函數 cvFindContours 

  1. int cvFindContours( CvArr* image, CvMemStorage* storage, CvSeq** first_contour,int header_size=sizeof(CvContour),int mode=CV_RETR_LIST,int method=CV_CHAIN_APPROX_SIMPLE, CvPoint offset=cvPoint(0,0) );  

這個函數用起來很方便,但是隨着你使用的深入,你會發現有一些迷惑在這裏。比如當你提取輪廓時只需要最外圍的一個輪廓,但是你會發現當輪廓畫出來時是好幾個;當你需要找一個最大輪廓時卻發現找出來的卻根本就不是你想要的那個。帶着這樣問題我們再來仔細看看cvFindContours這個函數。
下邊的是一位仁兄寫的測試程序和測試圖片,說明提取輪廓的兩種方法及繪製輪廓中最大等級分析問題,非常感謝他的分享,原文戳這裏
  1. /************************************************************************/      
  2. /* 提取輪廓兩種方法對比及繪製輪廓'最大等級'分析                         */      
  3. /************************************************************************/      
  4. #include "stdafx.h"      
  5. #include "cv.h"      
  6. #include "highgui.h"      
  7.       
  8. int main()      
  9. {      
  10.     IplImage* img = cvLoadImage("lena.jpg", CV_LOAD_IMAGE_GRAYSCALE);      
  11.     IplImage* img_temp = cvCreateImage(cvGetSize(img), 8, 1);      
  12.   
  13.     cvThreshold(img, img, 128, 255, CV_THRESH_BINARY);      
  14.   
  15.     CvMemStorage* mem_storage = cvCreateMemStorage(0);      
  16.     CvSeq *first_contour = NULL, *c = NULL;      
  17.   
  18.     //////////////////////////////////////////////////////////////////////////      
  19.     // 1、      
  20.     cvNamedWindow("contour1");      
  21.     cvCopyImage(img, img_temp);      
  22.     double t = (double)cvGetTickCount();    
  23.     cvFindContours(img_temp, mem_storage, &first_contour);      
  24.     cvZero(img_temp);      
  25.     cvDrawContours(      
  26.         img_temp,       
  27.         first_contour,      
  28.         cvScalar(100),      
  29.         cvScalar(100),      
  30.         1      
  31.         );      
  32.     t = (double)cvGetTickCount() - t;     
  33.     cvShowImage("contour1", img_temp);      
  34.   
  35.     printf("run1 = %gms\n", t/(cvGetTickFrequency()*1000.));      
  36.   
  37.     cvClearMemStorage(mem_storage);      
  38.   
  39.     //////////////////////////////////////////////////////////////////////////      
  40.     // 2、      
  41.     cvNamedWindow("contour2");      
  42.     cvCopyImage(img, img_temp);      
  43.     t = (double)cvGetTickCount();    
  44.     CvContourScanner scanner = cvStartFindContours(img_temp, mem_storage);      
  45.     while (cvFindNextContour(scanner));      
  46.     first_contour = cvEndFindContours(&scanner);      
  47.           
  48.     cvZero(img_temp);      
  49.     cvDrawContours(      
  50.         img_temp,       
  51.         first_contour,      
  52.         cvScalar(100),      
  53.         cvScalar(100),      
  54.         1      
  55.         );      
  56.     t = (double)cvGetTickCount() - t;     
  57.     cvShowImage("contour2", img_temp);      
  58.         
  59.     printf("run2 = %gms\n", t/(cvGetTickFrequency()*1000.));      
  60.           
  61.     cvClearMemStorage(mem_storage);      
  62.     cvReleaseImage(&img);      
  63.     cvReleaseImage(&img_temp);      
  64.       
  65.     cvWaitKey();      
  66.     
  67.     /************************************************************************/      
  68.     /* 經測試 run1 = 16.1431ms run2 = 15.8677ms (參考)  
  69.        不過可以肯定這兩中算法時間複雜度是相同的                                     */      
  70.     /************************************************************************/      
  71.           
  72.     //////////////////////////////////////////////////////////////////////////      
  73.     // 上述兩種方法完成了對輪廓的提取,如想繪製輪廓都得配合cvDrawContours來使用      
  74.     // 而cvDrawContours 函數第5個參數爲 max_level 經查ICVL含義如下:      
  75.     //      
  76.     // 繪製輪廓的最大等級。如果等級爲0,繪製單獨的輪廓。如果爲1,繪製輪廓及在其後的相同的級別下輪廓。      
  77.     // 如果值爲2,所有的輪廓。如果等級爲2,繪製所有同級輪廓及所有低一級輪廓,諸此種種。如果值爲負數,      
  78.     // 函數不繪製同級輪廓,但會升序繪製直到級別爲abs(max_level)-1的子輪廓。      
  79.     //      
  80.     // 相信好多讀者初次都無法理解等級的含義,而且測試時候輸入>=1 的整數效果幾乎一樣      
  81.     // 只有提取輪廓時候的提取模式設爲 CV_RETR_CCOMP CV_RETR_TREE 時這個參數纔有意義      
  82.     //      
  83.     // 經查FindContours 函數裏面這樣介紹提取模式(mode)的這兩個參數:      
  84.     // CV_RETR_CCOMP - 提取所有輪廓,並且將其組織爲兩層的 hierarchy: 頂層爲連通域的外圍邊界,次層爲洞的內層邊界。       
  85.     // CV_RETR_TREE - 提取所有輪廓,並且重構嵌套輪廓的全部 hierarchy       
  86.     //       
  87.     // 下面用第一種方法進行測試      
  88.       
  89.     cvNamedWindow("contour_test");      
  90.     cvNamedWindow("contour_raw");      
  91.     img = cvLoadImage("contour.jpg", CV_LOAD_IMAGE_GRAYSCALE);      
  92.     cvShowImage("contour_raw", img);      
  93.     cvThreshold(img, img, 128, 255, CV_THRESH_BINARY);      
  94.     img_temp = cvCloneImage(img);      
  95.     cvFindContours(      
  96.         img_temp,       
  97.         mem_storage,       
  98.         &first_contour,      
  99.         sizeof(CvContour),      
  100.         CV_RETR_CCOMP           //#1 需更改區域      
  101.         );      
  102.       
  103.     cvZero(img_temp);      
  104.     cvDrawContours(      
  105.         img_temp,       
  106.         first_contour,      
  107.         cvScalar(100),      
  108.         cvScalar(100),      
  109.         1                       //#2 需更改區域      
  110.         );      
  111.     cvShowImage("contour_test", img_temp);      
  112.     /************************************************************************/      
  113.     /* (1, 2) = (CV_RETR_CCOMP, 1)  如圖1   
  114.        (1, 2) = (CV_RETR_CCOMP, 2)  如圖2   
  115.        (1, 2) = (CV_RETR_TREE, 1)   如圖3   
  116.        (1, 2) = (CV_RETR_TREE, 2)   如圖4   
  117.        (1, 2) = (CV_RETR_TREE, 6)   如圖5   
  118.        經分析CV_RETR_CCOMP 只把圖像分爲兩個層次,頂層和次層,一等級輪廓只匹配與其最接近   
  119.        的內側輪廓即2等級   
  120.        CV_RETR_TREE 則從輪廓外到內按等級1 - n 全部分配           
  121.        CV_RETR_LIST 全部輪廓均爲1級                        */      
  122.     /************************************************************************/      
  123.       
  124.     cvWaitKey();      
  125.     cvReleaseImage(&img);      
  126.     cvReleaseImage(&img_temp);      
  127.     cvReleaseMemStorage(&mem_storage);      
  128.     cvDestroyAllWindows();      
  129.     return 0;      
  130. }  

原圖

圖一

圖二


圖三

圖四

圖五

這是OpenCV的經典一個例子:

  1. #include "cv.h"  
  2. #include "cxcore.h"  
  3. #include "highgui.h"  
  4. #include <math.h>  
  5. #endif  
  6.    
  7. #pragma   comment(lib,"cv.lib")    
  8. #pragma   comment(lib,"highgui.lib")    
  9. #pragma   comment(lib,"cxcore.lib")  
  10.   
  11. #define w 500  
  12. int levels = 3;  
  13. CvSeq* contours = 0;  
  14.    
  15. void on_trackbar(int pos)  
  16. {  
  17.     IplImage* cnt_img = cvCreateImage( cvSize(w,w), 8, 3 );  
  18.     CvSeq* _contours = contours;  
  19.     int _levels = levels - 3;  
  20.     if( _levels <= 0 ) // get to the nearest face to make it look more funny  
  21.         _contours = _contours->h_next->h_next->h_next->h_next->h_next->h_next->h_next->v_next->h_next->h_next;  
  22. //_contours = _contours->v_next;  
  23.     cvZero( cnt_img );  
  24.     cvDrawContours( cnt_img, _contours, CV_RGB(255,0,0), CV_RGB(0,255,0), _levels);//, 3, CV_AA, cvPoint(0,0) );  
  25.     /*_levels: 
  26. 3,所有外輪廓及包含的內輪廓及裏面的內輪廓 
  27. 2:所有外輪廓及包含的內輪廓 
  28. 1:所有外輪廓 
  29. 0,第一個外輪廓 
  30. -1:第一個外輪廓及包含的內輪廓 
  31. -2:第一個外輪廓及包含的內輪廓及裏面的內輪廓 
  32.  
  33.  
  34.    _contours->h_next:同級的下一個輪廓 
  35. _contours->v_next父級下的下層區域; 
  36. */  
  37. cvShowImage( "contours", cnt_img );  
  38.     cvReleaseImage( &cnt_img );  
  39. }  
  40.    
  41. int main( int argc, char** argv )  
  42. {  
  43.     int i, j;  
  44.     CvMemStorage* storage = cvCreateMemStorage(0);  
  45.     IplImage* img = cvCreateImage( cvSize(w,w), 8, 1 );  
  46.    
  47.     cvZero( img );  
  48.    
  49.     for( i=0; i < 6; i++ )  
  50.     {  
  51.         int dx = (i%2)*250 - 30;//0%2=0;  
  52.         int dy = (i/2)*150;  
  53.         CvScalar white = cvRealScalar(255);  
  54.         CvScalar black = cvRealScalar(0);  
  55.    
  56.         if( i == 0 )  
  57.         {  
  58.             for( j = 0; j <= 10; j++ )  
  59.             {  
  60.                 double angle = (j+5)*CV_PI/21;  
  61.                 cvLine(img, cvPoint(cvRound(dx+100+j*10-80*cos(angle)),  
  62.                     cvRound(dy+100-90*sin(angle))),  
  63.                     cvPoint(cvRound(dx+100+j*10-30*cos(angle)),  
  64.                     cvRound(dy+100-30*sin(angle))), white, 1, 8, 0);  
  65.             }  
  66.         }  
  67.    
  68.         cvEllipse( img, cvPoint(dx+150, dy+100), cvSize(100,70), 0, 0, 360, white, -1, 8, 0 );  
  69.         cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(30,20), 0, 0, 360, black, -1, 8, 0 );  
  70.         cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(30,20), 0, 0, 360, black, -1, 8, 0 );  
  71.         cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(15,15), 0, 0, 360, white, -1, 8, 0 );  
  72.         cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(15,15), 0, 0, 360, white, -1, 8, 0 );  
  73.         cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(5,5), 0, 0, 360, black, -1, 8, 0 );  
  74.         cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(5,5), 0, 0, 360, black, -1, 8, 0 );  
  75.         cvEllipse( img, cvPoint(dx+150, dy+100), cvSize(10,5), 0, 0, 360, black, -1, 8, 0 );  
  76.         cvEllipse( img, cvPoint(dx+150, dy+150), cvSize(40,10), 0, 0, 360, black, -1, 8, 0 );  
  77.         cvEllipse( img, cvPoint(dx+27, dy+100), cvSize(20,35), 0, 0, 360, white, -1, 8, 0 );  
  78.         cvEllipse( img, cvPoint(dx+273, dy+100), cvSize(20,35), 0, 0, 360, white, -1, 8, 0 );  
  79.     }  
  80.    
  81.     cvNamedWindow( "image", 1 );  
  82.     cvShowImage( "image", img );  
  83.    
  84.     cvFindContours( img, storage, &contours, sizeof(CvContour),  
  85.                     2, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );  
  86.    
  87.     // comment this out if you do not want approximation  
  88.     contours = cvApproxPoly( contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, 3, 1 );  
  89.  //cvApproxPoly:                                                 逼近方法     精度 逼近曲線是否封閉  
  90.   
  91.   
  92.     cvNamedWindow( "contours", 1 );  
  93.     cvCreateTrackbar( "levels+3""contours", &levels, 7, on_trackbar );  
  94.    
  95.     on_trackbar(0);  
  96.     cvWaitKey(0);  
  97.     cvReleaseMemStorage( &storage );  
  98.     cvReleaseImage( &img );  
  99.    
  100.     return 0;  
  101. }  

主要還是理解下int mode=CV_RETR_LIST,int method=CV_CHAIN_APPROX_SIMPLE,CvPoint offset=cvPoint(0,0));

當mode 爲CV_RETR_CCOMP 只把圖像分爲兩個層次,頂層和次層,一等級輪廓只匹配與其最接近  ;

cvDrawContours 函數第5個參數爲 max_level=0時,笑臉圖像會顯示第一個找到的輪廓,左邊的白色耳朵一隻;

max_level=1時,所有白色區域的輪廓都會被顯示出來,因爲他們都屬於等級1;

max_level=2時;每個白色區域裏面的黑色區域會被顯示出來,可能一個白色區域下面有多個黑色區域,但他們都是同級的;

這裏你要注意的的是每個白色區域下的黑色區域,如臉下面有4個黑色區域,白色眼珠下有一個黑色區域,這個黑色區域與臉下的那三個區域時同級的,也就是說他不屬於臉的內區域,他是白色眼珠的內區域;

當mode爲       CV_RETR_LIST 全部輪廓均爲1級

發佈了8 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章