【问题标题】:Apply changes of ROI to original image将 ROI 的更改应用于原始图像
【发布时间】:2014-03-27 23:07:49
【问题描述】:

我的任务是对图像的 roi 执行一些操作。但是在执行这些之后,我希望这些更改也可以在原始图像的同一区域(在称为“图像”的代码中)中可见,而不仅仅是在 roi 中作为单独的图像(即“image_roi2”)。我怎样才能做到这一点?

我的代码如下所示:

Mat image;
Mat image_roi2;
float thresh;

Rect roi = Rect(x, y, widh, height);
Mat image_roi = image(roi);
threshold(image_roi, image_roi2, thresh, THRESH_TOZERO, CV_THRESH_BINARY_INV);

【问题讨论】:

标签: c++ opencv roi


【解决方案1】:

你只需要一个额外的image_roi2.copyTo( image_roi );

下面是一个完整的例子。

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

int main(int argc, char** argv) {
    if (argc != 2) {
        std::cout << " Usage: " << argv[0] << " imagem.jpg" << std::endl;
        return -1;
    }
    cv::Mat image;
    cv::Mat image_roi2;

    image = cv::imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file
    if (!image.data)                                    // Check for invalid input
    {
        std::cout << "Could not open or find the image" << std::endl;
        return -1;
    }

    cv::Rect roi( 100, 100,200, 200);

    cv::Mat image_roi = image( roi );
    cv::threshold(image_roi, image_roi2, 250, 255, CV_THRESH_BINARY_INV );
    image_roi2.copyTo( image_roi );

    cv::namedWindow("Imagem", CV_WINDOW_NORMAL | CV_WINDOW_KEEPRATIO);
    cv::resizeWindow("Imagem", 600, 400);
    cv::imshow("Imagem", image);            // Show our image inside it.
    cv::waitKey(0);                     // Wait for a keystroke in the window
    return 0;
}

【讨论】:

    【解决方案2】:

    我想这就是你想要的 - image_roi.copyTo(image(roi));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-03
      相关资源
      最近更新 更多