[英]How can I change the color of the lip that got its landmarks without disturbing its texture? in opencv python
I have to write a make-up program for my university project.我必须为我的大学项目编写一个补课程序。 How can I change the color of the area I want without losing skin texture?
如何在不丢失皮肤纹理的情况下更改所需区域的颜色? I used Media Pipe to get landmarks cheeks and lip
我使用 Media Pipe 获取地标脸颊和嘴唇
Like this natural mode image!!!!!!!!!!!喜欢这个自然模式图像!!!!!!!!!!!
Here are the details for one way to do that in Python/OpenCV.以下是在 Python/OpenCV 中执行此操作的一种方法的详细信息。
Basically, we get the average color of the lips after creating a mask for the lips.基本上,我们在为嘴唇创建遮罩后得到嘴唇的平均颜色。 Then get the difference color (in each channel) between that and the desired color.
然后获得该颜色与所需颜色之间的差异颜色(在每个通道中)。 Then we add the difference to the input image.
然后我们将差异添加到输入图像。 Then we use the mask to combine the original and new image.
然后我们使用蒙版将原始图像和新图像组合起来。
Input:输入:
import cv2
import numpy as np
import skimage.exposure
# specify desired bgr color for lips and make into array
desired_color = (170, 130, 255)
desired_color = np.asarray(desired_color, dtype=np.float64)
# create swatch
swatch = np.full((200,200,3), desired_color, dtype=np.uint8)
# read image
img = cv2.imread("lady.jpg")
# threshold on lip color
lower = (0,0,200)
upper = (140,140,255)
mask = cv2.inRange(img, lower, upper)
# apply morphology open and close
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
# get average bgr color of lips
ave_color = cv2.mean(img, mask=mask)[:3]
print(ave_color)
# compute difference colors and make into an image the same size as input
diff_color = desired_color - ave_color
diff_color = np.full_like(img, diff_color, dtype=np.uint8)
# shift input image color
# cv2.add clips automatically
new_img = cv2.add(img, diff_color)
# antialias mask, convert to float in range 0 to 1 and make 3-channels
mask = cv2.GaussianBlur(mask, (0,0), sigmaX=3, sigmaY=3, borderType = cv2.BORDER_DEFAULT)
mask = skimage.exposure.rescale_intensity(mask, in_range=(128,255), out_range=(0,1)).astype(np.float32)
mask = cv2.merge([mask,mask,mask])
# combine img and new_img using mask
result = (img * (1 - mask) + new_img * mask)
result = result.clip(0,255).astype(np.uint8)
# save result
cv2.imwrite('lady_swatch.png', swatch)
cv2.imwrite('lady_mask.png', (255*mask).clip(0,255).astype(np.uint8))
cv2.imwrite('lady_recolor.jpg', result)
cv2.imshow('swatch', swatch)
cv2.imshow('mask', mask)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
Final Mask Image:最终蒙版图像:
New Color Swatch:新色样:
Result:结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.