I wrote a program to change the color of the skin in the photo.
First I get a skin mask, then I convert BGR image to HSV. add V channel value in mask. Like this:
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
H, S, V = cv2.split(hsv)
NV = np.where(skin_mask > 0, V + skin_mask / 255 * 50, V).astype(np.uint8)
NHSV = cv2.cvtColor(cv2.merge([H, S, NV]), cv2.COLOR_HSV2BGR)
But some original white pixel become to black, I think maybe V + skin_mask / 255 * 50 let pixel over 255.
so I try to:
NV = np.where(skin_mask > 0, np.where(V + skin_mask / 255 * 50 > 255, 255, V + skin_mask / 255 * 50), V).astype(np.uint8)
It's work. but ugly.
I want to know how to beautify this writing, don't use np.where includes np.where. Thank you very much!!!
It may be more elegant to use
skin_maskas a mask, instead of applying arithmetic likeskin_mask / 255 * 50.You may solve it using cv2.add:
Advantages of using
cv2.addover NumPy arithmetic:cv2.addsupportsmaskargument (mask element values are usually0and255).cv2.addclips the result to the valid range of uint8 [0, 255], without overflow.Code I used for testing the solution: