簡體   English   中英

C ++ Opencv重定界矩形

[英]C++ Opencv Rescale Bounding Rect

所以,我有一個問題:

我在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);

好的,因此,我將從此開始:

    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相乘。 您可以從中找出其余的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM