简体   繁体   English

如何在不影响其纹理的情况下更改具有界标的嘴唇的颜色? 在 opencv python

[英]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!!!!!!!!!!!喜欢这个自然模式图像!!!!!!!!!!!

enter image description here在此处输入图像描述

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.

相关问题 我如何使用opencv python仅更改白色的黑色而无像素操作 - How can i change only black color in white using opencv python without pixel operation 如何更改 python 中的颜色但保留其阴影 - How to change the color in python but retain its shade Python:如何在更改属性时更改对象的实例? - Python: How can I change the instance of an object on change of its attribute? 如何用 Python 中的区域指定的颜色填充 openCV 轮廓? - How to fill openCV contours with a color specified by its area in Python? Python请求获取了原始HTML数据,但是如何从元素的ID中获取数据呢? - Python requests got the Raw HTML data but how can i get the data from element by its id? 如何通过在 Python 中使用 PIL 或 opencv 比较其坐标值,将图像粘贴到另一个图像? - How can I paste an image to another image by comparing its coordinate values using PIL or opencv in Python? 我已经安装了 OpenCV C++。 我可以在不重新安装库的情况下使用它在 Python 中的功能吗? - I already have OpenCV C++ installed. Can I use its functions in Python without reinstalling the library? 如何使条目中的部分文本变为粗体并更改其背景颜色? - How can I make part of the text in the Entry bold and change its background color? 如何在pygame中更改弹丸的位置而不改变其面对每个循环? - How can I change the location of a projectile in pygame without changing its facing every loop? 如何使用 python opencv 模糊图像中的红色,使其不清晰可见? - How to blur red color in image with python opencv so that its not clearly visible?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM