2013-02-08 88 views
0

你好StackOverflowers计算包围盒

领域我已经创建了一个应用程序,使用分段INRANGE功能的预定义颜色的基础上的图像。然后,我围绕检测到的物体绘制边界框。

我在这里的问题是,我该如何确定区域的属性,如:面积,大小,高度,并用,中心点。

在这里,我放在一个画面倾印.....

enter image description here

我应该怎样的方法来获取这些边框或任何其他包围盒即得到淹没区域特性..... ..?

vector<vector<Point> > contours; 
    vector<Vec4i> hierarchy; 
    findContours(mBlur, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); 



    /// Approximate contours to polygons + get bounding rects and circles 
    vector<vector<Point> > contours_poly(contours.size()); 
    vector<Rect> boundRect(contours.size()); 
    vector<Point2f>center(contours.size()); 
    vector<float>radius(contours.size()); 

    for(int i = 0; i < contours.size(); i++) 
    { approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true); 
     boundRect[i] = boundingRect(Mat(contours_poly[i])); 
    } 


    /// Draw polygonal contour + bonding rects 
    Mat drawing = Mat::zeros(range_out.size(), CV_8UC3); 
    for(int i = 0; i< contours.size(); i++) 
    { 
     Scalar color = Scalar(255,0,255); 
     drawContours(drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point()); 
     rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0); 
    } 

问候

+1

你是如何绘制边界框的?你需要显示一些代码。 – 2013-02-08 19:46:12

+0

@sftrabbit我添加了负责填充边界框的代码。希望这有助于 – Tomazi 2013-02-08 19:51:04

回答

2

您可以使用内置的OpenCV function获取该区域。还有其他的功能,以获得你需要的一切。

1

只是遍历2D分段形状的中心坐标(在你的照片薄粉红色的线,你可以发现这只是检查其像素不是黑色,寻找到它的坐标)和存储找到最大值和最小值X和Y.然后,宽度为maxX - minX,高度为maxY - minY

+0

而我该如何做到这一点...? – Tomazi 2013-02-08 21:50:32

0
void visualizeSegments(Mat& img, Mat& dst) 
{ 
    vector<vector<Point>> contours; 
    vector<Vec4i> hierarchy; 
    findContours(img, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE); 
    dst=Mat::zeros(img.size(), CV_8UC3); 
    for(int i = 0; i < contours.size(); i++) 
    { 
     //Moments mu = moments(contours[i], true); 
     //Point2f centroid(mu.m10/mu.m00,mu.m01/mu.m00);   
     //double area = fabs(contourArea(Mat(contours[i]))); 
     //vector<Point> contours_poly; 
     //approxPolyDP(Mat(contours[i]), contours_poly, 3, true); 
     //Rect boundRect = boundingRect(Mat(contours_poly)); 
     drawContours(dst, contours, i, Scalar(255,0,0), -1, 8, hierarchy); 
    } 
} 
0

如上所述befor有在OpenCV 1.双contourArea一组有用的功能(InputArray轮廓,BOOL取向=假):向comute区域 2.双弧长(InputArray曲线,布尔闭合) :计算周长 3.矩矩(InputArray array,bool binaryImage = false):计算重心 4. void HuMoments(const Moments & m,OutputArray hu):如果您需要其他有用的属性分类