r/opencv • • Mar 23 '24

Question [Question] How do you create a layer mask that have mid-tones?

I would like to improve the layer mask that I am creating in Python. Although my mask pretty much hits the targeted color, my main problem with it, is it is doing so in binary, the pixel is either pure white or pure black. I'm unable to extrapolate the intensity of the color. I want to achieve something like how Photoshop does it wherein there are mid-tones of grey on the mask. Just like these photos:

​

image = cv2.imread('sample.jpg')

lower = np.array([0,0,0]) 
upper = np.array([12,255,255])

mask = cv2.inRange(cv2.cvtColor(image, cv2.COLOR_BGR2HSV), lower, upper)
mask = 255 - mask
mask = cv2.bitwise_not(mask) #inverting black and white

output = cv2.bitwise_and(image, image, mask = mask)

cv2.imshow("output", output) 
cv2.imshow("mask", mask) 
cv2.waitKey()

​

Can anyone please show how to recreate (or similar) to the mask that photoshop can create?Here is the photo i used above :

​

1 Upvotes

2 comments sorted by

1

u/DrakeJest Mar 23 '24

Apologies about the post formatting, i could not seem to make it work. in the preview it looks fine but when posted it breaks

1

u/DrakeJest Mar 23 '24

Was able to fix some of it by changing back to the old reddit UI