我正在尝试使用 opencv 实现反投影算法来检测手。该算法由多个来源组成。我尝试了多种方法,例如形态学和在投影中添加背景减法以尝试获得更好的结果。我也在网上查看。但是,我继续得到下面的图片。有没有人对我可能做错了什么有建议?
-谢谢
这是我的代码,只有 backProjection:
import cv2
import numpy as np
#module for esc keyMap on my computer
import keyMappings as kM
#set up webcam
cap = cv2.VideoCapture(0)
cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 1000)
cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 600)
#read a picture of a hand from my desktop
Hand = cv2.imread('/home/lie/Desktop/handPic.jpg')
#convert HSV and calc Histogram of this Pic
hsvHand = cv2.cvtColor(Hand, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsvHand)
roihist = cv2.calcHist([hsvHand], [0,1], None, [180,256],[0,180,0,256])
cv2.normalize(roihist,roihist,0,255,cv2.NORM_MINMAX)
#while not pressing esc
while cv2.waitKey(30) != kM.esc:
#take pic convert HSV
_,frame = cap.read()
hsvt = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
#backproject
dst = cv2.calcBackProject([hsvt],[0,1],roihist,[0,180,0,256],1)
#filtering
disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
cv2.filter2D(dst,-1,disc,dst)
#threshold
ret,thresh = cv2.threshold(dst,50,255,0)
#find contours in thresholded pic
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
ci =0
max_area =0
if len(contours)!=0:
#find max contour
for i in range(len(contours)):
cnt = contours[i]
area = cv2.contourArea(cnt)
if(area>max_area):
max_area = area
ci =i
#create hull around contour
cnt = contours[ci]
hull = cv2.convexHull(cnt)
#Code to draw contours and show pic is ommited
这是用于帮助识别手的图像:
这是阈值图片:
这张照片显然没有太多的手和很多噪音。