I read an image with ndimage
, which results in a binary image like this:
I would like to invert the image such that white turns into black, and vice versa.
Help is appreciated.
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.
In order to invert the image, we have to apply the broadcasting technique using NumPy. We have to subtract each pixel value from 255. It is because the highest pixel value or color value is 255 in an image. Either way, we can apply the '~' (negation) operation to the image.
If you have a binary image binImage with just zeroes and ones, there are a number of simple ways to invert it: binImage = ~binImage; binImage = 1-binImage; binImage = (binImage == 0); Then just save the inverted image using the function IMWRITE.
numpy.invert(close_img)
I use invert array. It works for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With