Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV bitwise_and + mask

Tags:

opencv

I have this mask :

mask

Let's say I would like to make a bitwise_and with another image to fill in the white this works:

cv::bitwise_and(srcImage, mask, dstImage, [mask???]);

Now, the resulting image gives something like this :

Image + Mask

This is fine and dandy, but I'd like to subtract the black from the result. I've seen that bitwise_and also accepts an 8-bit single channel image, I'm not exactly sure what it does. I suppose if it's 0 then it let's it pass otherwise it ignores it.

So how would I use the same mask to convert it as an 8-bit image and use it to remove the black of the result? I'd like the same image, but without the black border essentially.

like image 762
Patrick.SE Avatar asked Jul 18 '12 01:07

Patrick.SE


People also ask

What does cv2 Bitwise_and do?

cv2. bitwise_and() is a function that performs bitwise AND processing as the name suggests. The AND of the values for each pixel of the input images src1 and src2 is the pixel value of the output image. Here, a grayscale image is used as a mask image for src2 .

How do I mask an image in OpenCV?

To invert a mask in OpenCV, we use the cv2. bitwise_not() function, which performs bitwise not operation on individual pixels. Parameters: masked_image: It is the image that is to be inverted.

How do I put 2 photos on cv2?

You can add two images with the OpenCV function, cv. add(), or simply by the numpy operation res = img1 + img2.

What is OpenCV mask?

Masking is a common technique to extract the Region of Interest (ROI). In openCV, it is possible to construct arbitrary masking shape using draw function and bitwise operation.


1 Answers

The solution is pretty simple. Since I didn't know much about the openCV library I wasn't using the right function for the task. I'm still not certain for sure why the application crashed when passing a mask to the bitwise_and method though.

Essentially all you have to do is the following :

image.copyTo(dst, mask);

This will copy the image to dst and even handle the details of the mat to give it the same properties as 'image'. Just have to make sure to have mask be an 8-bit single channel image. This way when you call the method only the pixels that pass the mask will be copied to dst.

Here's the doc link for more information:copyTo

like image 137
Patrick.SE Avatar answered Nov 29 '22 13:11

Patrick.SE