提取轮廓函数 cvFindContours ---OpenCV

提取轮廓在OpenCV里有一个函数 cvFindContours

http://www.myexception.cn/internet/1450415.html

[cpp] view plaincopyprint?
  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) );
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这个函数。
下边的是一位仁兄写的测试程序和测试图片,说明提取轮廓的两种方法及绘制轮廓中最大等级分析 问题,非常感谢他的分享,原文戳这里:
[cpp] view plaincopyprint?
  1. /************************************************************************/     
  2. /* 提取轮廓两种方法对比及绘制轮廓'最大等级'分析                         */     
  3. /************************************************************************/     
  4. #include "stdafx.h"     
  5. #include "cv.h"     
  6. #include "highgui.h"     
  7. int main()     
  8. {     
  9.     IplImage* img = cvLoadImage("lena.jpg", CV_LOAD_IMAGE_GRAYSCALE);     
  10.     IplImage* img_temp = cvCreateImage(cvGetSize(img), 8, 1);     
  11.     cvThreshold(img, img, 128, 255, CV_THRESH_BINARY);     
  12.     CvMemStorage* mem_storage = cvCreateMemStorage(0);     
  13.     CvSeq *first_contour = NULL, *c = NULL;     
  14.     //     
  15.     // 1、     
  16.     cvNamedWindow("contour1");     
  17.     cvCopyImage(img, img_temp);     
  18.     double t = (double)cvGetTickCount();   
  19.     cvFindContours(img_temp, mem_storage, &first_contour);     
  20.     cvZero(img_temp);     
  21.     cvDrawContours(     
  22.         img_temp,      
  23.         first_contour,     
  24.         cvScalar(100),     
  25.         cvScalar(100),     
  26.         1     
  27.         );     
  28.     t = (double)cvGetTickCount() - t;    
  29.     cvShowImage("contour1", img_temp);     
  30.     printf("run1 = %gms\n", t/(cvGetTickFrequency()*1000.));     
  31.     cvClearMemStorage(mem_storage);     
  32.     //     
  33.     // 2、     
  34.     cvNamedWindow("contour2");     
  35.     cvCopyImage(img, img_temp);     
  36.     t = (double)cvGetTickCount();   
  37.     CvContourScanner scanner = cvStartFindContours(img_temp, mem_storage);     
  38.     while (cvFindNextContour(scanner));     
  39.     first_contour = cvEndFindContours(&scanner);     
  40.     cvZero(img_temp);     
  41.     cvDrawContours(     
  42.         img_temp,      
  43.         first_contour,     
  44.         cvScalar(100),     
  45.         cvScalar(100),     
  46.         1     
  47.         );     
  48.     t = (double)cvGetTickCount() - t;    
  49.     cvShowImage("contour2", img_temp);     
  50.     printf("run2 = %gms\n", t/(cvGetTickFrequency()*1000.));     
  51.     cvClearMemStorage(mem_storage);     
  52.     cvReleaseImage(&img);     
  53.     cvReleaseImage(&img_temp);     
  54.     cvWaitKey();     
  55.     /************************************************************************/     
  56.     /* 经测试 run1 = 16.1431ms run2 = 15.8677ms (参考)
  57.        不过可以肯定这两中算法时间复杂度是相同的                                     */     
  58.     /************************************************************************/     
  59.     //     
  60.     // 上述两种方法完成了对轮廓的提取,如想绘制轮廓都得配合cvDrawContours来使用     
  61.     // 而cvDrawContours 函数第5个参数为 max_level 经查ICVL含义如下:     
  62.     //     
  63.     // 绘制轮廓的最大等级。如果等级为0,绘制单独的轮廓。如果为1,绘制轮廓及在其后的相同的级别下轮廓。     
  64.     // 如果值为2,所有的轮廓。如果等级为2,绘制所有同级轮廓及所有低一级轮廓,诸此种种。如果值为负数,     
  65.     // 函数不绘制同级轮廓,但会升序绘制直到级别为abs(max_level)-1的子轮廓。     
  66.     //     
  67.     // 相信好多读者初次都无法理解等级的含义,而且测试时候输入>=1 的整数效果几乎一样     
  68.     // 只有提取轮廓时候的提取模式设为 CV_RETR_CCOMP CV_RETR_TREE 时这个参数才有意义     
  69.     //     
  70.     // 经查FindContours 函数里面这样介绍提取模式(mode)的这两个参数:     
  71.     // CV_RETR_CCOMP - 提取所有轮廓,并且将其组织为两层的 hierarchy: 顶层为连通域的外围边界,次层为洞的内层边界。      
  72.     // CV_RETR_TREE - 提取所有轮廓,并且重构嵌套轮廓的全部 hierarchy      
  73.     //      
  74.     // 下面用第一种方法进行测试     
  75.      
  76.     cvNamedWindow("contour_test");     
  77.     cvNamedWindow("contour_raw");     
  78.     img = cvLoadImage("contour.jpg", CV_LOAD_IMAGE_GRAYSCALE);     
  79.     cvShowImage("contour_raw", img);     
  80.     cvThreshold(img, img, 128, 255, CV_THRESH_BINARY);     
  81.     img_temp = cvCloneImage(img);     
  82.     cvFindContours(     
  83.         img_temp,      
  84.         mem_storage,      
  85.         &first_contour,     
  86.         sizeof(CvContour),     
  87.         CV_RETR_CCOMP           //#1 需更改区域     
  88.         );     
  89.      
  90.     cvZero(img_temp);     
  91.     cvDrawContours(     
  92.         img_temp,      
  93.         first_contour,     
  94.         cvScalar(100),     
  95.         cvScalar(100),     
  96.         1                       //#2 需更改区域     
  97.         );     
  98.     cvShowImage("contour_test", img_temp);     
  99.     /************************************************************************/     
  100.     /* (1, 2) = (CV_RETR_CCOMP, 1)  如图1 
  101.        (1, 2) = (CV_RETR_CCOMP, 2)  如图2 
  102.        (1, 2) = (CV_RETR_TREE, 1)   如图3 
  103.        (1, 2) = (CV_RETR_TREE, 2)   如图4 
  104.        (1, 2) = (CV_RETR_TREE, 6)   如图5 
  105.        经分析CV_RETR_CCOMP 只把图像分为两个层次,顶层和次层,一等级轮廓只匹配与其最接近 
  106.        的内侧轮廓即2等级 
  107.        CV_RETR_TREE 则从轮廓外到内按等级1 - n 全部分配         
  108.        CV_RETR_LIST 全部轮廓均为1级                        */     
  109.     /************************************************************************/     
  110.     cvWaitKey();     
  111.     cvReleaseImage(&img);     
  112.     cvReleaseImage(&img_temp);     
  113.     cvReleaseMemStorage(&mem_storage);     
  114.     cvDestroyAllWindows();     
  115.     return 0;     
/************************************************************************/    
/* 提取轮廓两种方法对比及绘制轮廓'最大等级'分析                         */    
/************************************************************************/    
#include "stdafx.h"    
#include "cv.h"    
#include "highgui.h"    
    
int main()    
{    
    IplImage* img = cvLoadImage("lena.jpg", CV_LOAD_IMAGE_GRAYSCALE);    
    IplImage* img_temp = cvCreateImage(cvGetSize(img), 8, 1);    

    cvThreshold(img, img, 128, 255, CV_THRESH_BINARY);    
    CvMemStorage* mem_storage = cvCreateMemStorage(0);    
    CvSeq *first_contour = NULL, *c = NULL;    
    //    
    // 1、    
    cvNamedWindow("contour1");    
    cvCopyImage(img, img_temp);    
    double t = (double)cvGetTickCount();  
    cvFindContours(img_temp, mem_storage, &first_contour);    
    cvZero(img_temp);    
    cvDrawContours(    
        img_temp,     
        first_contour,    
        cvScalar(100),    
        cvScalar(100),    
        1    
        );    
    t = (double)cvGetTickCount() - t;   
    cvShowImage("contour1", img_temp);  
    printf("run1 = %gms\n", t/(cvGetTickFrequency()*1000.));  
    cvClearMemStorage(mem_storage);    
    //    
    // 2、    
    cvNamedWindow("contour2");    
    cvCopyImage(img, img_temp);    
    t = (double)cvGetTickCount();  
    CvContourScanner scanner = cvStartFindContours(img_temp, mem_storage);    
    while (cvFindNextContour(scanner));    
    first_contour = cvEndFindContours(&scanner);
    cvZero(img_temp);    
    cvDrawContours(    
        img_temp,     
        first_contour,    
        cvScalar(100),    
        cvScalar(100),    
        1    
        );    
    t = (double)cvGetTickCount() - t;   
    cvShowImage("contour2", img_temp);    
    printf("run2 = %gms\n", t/(cvGetTickFrequency()*1000.));   
    cvClearMemStorage(mem_storage);    
    cvReleaseImage(&img);    
    cvReleaseImage(&img_temp);    
    cvWaitKey();    
    /************************************************************************/    
    /* 经测试 run1 = 16.1431ms run2 = 15.8677ms (参考) 
       不过可以肯定这两中算法时间复杂度是相同的                                     */    
    /************************************************************************/ 
    //    
    // 上述两种方法完成了对轮廓的提取,如想绘制轮廓都得配合cvDrawContours来使用    
    // 而cvDrawContours 函数第5个参数为 max_level 经查ICVL含义如下: 
    // 绘制轮廓的最大等级。如果等级为0,绘制单独的轮廓。如果为1,绘制轮廓及在其后的相同的级别下轮廓。    
    // 如果值为2,所有的轮廓。如果等级为2,绘制所有同级轮廓及所有低一级轮廓,诸此种种。如果值为负数,    
    // 函数不绘制同级轮廓,但会升序绘制直到级别为abs(max_level)-1的子轮廓。    
    //    
    // 相信好多读者初次都无法理解等级的含义,而且测试时候输入>=1 的整数效果几乎一样    
    // 只有提取轮廓时候的提取模式设为 CV_RETR_CCOMP CV_RETR_TREE 时这个参数才有意义    
    //    
    // 经查FindContours 函数里面这样介绍提取模式(mode)的这两个参数:    
    // CV_RETR_CCOMP - 提取所有轮廓,并且将其组织为两层的 hierarchy: 顶层为连通域的外围边界,次层为洞的内层边界。     
    // CV_RETR_TREE - 提取所有轮廓,并且重构嵌套轮廓的全部 hierarchy     
    //     
    // 下面用第一种方法进行测试    
    cvNamedWindow("contour_test");    
    cvNamedWindow("contour_raw");    
    img = cvLoadImage("contour.jpg", CV_LOAD_IMAGE_GRAYSCALE);    
    cvShowImage("contour_raw", img);    
    cvThreshold(img, img, 128, 255, CV_THRESH_BINARY);    
    img_temp = cvCloneImage(img);    
    cvFindContours(    
        img_temp,     
        mem_storage,     
        &first_contour,    
        sizeof(CvContour),    
        CV_RETR_CCOMP           //#1 需更改区域    
        );    
    cvZero(img_temp);    
    cvDrawContours(    
        img_temp,     
        first_contour,    
        cvScalar(100),    
        cvScalar(100),    
        1                       //#2 需更改区域    
        );    
    cvShowImage("contour_test", img_temp);    
    /************************************************************************/    
    /* (1, 2) = (CV_RETR_CCOMP, 1)  如图1  
       (1, 2) = (CV_RETR_CCOMP, 2)  如图2  
       (1, 2) = (CV_RETR_TREE, 1)   如图3  
       (1, 2) = (CV_RETR_TREE, 2)   如图4  
       (1, 2) = (CV_RETR_TREE, 6)   如图5  
       经分析CV_RETR_CCOMP 只把图像分为两个层次,顶层和次层,一等级轮廓只匹配与其最接近  
       的内侧轮廓即2等级  
       CV_RETR_TREE 则从轮廓外到内按等级1 - n 全部分配          
       CV_RETR_LIST 全部轮廓均为1级                        */    
    /************************************************************************/   
    cvWaitKey();    
    cvReleaseImage(&img);    
    cvReleaseImage(&img_temp);    
    cvReleaseMemStorage(&mem_storage);    
    cvDestroyAllWindows();    
    return 0;    
}

原图

图一

图二


图三

图四

图五

这是OpenCV的经典一个例子:

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

主要还是理解下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级


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值