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