Search code examples
pythonnumpyimage-processingmaskopencv

Unable to add mask to an image using both cv2 and numpy


This is the original image:

plt.imshow(new_image)

enter image description here

This array was generated by a semantic-segmentation model :

print(image_mask)

array([[2, 2, 2, ..., 7, 7, 7],
       [2, 2, 2, ..., 7, 7, 7],
       [2, 2, 2, ..., 7, 7, 7],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]])

When one plots this as an image using matplotlib it adds false colors and makes an image:

plt.imshow(image_mask)

enter image description here

To change it into an image i did:

image_mask_copy = image_mask.copy()
np.place(image_mask_copy,image_mask_copy!=15,[0]) # REMOVE ALL EXCEPT PEOPLE, == 0
np.place(image_mask_copy,image_mask_copy==15,[255]) # MAKE PEOPLE == 255
new_image_mask = np.expand_dims(image_mask_copy,-1)*np.ones((1,1,3))
plt.imshow(new_image_mask)

enter image description here

But when I try to do cv2.bitwise_and I get the original image again instead of an image with only the people...:

new_image = cv2.bitwise_and(image,image,new_image_mask)
plt.imshow(new_image)

enter image description here

And I get the mask when I try numpy..:

image_mask_copy = image_mask.copy()
np.place(image_mask_copy,image_mask_copy!=15,[0])
np.place(image_mask_copy,image_mask_copy==15,[1]) #NOTICE 1 NOT 255

new_image = np.multiply(new_image_mask,image)
plt.imshow(new_image)

enter image description here

I can't understand why this is happening... Please help


Solution

  • bitwise_and takes 3 arguments. cv2.bitwise_and(src1, src2, mask)
    

    and calculates the bitwise_and of src1 and src2 for each pixel that is != 0 in mask.

    https://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html

    try

    new_image = cv2.bitwise_and(image,new_image_mask)
    plt.imshow(new_image)
    

    instead of

    new_image = cv2.bitwise_and(image,image,new_image_mask)
    plt.imshow(new_image)