OpenCV: Invert a mask?


Question

Is there a simple way to invert a mask using OpenCV? For example, if I've got a mask like this:

010
111
010

I'd like to invert it and get this:

101
000
101

Note: I'm using OpenCV's Python bindings, so while it would be possible to simply loop over each element in the mask, execution speed could become an issue.

1
25
1/25/2017 6:57:55 PM

Accepted Answer

If you have an 8-bit mask, then you should do mask = 255 - mask. cv::Mat subtraction operator is overloaded to do scalar per-element subtraction.

31
10/11/2011 11:58:49 PM

cv2.bitwise_not(mask) would help here


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon