OpenCV bitwise_and + mask

I have this mask:

mask

Let's say I would like to do bitwise_and with another image to fill in white, this works:

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

Now the resulting image gives something like this:

Image + Mask

It's fine and dandy, but I would like to subtract black from the result. I saw that bitwise_and also accepts an 8-bit single-channel image, I'm not quite sure what it does. I suppose if it is 0, then it skips it, 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 result? I need the same image, but without a black border.

+6
source share
2 answers

The solution is pretty simple. Since I had little knowledge of the openCV library, I did not use the correct function for this task. I'm still not sure exactly why the application crashed when passing the mask to the bitwise_and method.

Essentially, all you have to do is the following:

 image.copyTo(dst, mask); 

This will copy the image to dst and even process the details of the mat to give it the same properties as the "image". You just need to make sure that the mask will be an 8-bit single-channel image. That way, when you call the method, only the pixels passing the mask will be copied to dst.

Here's a link to the doc for more info: copyTo

+7
source

just create a new canvas larger than fox_image

then use the AND method

or you can translate the black part of the first image to white: 0 --- 255

then the problem will be solved.

0
source

Source: https://habr.com/ru/post/920760/


All Articles