我正在使用 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);
}
}