【发布时间】:2015-03-06 12:34:15
【问题描述】:
我正在尝试制作肤色检测程序。基本上,它从网络摄像头获取视频,然后创建一个蒙版,之后只有皮肤可见。我在一篇论文中找到了a criterium for detecting skin-colour ranges。它看起来像这样:
均匀日光照射规则下的肤色定义为 (R > 95 ) AND (G > 40 ) AND (B > 20 ) AND (max{R, G, B} - min{R, G, B} 15) AND (|R - G| > 15 ) AND (R > G) AND (R > B) (1) 同时给出手电筒或日光侧照规则下的肤色 通过 (R > 220 ) AND (G > 210 ) AND (B > 170 ) AND (|R - G| B) 与 (G > B)
我在 Python 中所做的是:
def check(list):
return ( ( (list[2]>95) and (list[1]>40) and (list[0]>20)) and ((max(list)-min(list))>15)
and (abs(list[2]-list[1])>15) and (list[2]>list[1]) and (list[2]>list[0]))
def check2(list):
return (list[2]>220) and (list[1]>210) and (list[0]>170) and (abs(list[2]-list[1])<=15) and ((list[2]>list[0]) and (list[1]>list[0]))
(grabbed, frame) = camera.read()
img=frame
img=img.tolist()
skinmask = [[(1 if (check(list) or check2(list)) else 0) for list in l1] for l1 in img]
mask=np.array(skinmask, dtype = "uint8")
skin = cv2.bitwise_and(frame, frame, mask = mask)
cv2.imshow("images", np.hstack([frame, skin]))
但这不是我真正期望的。它减慢了这个过程。我找到了cv2.inRange(image, lower, upper),但它无法处理如此复杂的颜色范围规则。
还有其他更有效的方法吗?
【问题讨论】:
-
您可以尝试使用
all和any来代替您在检查功能中使用的ands 进行基准测试。但是代码很简单,没有太多吸引眼球的优化。您可以做的最大优化是摆脱skinmask。即便如此,我唯一能想到的就是将这些循环扔给 C/C++,因为遍历图像中的所有像素将花费一秒二(取决于图像大小)。 -
我选择了 Python(不是 Java、C++ 或其他)来实现更快的原型设计和开发过程。我真的很想知道,为什么像 OpneCV 这样有名的东西没有内置函数可以解决这个问题。也许还有其他工具或库具有与 OpenCV 类似的功能?
-
谢谢,我会考虑的。但是 'np.array.where' 将返回包含 only 处于条件下的元素的数组。但我需要某种过滤器。
标签: python opencv numpy computer-vision image-recognition