Thalia
Thalia

Reputation: 14615

Combine two images based on a black and white mask

I want to create a mask operation...

I have two input images, of the same size (do they have to have the same depth/number of channels ? I'd like to be anything, likely 3 channels, CV_32FC3 or gray...) and I created a mask, of the same size (rows and cols)

cv::Mat mask = cv::Mat(image1.rows, image1.cols, CV_8UC1);

The mask is created with areas of black and white.

I would like to create a new cv::Mat, that will have image1 where mask has 1, and image2 where mask has 0.

I looked into cv::filter2D and copyTo... Also looked at addWeighted, but I don't want to blend them - the areas for each image should be completely separate. A roi would not help - the mask is likely to not contain a rectangle, but one or more polygons.

I can't find something that does what I want.

Is there any OpenCV function that combines my images based on the mask ? Or do I have to create my own, looping through rows and cols ?

Thank you.

Upvotes: 4

Views: 11160

Answers (1)

Cloud
Cloud

Reputation: 19333

Just use the bitwise_and() function and you're set. The references below include a full working example.

References:


  1. How to "zero" everything within a masked part of an image in OpenCV
  2. OpenCV bitwise_and + mask

Upvotes: 5

Related Questions