Tonmoy
Tonmoy

Reputation: 557

Centroid of cvBoundingRect()

I'm using OpenCV 2.4.6 and Visual C++ 2010. I am trying to find the centroid of the contours of BoundingRect. Below is my code. Here I am getting the rectangles in proper positions. But the circles that i'm trying to point on the centroid of each Rectangle is shifted towards upper left side. How can i get those circle points in the centroid of the rectangles?

    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

Upvotes: 0

Views: 816

Answers (1)

HugoRune
HugoRune

Reputation: 13829

This part is wrong:

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

the centroid is rect.x + rect.width/2 , rect.y + rect.height/2 (no parenthesis)

Upvotes: 0

Related Questions