I would like to ask which is the most efficient way to set a region of a grayscale Mat image to zeros (or any other constant value, for that matter).
Should I create a zeros image and then use copyTo()
or is there a better way?
I would use setTo()
, for example:
// load an image
cv::Mat pImage = cv::imread("someimage.jpg", CV_LOAD_IMAGE_COLOR);
// select a region of interest
cv::Mat pRoi = pImage(cv::Rect(10, 10, 20, 20));
// set roi to some rgb colour
pRoi.setTo(cv::Scalar(blue, green, red));
Let's say we paint a black rectangle in a white canvas:
cv::Mat img(100,100,CV_8U,cv::Scalar(255));
img(cv::Rect(15,15,20,40))=0;
cv::imshow("Img",img);
cv::waitKey();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With