pythonimageopencvwhere-clausebit-masks

replace mask with original image opencv Python


I am trying to replace objects which I found using a mask with the original images pixels. I have a mask that shows black where the object is not detected and white if detected. I am then using the image in a where statement

image[np.where((image2 == [255,255,255].any(axis = 2)) 

I am stuck here and I have no idea how to change found white values to what the original image is (to use alongside other masks). I have tried image.shape and this did not work.

Thanks.


Solution

  • Make a copy of the mask and then draw the original image over the white pixels of the mask from the white pixel coordinates. You can also check mask == 255 to compare element-wise. You don't need np.where because you can index arrays via the boolean mask created by mask == 255.

    out = mask.copy()
    out[mask == 255] = original_image[mask == 255]