cvBoundingRect()的质心

时间:2013-09-28 10:17:21

标签: opencv

我正在使用OpenCV 2.4.6和Visual C ++ 2010.我试图找到BoundingRect轮廓的质心。以下是我的代码。在这里,我将矩形放在适当的位置。但是,我试图指向每个矩形的质心的圆圈向左上方移动。如何在矩形的质心中获得这些圆点?

    int x = cvFindContours(imgThresh,storage,&contours,sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
    //printf("%d\n",contours->total);
    for (; contours != 0; contours = contours->h_next)
    {
        rect = cvBoundingRect(contours); //extract bounding box for current contour
        //drawing rectangle
        printf("%d   %d  %d  %d\n",rect.x, rect.y,rect.width,rect.height);
        if(rect.width*rect.height<1400 && (rect.width*rect.height)>700)
        {
            cvRectangle(mCVImageColor,cvPoint(rect.x, rect.y),cvPoint(rect.x+rect.width, rect.y+rect.height),cvScalar(0, 0, 255),2, 8, 0); 
            CvPoint centroid[1];
            centroid[0].x = ((rect.x+rect.width)/2);
            centroid[0].y = ((rect.y+rect.height)/2);
            cvCircle( mCVImageColor, centroid[0], 5, CV_RGB(255, 0, 0),-1,0,0);
        }
    }

enter image description here

1 个答案:

答案 0 :(得分:0)

这部分错了:

centroid[0].x = ((rect.x+rect.width)/2);
centroid[0].y = ((rect.y+rect.height)/2);

质心为rect.x + rect.width/2rect.y + rect.height/2(无括号)