【问题标题】:opencv Adding rectangular bounding box to the contoursopencv 向轮廓添加矩形边界框
【发布时间】:2013-04-16 08:36:32
【问题描述】:

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

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

【问题讨论】:

    标签: opencv


    【解决方案1】:

    我不熟悉 C 风格的 API。但也许你可以使用以下功能。

    Rect r = cvBoundingRect(points,0);
    

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

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

    【讨论】:

    • 我找不到合适的语法在我的代码中实现它们
    猜你喜欢
    • 2021-11-19
    • 2013-05-08
    • 2015-08-23
    • 2018-03-13
    • 2021-09-06
    • 2017-05-23
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    相关资源
    最近更新 更多