opencv在轮廓上添加矩形边界框

时间:2013-04-15 20:40:05

标签: opencv

我已经从图像中找到了轮廓,现在我想将一个矩形边界框应用到轮廓上,另外我还希望这里的计数器应该是我使用的代码

int main ()
{

    CvSeq*contours=0;
    CvSeq* canny_contours = 0;
    CvMemStorage*storage=cvCreateMemStorage(0);
    CvMemStorage* canny_storage = cvCreateMemStorage(0);

    IplImage*a=cvLoadImage("lisc.jpg");
    IplImage*b(cvCreateImage(cvGetSize(a),IPL_DEPTH_8U,1)); 
    IplImage*c(cvCreateImage(cvGetSize(b),IPL_DEPTH_8U,1));
    IplImage*d(cvCreateImage(cvGetSize(c),IPL_DEPTH_8U,1));

    cvCvtColor(a,b,CV_RGB2GRAY);
    cvThreshold(b,c,128,255,CV_THRESH_BINARY);
    cvCanny(c,d,50,150,3);

    cvFindContours(c,storage,&contours,sizeof(CvContour), 
        CV_RETR_EXTERNAL, CV_CHAIN_CODE);
    cvFindContours(d, canny_storage, &canny_contours, sizeof(CvContour),
                   CV_RETR_EXTERNAL, CV_CHAIN_CODE);
    cvDrawContours(c,contours,cvScalar(255,255,255), cvScalarAll(255), 100);


    //for( ; canny_contours != 0; canny_contours = canny_contours->h_next )
    //{
    //     CvScalar color = CV_RGB( rand()&200, rand()&200, rand()&200 );

    /* replace CV_FILLED with 1 to see the outlines */

    cvDrawContours( d, canny_contours, cvScalar(rand()&200,rand()&200,rand()&200), 
        cvScalarAll(rand()&200),1);

    //}

    cvNamedWindow("rola",CV_GUI_NORMAL);
    cvNamedWindow("meaow0",CV_GUI_NORMAL);
    cvShowImage("rola",c);
    cvShowImage("meaow0",d);
    cvWaitKey(0);
    cvReleaseImage(&c);
    cvReleaseImage(&d);
}

1 个答案:

答案 0 :(得分:0)

我对C style API并不熟悉。但也许你可以使用以下功能。

Rect r = cvBoundingRect(points,0);

在C ++中,点是“std :: vector< cv :: Point>”。

http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=boundingrect#boundingrect