如何简单地做np.where不包括np.where

2024-05-23 21:15:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个程序来改变照片中皮肤的颜色

首先我得到一个皮肤面具,然后我把BGR图像转换成HSV。在遮罩中添加V通道值。像这样:

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)

但是一些原始的白色像素变成了黑色,我想也许V+skin_mask/255*50让像素超过255

因此,我尝试:

NV = np.where(skin_mask > 0, np.where(V + skin_mask / 255 * 50 > 255, 255, V + skin_mask / 255 * 50), V).astype(np.uint8)

这是工作。但是很难看

我想知道如何美化这篇文章,不要用np.where包括np.where。非常感谢


Tags: 程序npmask像素wherecv2hsv照片
2条回答

使用skin_mask作为掩码,而不是像skin_mask / 255 * 50那样应用算术,可能更为优雅

您可以使用cv2.add来解决它:

NV = V.copy()
cv2.add(NV, np.full_like(V, 50), NV, mask=skin_mask)

使用cv2.add优于NumPy算法的优点:

  • cv2.add支持mask参数(掩码元素值通常为0255
  • cv2.add将结果剪辑到uint8[0,255]的有效范围,而不会溢出

用于测试解决方案的代码I:

import numpy as np
import cv2

# Build sample skin_mask
skin_mask = np.zeros((100, 80), np.uint8)
skin_mask[30:70, 30:60] = 255

# Build sample V matrix
V = np.full_like(skin_mask, 60)
V[40:80, 40:80] = 220

# Sum using cv2.add
NV = V.copy()
cv2.add(NV, np.full_like(V, 50), NV, mask=skin_mask)

# Sum using NumPy (used as reference for testing).
refNV = np.where(skin_mask > 0, np.minimum(V + skin_mask / 255 * 50, 255), V).astype(np.uint8)  # Reference

if np.any(NV != refNV):
    print('There is a bug: NV != refNV')  # Should not enter here

# Show the images
cv2.imshow('skin_mask', skin_mask)
cv2.imshow('V', V)
cv2.imshow('NV', NV)
cv2.imshow('refNV', refNV)
cv2.waitKey()
cv2.destroyAllWindows()

您仍然需要检测溢出。稍微干净一点的方法是使用np.minimumnp.clip

brighter_V = np.minimum(V + skin_mask / 255 * 50, 255)
NV = np.where(skin_mask > 0, brighter_V, V).astype(np.uint8)

np.clip方法更为通用,以防值溢出到0以下:

brighter_V = np.clip(V + skin_mask / 255 * 50, 0, 255)
NV = np.where(skin_mask > 0, brighter_V, V).astype(np.uint8)

(分成两行只是我个人的编码偏好)

相关问题 更多 >