我正在尝试在边界框中绘制一个点,该点将代表该框的中心点。
我已经计算了中心点,但是它仅以CMD输出,并且我不会在图像上看到该点。

我正在Visual Studio 2010 C++上使用OpenCV2.4.3

 for(int i= 0; i < boundRect.size(); i++ )
       {
            //BoundingBox Area
            boundingBoxArea.clear();
            boundingBoxArea.push_back(Point2f(boundRect[i].x, boundRect[i].y));
            boundingBoxArea.push_back(Point2f(boundRect[i].x + boundRect[i].width, boundRect[i].y));
            boundingBoxArea.push_back(Point2f(boundRect[i].x + boundRect[i].width, boundRect[i].y + boundRect[i].height));
            boundingBoxArea.push_back(Point2f(boundRect[i].x, boundRect[i].y + boundRect[i].height));

            double area0 = contourArea(boundingBoxArea);

            cout << "area of bounding box no." << i << " = " << area0 << endl;

            //Bounding Box Centroid
            area0 = (boundRect[i].x + boundRect[i].width)/2, (boundRect[i].y + boundRect[i].height)/2;

            cout<<"Rectangle " <<i<< " Centroid possition is at: " "=" <<area0<<endl;
            cout<<""<<endl;
            cout<<""<<endl;
     }

上面的代码我只用一小部分,但一部分负责边界框的计算

最佳答案

哦,您已经计算出面积了,现在您正在尝试将中心(点)设置为该面积?不好了。将您的最后几行替换为:

//Bounding Box Centroid
Point center = Point((boundRect[i].x + boundRect[i].width)/2, (boundRect[i].y + boundRect[i].height)/2);

// print it:
cout<<"Rectangle " <<i<< " Centroid position is at: " << center.x << " " << center.y << endl;

另外,您的boundingBoxArea错误。请改用原始的boundingRect [i](用于计算面积)!

关于c++ - OpenCV工程图边界框CenterPoint,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14968910/

10-12 23:19