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

其他提示

I think this is what you want - image_roi.copyTo(image(roi));

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top