0

我已经从图像中找到了轮廓现在我想将一个矩形边界框分别应用于轮廓我也希望计数应该被着色这里是我正在使用的代码

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);
}
4

1 回答 1

0

我不熟悉 C 风格的 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

于 2013-04-16T00:36:32.167 回答