Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算机视觉:使用OpenCv和Python创建手部遮罩_Python_Opencv_Computer Vision - Fatal编程技术网

计算机视觉:使用OpenCv和Python创建手部遮罩

计算机视觉:使用OpenCv和Python创建手部遮罩,python,opencv,computer-vision,Python,Opencv,Computer Vision,我正在尝试用手制作面具(黑色背景,白色手) 这是第一张原始图像: 这是我的代码: hand = cv2.imread('hand.jpg') blur = cv2.GaussianBlur(hand, (3, 3), 0) hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV) mask2 = cv2.inRange(hsv, np.array([2, 0, 0]), np.array([20, 255, 255])) kernel = np.ones(

我正在尝试用手制作面具(黑色背景,白色手)

这是第一张原始图像:

这是我的代码:

hand = cv2.imread('hand.jpg')

blur = cv2.GaussianBlur(hand, (3, 3), 0)

hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)

mask2 = cv2.inRange(hsv, np.array([2, 0, 0]), np.array([20, 255, 255]))

kernel = np.ones((7, 7))
dilation = cv2.dilate(mask2, kernel, iterations=1)
erosion = cv2.erode(dilation, kernel, iterations=1)

filtered = cv2.GaussianBlur(erosion, (5, 5), 0)

ret, thresh = cv2.threshold(filtered, 90, 255, 0)

cv2.imshow('Threshold', thresh)
结果是:

但我需要有更好的结果,比如:

我该怎么办

[编辑] 具有不同背景的第二幅图像:

使用@Rotem代码的结果:

阿杰·库马尔。IIT德里掌纹图像数据库1.0版。2007年


您可以通过在红色通道上应用阈值来解决此问题

背景色为深蓝色和绿色,手部颜色明亮且倾向于红色,因此仅使用红色通道可能比转换为HSV效果更好

以下解决方案使用以下阶段:

  • 提取红色通道-将红色通道用作灰度图像
  • 使用自动选择的阈值应用二进制阈值(使用
    cv2.THRESH\u OTSU
    参数)
  • 使用“打开”形态学操作清除一些小点(噪音)。
    打开相当于应用腐蚀而不是扩张
  • 使用“闭合”形态学操作闭合小间隙(我选择了圆盘状遮罩)。
    闭合相当于应用扩张而不是侵蚀
代码如下:

import cv2

img = cv2.imread('hand.jpg')

# Extract red color channel (because the hand color is more red than the background).
gray = img[:, :, 2]

# Apply binary threshold using automatically selected threshold (using cv2.THRESH_OTSU parameter).
ret, thresh_gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

# Use "opening" morphological operation for clearing some small dots (noise)
thresh_gray = cv2.morphologyEx(thresh_gray, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3)))

# Use "closing" morphological operation for closing small gaps
thresh_gray = cv2.morphologyEx(thresh_gray, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9)))

# Display result:
cv2.imshow('thresh_gray', cv2.resize(thresh_gray, (thresh_gray.shape[1]//2, thresh_gray.shape[0]//2)))
cv2.waitKey(0)
cv2.destroyAllWindows()

结果:


我认为您可以通过找到手的轮廓,并将其接近顶点较少的多边形(但可能是过度拟合)来改进结果。

谢谢!好主意,但是当图片没有深色背景并且与手绘颜色相似时,我能做什么呢?请检查我的编辑:)你可以尝试一些颜色分割像谢谢你的链接。但是,当程序返回多个结果(例如K=10)时,如何确定哪个结果是正确的呢?我尝试过,但找不到真正有效的解决方案。也许将颜色分割与其他启发式方法相结合,比如质心的颜色、区域的连续性和其他想法。