简体   繁体   中英

Apply changes of ROI to original image

My task is to perform some operations on the roi of an image. But after performing these, I want the changes also to be made visible in the same region of the original image (in code called "image"), not just in the roi as seperate image (which is "image_roi2"). How could I achieve this?

My code looks like this:

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

You just need an additional image_roi2.copyTo( image_roi );

Below is an entire example.

#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;
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM