Reputation: 28012
I have an image img. I also have a mask with value 255 at all the places where I want to retain the pixel values of img are 0 at all other places.
I want to use these two images viz. the mask and img such that I create a matrix with original img values at places where the mask is 255, and the value -1 at all places where mask is 0.
So, far, I have written this:
maskedImg = cv2.bitwise_and(img, mask)
but the maskedImg has 0 at all the places where the mask has 0. How can I get the value -1 instead of 0 at all the other places using a fast bitwise operation?
Upvotes: 1
Views: 4481
Reputation: 18331
I don't know what is your image's dtype. Default is np.uint8, so you cann't set -1 on the result, it underflows to -1 + 256 = 255
. That is to say, if the dtype is np.uint8
, you cann't set it to negative value.
If you want to set to -1, you should change the dtype
.
#masked = cv2.bitwise_and(img, mask).astype(np.int32)
masked = np.int32(img)
masked[mask==0] = -1
Upvotes: 2