将ROI的更改应用于原始图像

时间:2014-03-23 16:42:07

标签: c++ opencv roi

我的任务是对图像的roi执行一些操作。但是在执行这些之后,我希望更改也可以在原始图像的相同区域中显示(在代码中称为"图像"),而不仅仅是在roi中作为单独的图像(这是&#34) ; image_roi2&#34)。我怎么能做到这一点?

我的代码如下所示:

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

2 个答案:

答案 0 :(得分:0)

您只需要额外的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;
}

答案 1 :(得分:0)

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