首先选取图像中的Roi区域,然后对Roi区域进行赋值,那么原图像相应的区域也跟着变化了:
dst = src.clone();
cv::Mat Roi(dst, cv::Rect(x, y, cut_out_len, cut_out_len));
cv::RNG rnger(cv::getTickCount());
rnger.fill(Roi, cv::RNG::UNIFORM, cv::Scalar::all(), cv::Scalar::all());
上述代码是将Roi区域进行随机的颜色填充
//创建一幅320×240的图像
Mat img(Size(,), CV_8UC3);
//选择一块ROI
Mat roi(img, Rect(, , , ));
//将ROI的颜色填充为绿色,原始的img图像将会改变。
roi=Scalar(,,);
cv::Mat image = cv::Mat::zeros(, , CV_8UC3);
image.setTo(cv::Scalar(, , ));
cv::imshow("original", image);
cv::Mat roi = cv::imread("E:\\Images\\Hepburn.png", cv::IMREAD_COLOR);
cv::imshow("roi", roi);
cv::Rect roi_rect = cv::Rect(, , roi.cols, roi.rows);
roi.copyTo(image(roi_rect));
cv::imshow("result", image);
cv::waitKey();
return;
另外生成一定范围的随机数:https://www.cnblogs.com/vathena/articles/4356646.html
srand((unsigned)time(null));
(low,up) #define Random (rand()%(up-low+1)) + low - 1
[low,up) #define Random (rand()%(up-low)) + low
(low,up] #define Random (rand()%(up-low))+ low + 1
[low,up] #define Random (rand()%(up-low+1)) + low