Opencv 利用分割的映像掩膜來完善檢測前景

來源:互聯網
上載者:User

 在目標檢測中,經常會出現目標斷開、不連續或空洞的情況,針對該鐘情況,我們總是要想各種方法去填補空洞,使斷開的部分串連起來。下面介紹一個函數來實現該功能:

void cvRefineForegroundMaskBySegm( CvSeq* segments, IplImage*  pFrImg ){   IplImage* tmp_image = cvCreateImage(cvSize(pFrImg->width,pFrImg->height),                                            IPL_DEPTH_8U, 1);  for( ; segments; segments = ((CvSeq*)segments)->h_next )  {      CvSeq seq = *segments;      seq.v_next = seq.h_next = NULL;     cvZero(tmp_image);     cvDrawContours( tmp_image, &seq, CV_RGB(0, 0, 255), CV_RGB(0, 0, 255), 10, -1);     int num1 = cvCountNonZero(tmp_image);      cvAnd(tmp_image, pFrImg, tmp_image);     int num2 = cvCountNonZero(tmp_image);    if( num2 > num1*0.5 )          cvDrawContours( pFrImg, &seq, CV_RGB(0, 0, 255), CV_RGB(0, 0, 255), 10, -1);    else        cvDrawContours( pFrImg, &seq, CV_RGB(0, 0, 0), CV_RGB(0, 0, 0), 10, -1);  }      cvReleaseImage(&tmp_image);   }

其中,segments是利用映像分割演算法得到的輪廓序列,而pFrImg則是目標檢測演算法檢測到的前景目標,這樣就可以分割映像的結果來對目標檢測的地區進行填充。另外,對於如何得到分割映像,也可以採用以下函數來實現:

CvSeq*cvSegmentFGMask( CvArr* _mask, int poly1Hull0, float perimScale,                 CvMemStorage* storage, CvPoint offset ){    CvMat mstub, *mask = cvGetMat( _mask, &mstub );    CvMemStorage* tempStorage = storage ? storage : cvCreateMemStorage();    CvSeq *contours, *c;    int nContours = 0;    CvContourScanner scanner;        // clean up raw mask    cvMorphologyEx( mask, mask, 0, 0, CV_MOP_OPEN, 1 );    cvMorphologyEx( mask, mask, 0, 0, CV_MOP_CLOSE, 1 );    // find contours around only bigger regions    scanner = cvStartFindContours( mask, tempStorage,        sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, offset );        while( (c = cvFindNextContour( scanner )) != 0 )     {        double len = cvContourPerimeter( c );        double q = (mask->rows + mask->cols)/perimScale; // calculate perimeter len threshold        if( len < q ) //Get rid of blob if it's perimeter is too small            cvSubstituteContour( scanner, 0 );        else //Smooth it's edges if it's large enough        {            CvSeq* newC;            if( poly1Hull0 ) //Polygonal approximation of the segmentation                 newC = cvApproxPoly( c, sizeof(CvContour), tempStorage, CV_POLY_APPROX_DP, 2, 0 );             else //Convex Hull of the segmentation                newC = cvConvexHull2( c, tempStorage, CV_CLOCKWISE, 1 );            cvSubstituteContour( scanner, newC );            nContours++;        }    }    contours = cvEndFindContours( &scanner );    // paint the found regions back into the image    cvZero( mask );    for( c=contours; c != 0; c = c->h_next )         cvDrawContours( mask, c, cvScalarAll(255), cvScalarAll(0), -1, CV_FILLED, 8,            cvPoint(-offset.x,-offset.y));    if( tempStorage != storage )    {        cvReleaseMemStorage( &tempStorage );        contours = 0;    }    return contours;}

_mask是一個二值映像,可以是Canny邊緣檢測或cvThreshold等函數得到的二值映像。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.