2017-05-31 216 views
0

所以,我有一個問題:C++ opencv的重縮放邊界矩形

Im做android的OpenCV中和,因爲,即時通訊與我的手機拍攝的圖像非常高的分辨率,我想調整圖像以較小的,得到輪廓和處理圖像,然後在原始圖像上創建邊界矩形。爲此,我需要縮放邊界框,以便它完美地適合我的原始圖像。我的代碼與原始圖像上的處理和繪圖邊界框非常吻合,但是,如何進行縮放?以下是代碼片段:

Mat &image = *(Mat *) matAddrRgba; 
//over here I should resize image and do the processing with the resized one, and in the end, scale everything back so I can draw the bounding box to the original 
    Rect bounding_rect; 

    Mat thr(image.rows, image.cols, CV_8UC1); 
    cvtColor(image, thr, CV_BGR2GRAY); //Convert to gray 
    threshold(thr, thr, 150, 255, THRESH_BINARY + THRESH_OTSU); //Threshold the gray 

    vector<vector<Point> > contours; // Vector for storing contour 
    vector<Vec4i> hierarchy; 
    RotatedRect rect; 
    findContours(thr, contours, hierarchy, CV_RETR_CCOMP, 
       CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image 
    sort(contours.begin(), contours.end(), 
     compareContourAreas);   //Store the index of largest contour 
    bounding_rect = boundingRect(contours[0]); 
    rect = minAreaRect(contours[0]); 
    // matrices we'll use 
    Mat rot_mat, rotated; 
    // get angle and size from the bounding box 
    float angle = rect.angle; 
    Size rect_size = rect.size; 

    if (rect.angle < -45.) { 
     angle += 90.0; 
     swap(rect_size.width, rect_size.height); 
    } 

    rot_mat = getRotationMatrix2D(rect.center, angle, 1); 

    warpAffine(image, rotated, rot_mat, image.size(), INTER_CUBIC); 

    image = rotated; 

    cvtColor(image, thr, CV_BGR2GRAY); //Convert to gray 
    threshold(thr, thr, 150, 255, THRESH_BINARY + THRESH_OTSU); //Threshold the gray 

    findContours(thr, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); 
    sort(contours.begin(), contours.end(), compareContourAreas); 
    bounding_rect = boundingRect(contours[0]); 

    image = Mat(image, bounding_rect); 
+0

「如何進行縮放」 - 除非是指攀爬或從物體上移除刻度,那麼*乘法*往往會起到訣竅的作用。你可以在新的寬度和高度上增加一個「scale-1」的偏移量,使它更好一些。拿一張方形紙畫出來 - 這不應該花費太多工作才能弄清楚。 –

+0

@DanMašek我得到了我的計算結果的旋轉矩形。然後旋轉圖像使其與我的設備完美對齊。在那之後,我用這個旋轉矩形切割(從它創建邊界矩形)。如何擴展/放大/擴大邊界矩形? –

+0

例如,如果您將圖像的寬度和高度加倍,則相同的縮放係數適用於座標。所以邊界框的x和y座標會加倍,寬度和高度會加倍。 –

回答

2

好了,所以,我就用這個啓動:

Mat &img = (Mat) matAddrRgba; 
    Mat image; 
    double thrA = 5; 
    resize(img, image, Size((int) (img.size().width/thrA), (int) (img.size().height/thrA))); 

    Rect bounding_rect; 

    Mat thr(image.rows, image.cols, CV_8UC1); 
    cvtColor(image, thr, CV_BGR2GRAY); //Convert to gray 
    threshold(thr, thr, 150, 255, THRESH_BINARY + THRESH_OTSU); //Threshold the gray 

    vector<vector<Point> > contours; // Vector for storing contour 
    vector<Vec4i> hierarchy; 
    RotatedRect rect; 
    findContours(thr, contours, hierarchy, CV_RETR_CCOMP, 
       CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image 
    sort(contours.begin(), contours.end(), 
     compareContourAreas);   //Store the index of largest contour 
    bounding_rect = boundingRect(contours[0]); 

    bounding_rect.width = (int) (bounding_rect.width * thrA); 
    bounding_rect.height = (int) (bounding_rect.height * thrA); 
    bounding_rect.x = (int) (bounding_rect.x * thrA); 
    bounding_rect.y = (int) (bounding_rect.y * thrA); 

    rectangle(img, bounding_rect, Scalar(0, 172, 236, 255), 3); 

正如你所看到的,你應該有一個尺度,該尺度應與寬度,高度乘以,邊界矩的x和y。你可以從中找出其餘的。