Search code examples
opencvmask

How to apply the mask which is not a cv2.THRESH_BINARY mask to cover origin image with python-opencv?


The mask edge is soft, like this:

enter image description here

and Result with this mask in photoshop is:

enter image description here

After cv2.bitwise_and ,So bad the result is!

enter image description here

How can I handel this like photoshop,or any other method in python-opencv can do this?

Thanks!


Solution

  • If you want the transparent effect like in photoshop, you need to use an alpha channel. See this question.

    If you want to composite the image with another background, you can use the alpha matting formula I = aF+(1-a)B, where a the alpha, F the foreground and B the background. Like this:

    ex_alpha = np.repeat(alpha[:, :, np.newaxis], 3, axis=2)
    output = (foreground*ex_alpha) + (1-ex_alpha)*background
    

    The repeat exists because foreground/background are 3-channels while alpha is 1-channel.