android - C++ Opencv 重新缩放边界矩形

标签 android c++ opencv

所以,我有一个问题:

我在 android 中执行 opencv,由于我用手机拍摄的图像分辨率非常高,我想将图像调整为较小的图像,获取轮廓并处理图像,然后在原始图像上创建边界矩形.为此,我需要缩放边界框以使其完全适合我的原始图像。我的代码在原始图像上处理和绘制边界框时效果很好,但是,如何进行缩放?这是代码 fragment :

 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。您可以从中找出其余部分。

关于android - C++ Opencv 重新缩放边界矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44291434/

相关文章:

c++ - 为什么在windows中注入(inject)了additional\r?

OpenCV 从瞬间完全旋转?

Android OpenCV 2.4.9 静态初始化不像示例 APK 那样工作

python-2.7 - 类型错误 : img data type = 17 is not supported

android - SurfaceTexture 大小,如何定义?

android - 键入电子邮件文本 android

java - 带有菜单图标的工具栏的默认高度

c++ - directx 9 : basic lighting on c++ 6?

c++ - 在整个 union 上使用 std::memcpy 是否保证保留事件的 union 成员?

java - 运行 exec() 时出错。命令 : Working Directory: null Environment: null - how to execute a binary in Android correctly on a non-rooted device?