首先选取图像中的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(0), cv::Scalar::all(256));
上述代码是将Roi区域进行随机的颜色填充
//创建一幅320×240的图像 Mat img(Size(320,240), CV_8UC3); //选择一块ROI Mat roi(img, Rect(10, 10, 100, 100)); //将ROI的颜色填充为绿色,原始的img图像将会改变。 roi=Scalar(0,255,0);
cv::Mat image = cv::Mat::zeros(512, 512, CV_8UC3); image.setTo(cv::Scalar(100, 0, 0)); 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(128, 128, roi.cols, roi.rows); roi.copyTo(image(roi_rect)); cv::imshow("result", image); cv::waitKey(0); 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