2013-02-08 89 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):如果您需要其他有用的屬性分類