2013-01-24 62 views
0

你好,我已經開發了一個繪製輸入圖像輪廓的軟件,現在我不想將它帶到下一層,並圍繞感興趣的對象繪製邊界框,即一個人。我查看了boundingRect()函數,但我很難理解它。也許有不同的函數算法繪製邊界框.....?繪圖在C++環境中使用OpenCV的邊界框

這裏是我的程序的代碼:

 #include "iostream" 
    #include<opencv\cv.h> 
    #include<opencv\highgui.h> 
    #include<opencv\ml.h> 
    #include<opencv\cxcore.h> 
    #include <iostream> 
    #include <string> 
    #include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat) 
    #include <opencv2/highgui/highgui.hpp> // Video write 

using namespace cv; 
using namespace std; 

Mat image; Mat image_gray; Mat image_gray2; Mat threshold_output; 
int thresh=100, max_thresh=255; 


int main(int argc, char** argv) { 

    //Load Image 
    image =imread("C:/Users/Tomazi/Pictures/Opencv/tomazi.bmp"); 

    //Convert Image to gray & blur it 
    cvtColor(image, 
     image_gray, 
     CV_BGR2GRAY); 

    blur(image_gray, 
     image_gray2, 
     Size(3,3)); 
    //Threshold Gray&Blur Image 
    threshold(image_gray2, 
     threshold_output, 
     thresh, 
     max_thresh, 
     THRESH_BINARY); 

    //2D Container 
    vector<vector<Point>> contours; 

    //Fnd Countours Points, (Imput Image, Storage, Mode1, Mode2, Offset??) 
    findContours(threshold_output, 
     contours, // a vector of contours 
     CV_RETR_EXTERNAL,// retrieve the external contours 
     CV_CHAIN_APPROX_NONE, 
     Point(0, 0)); // all pixels of each contours  

    // Draw black contours on a white image 
    Mat result(threshold_output.size(),CV_8U,Scalar(255)); 
    drawContours(result,contours, 
     -1, // draw all contours 
     Scalar(0), // in black 
     2); // with a thickness of 2 


     //Create Window 
    char* DisplayWindow = "Source"; 
    namedWindow(DisplayWindow, CV_WINDOW_AUTOSIZE); 
    imshow(DisplayWindow, result); 


    waitKey(5000); 
    return 1; 
} 

任何人都可以提出一個解決方案...?也許直接給我一些來源,教程等閱讀OpenCV文檔,並看看boundingRect()函數,我仍然不明白。請幫助:)

+0

是你的問題如何獲得邊界框的參數或如何使用'rectangle'函數實際繪製它? – sietschie

+0

both:/如果你可以指導我一些簡單的源代碼或教程,或者如果你能在這裏向我解釋它,甚至更好。謝謝 – Tomazi

回答

1

但你也可以很容易地計算邊框自己,然後使用rectangle功能吸引他們:

int maxX = 0, minX = image.cols, maxY=0, minY = image.rows; 

for(int i=0; i<contours.size(); i++) 
    for(int j=0; j<contours[i].size(); j++) 
    { 
     Point p = contours[i][j]; 

     maxX = max(maxX, p.x); 
     minX = min(minX, p.x); 

     maxY = max(maxY, p.y); 
     minY = min(minY, p.y); 
    } 

rectangle(result, Point(minX,minY), Point(maxX, maxY), Scalar(0)); 
0

This鏈接是不是有幫助?

我認爲它演示瞭如何獲取輪廓對象並使其成爲多邊形逼近,以及如何在其周圍繪製邊界矩形。

它似乎是OpenCV的基本演示之一。