Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to set image region to zeros in OpenCV C++?

Tags:

c++

opencv

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?

like image 815
Nfys Avatar asked Jul 15 '14 09:07

Nfys


2 Answers

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));
like image 67
Roger Rowland Avatar answered Nov 06 '22 14:11

Roger Rowland


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();
like image 5
LovaBill Avatar answered Nov 06 '22 15:11

LovaBill