我目前正在做一些实验,以使用python更改图像上的肤色。当我尝试通过减去或求和某些值来修改饱和度和值通道时,遇到了问题。请查看此图片以了解我的意思
这是我尝试增加饱和度同时减小其值的结果。轮廓上出现一些奇怪的黑色和红色像素
这是另一个结果,但是我切换了修改以降低饱和度,同时增加值。某些图像会出现类似此结果的红色出血
这是我用来修改图像的代码。基本上我只提取图像的RGB值并将其更改为HSV值,然后按元素方式直接减去/汇总通道
for img in list_im:
ori_image = cv2.imread(img)
image = cv2.cvtColor(ori_image,cv2.COLOR_BGR2HSV_FULL)
h=image[:,:,0].copy()
hues=np.round(h/255*360)
sat=image[:,:,1].copy()
saturation=sat.flatten()
val=image[:,:,2].copy()
value=value.flatten()
#get hue channel only
#create bin interval (0,1),(1,2),...
binEdges = np.arange(0,361)
satBin = np.arange(1,255)
valBin = np.arange(0,254)
row,col=hues.shape
hues= hues.reshape(row*col)
#experiment negrofication
sat[sat>0]+=60
val[val<255]-=100
im=np.dstack((h,sat,val))
im=cv2.cvtColor(im,cv2.COLOR_HSV2RGB_FULL)
rainbow = plt.get_cmap('hsv')
fig = plt.figure()
fig.set_size_inches(18.5, 10.5, forward=True)
plot = plt.scatter(binEdges, binEdges, c = binEdges, cmap = 'hsv')
plt.clf()
a = fig.add_subplot(5, 1, 1)
imgplot = plt.imshow(cv2.cvtColor(ori_image,cv2.COLOR_BGR2RGB))
a.set_title('original')
plt.subplots_adjust(top=2)
e = fig.add_subplot(5,1,2)
plt.imshow(im)
e.set_title('rework')
plt.savefig(os.path.join('/home/cgal/color_harmonization/', 'Result_saturation', os.path.basename(img)),bbox_inches="tight")
plt.show()
这是我用来创建遮罩的代码,以防有人怀疑。因此,那些下巴,眼睛和嘴巴就是图像上的位置。得到面具后,将其乘以原始图像。
def getmask(img,jawline,eyes,mouth):
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
imArray = np.asarray(img)
# create mask
polygon = jawline.flatten().tolist()
maskIm = Image.new('L', (imArray.shape[1], imArray.shape[0]), 0)
ImageDraw.Draw(maskIm).polygon(polygon, fill='white')
#ImageDraw.Draw(maskIm).polygon(polygon, outline=(1))
# draw eyes
righteyes=eyes[0:6].flatten().tolist()
ImageDraw.Draw(maskIm).polygon(righteyes, fill='black')
lefteyes=eyes[6:].flatten().tolist()
ImageDraw.Draw(maskIm).polygon(lefteyes, fill='black')
# draw mouth
mouth=mouth.flatten().tolist()
ImageDraw.Draw(maskIm,).polygon(mouth, fill='black')
mask = np.array(maskIm)
return mask
因此,我希望找到一种方法来修改饱和度和值通道,而不会在上图显示的轮廓上出现红色斑点或意外的黑色像素的任何副作用。欢迎使用任何新技术或建议来修复我的代码。谢谢