cvFindContours函数使用

CV_IMPL int
cvFindContours( void*  img,  CvMemStorage*  storage,               
                CvSeq**  firstContour, int  cntHeaderSize,                
                int  mode,
                int  method, CvPoint offset )
{
    CvContourScanner scanner = 0;
    CvSeq *contour = 0;
    int count = -1;
   
    CV_FUNCNAME( "cvFindContours" );

    __BEGIN__;

    if( !firstContour )
        CV_ERROR( CV_StsNullPtr, "NULL double CvSeq pointer" );

    if( method == CV_LINK_RUNS )
    {
        if( offset.x != 0 || offset.y != 0 )
            CV_ERROR( CV_StsOutOfRange,
            "Nonzero offset is not supported in CV_LINK_RUNS yet" );

        CV_CALL( count = icvFindContoursInInterval( img, storage,
                                    firstContour, cntHeaderSize ));
    }
    else
    {
        CV_CALL( scanner = cvStartFindContours( img, storage,
                        cntHeaderSize, mode, method, offset ));
        assert( scanner );

        do
        {
            count++;
            contour = cvFindNextContour( scanner );
        }
        while( contour != 0 );

        *firstContour = cvEndFindContours( &scanner );   
    }

    __END__;

    return count;
}


/* End of file. */

 

cvFindContours可以得到一个图象所有的轮廓,返回的是轮廓的数量.它可以对cvCanny,cvThreshold(),cvAdaptiveThreshold()函数处理得到的函数进行轮廓的提取.firstContour参数可以不用创建空间,在函数内部从函数cvFindNextContour返回轮廓的指针.最主要的是method参数,这个参数涉及轮廓的存储方式,以及什么轮廓能被发现

我可以知道怎么得到每个轮廓呢,通过函数cvStartFindContours,cvFindNextContour,cvEndFindContours对每个轮廓,可以计算它们的周长,周长太小的轮廓可以去掉/

 

//计算最大周长的轮廓.]

//lowhere write

CvMemStorage *ms = cvCreateMemStorage();
  CvContourScanner cs=cvStartFindContours(cannyImage,ms,sizeof(CvContour),CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE);
  double blCoutourLength=0.;
  CvSeq *sqMax=NULL;
  i=1;
  void *pTmp=NULL;
  cvRectTmp=cvRect(0,0,0,0);
  do
  {
   sq=cvFindNextContour(cs);
   if(sq==NULL)
    break;
    double bl=cvContourPerimeter(sq);
   if(bl>blCoutourLength)
   {
    blCoutourLength=bl;
    sqMax=sq;//得到最大周长的轮廓的点
   }

   i++;
  }while(sq!=0);
  sq=cvEndFindContours(&cs);

你可能感兴趣的:(opencv)