新闻中心

EEPW首页 > 消费电子 > 设计应用 > 如何用Opencv实现静态手势识别玩石头剪刀布?

如何用Opencv实现静态手势识别玩石头剪刀布?

作者:时间:2017-10-22来源:网络收藏

要想运行该代码,请确保安装了:python 2.7, 2.4.9

本文引用地址:http://www.eepw.com.cn/article/201710/367547.htm

效果如下:

算法如下:

把图片先进行处理,处理过程:

1.用膨胀图像与腐蚀图像相减的方法获得轮廓。

2.用二值化获得图像

3. 反色

经过如上的处理之后,图片为:

这之后就简单了,设计一个办法把三种图像区分开来即可。

代码如下:

# -*- coding: cp936 -*-import cv2import numpyimport TImeimport randomimport osdef judge( ):#构造一个3&TImes;3的结构元素# return 0 stone ,1 jiandao, 2 buimg = cv2.imread(wif.jpg,0)element = cv2.getStructuringElement(cv2.MORPH_RECT,(11,11))dilate = cv2.dilate(img, element)erode = cv2.erode(img, element)#将两幅图像相减获得边,第一个参数是膨胀后的图像,第二个参数是腐蚀后的图像result = cv2.absdiff(dilate,erode);#上面得到的结果是灰度图,将其二值化以便更清楚的观察结果retval, result = cv2.threshold(result, 40, 255, cv2.THRESH_BINARY);#反色,即对二值图每个像素取反result = cv2.bitwise_not(result);result =cv2.medianBlur(result,23)a=[]posi =[]width =[]count = 0area = 0 for i in range(result.shape[1]):for j in range(result.shape[0]):if(result[j][i]==0):area+=1for i in range(result.shape[1]):if(result[5*result.shape[0]/16][i]==0 and result[5*result.shape[0]/16][i-1]!=0 ):count+=1width.append(0)posi.append(i)if(result[5*result.shape[0]/16][i]==0):width[count-1]+=1print the pic width is ,result.shape[1],\nfor i in range(count):print the ,i,th, ,is;print width ,width[i]print posi ,posi[i],\nprint count,\nprint area is ,area,\ncv2.line(result,(0,5*result.shape[0]/16),(214,5*result.shape[0]/16),(0,0,0))cv2.namedWindow(fcuk)cv2.imshow(fcuk,result)cv2.waitKey(0)#判定时间width_length=0width_jiandao = Truefor i in range(count):if width[i]>45:#print bu1;return 2;if width[i]=20 or width[i]>=40:width_jiandao= Falsewidth_length += width[i]if width_jiandao==True and count==2:return 1;if(area 8500):#print shi tou;return 0;print width_leng,width_lengthif(width_length35):#这个时候说明照片是偏下的,所以需要重新测定。a=[]posi =[]width =[]count = 0for i in range(result.shape[1]):if(result[11*result.shape[0]/16][i]==0 and result[11*result.shape[0]/16][i-1]!=0 ):count+=1width.append(0)posi.append(i)if(result[11*result.shape[0]/16][i]==0):width[count-1]+=1print the pic width is ,result.shape[1],\nfor i in range(count):print the ,i,th, ,is;print width ,width[i]print posi ,posi[i],\nprint count,\nprint area is ,area,\nwidth_length=0width_jiandao = Truefor i in range(count):if width[i]>45:#print bu1;return 2;if width[i]=20 or width[i]>=40:width_jiandao= Falsewidth_length += width[i]if width_jiandao==True and count==2:return 1;if(area>14000 or count>=3):#print bu2;return 2;if(width_length110):#print jian dao;return 1;else:#print bu3;return 2;print(这是通过摄像头来玩的剪刀石头布的游戏,输入y开始\n)s = raw_input()capture = cv2.VideoCapture(0)cv2.namedWindow(camera,1)start_TIme = TIme.time()print(给你5秒的时间把手放到方框的位置\n)while(s==y or s==Y):ha,img =capture.read()end_time = time.time()cv2.rectangle(img,(426,0),(640,250),(170,170,0))cv2.putText(img,str(int((5-(end_time- start_time)))), (100,100), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)cv2.imshow(camera,img)if(end_time-start_time>5):breakif(cv2.waitKey(30)>=0):breakha,img = capture.read()capture.release()cv2.imshow(camera,img)img = img[0:210,426:640]cv2.imwrite(wif.jpg,img)judge()  cv2.waitKey(0)print fuckdef game():fuck =[]fuck.append(石头)fuck.append(剪刀)fuck.append(布)capture = cv2.VideoCapture(0)cv2.namedWindow(camera,1)start_time = time.time()print(给你5秒的时间把手放到方框的位置\n)while(1):ha,img =capture.read()end_time = time.time()cv2.rectangle(img,(426,0),(640,250),(170,170,0))cv2.putText(img,str(int((5-(end_time- start_time)))), (100,100), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)cv2.imshow(camera,img)if(end_time-start_time>5):breakif(cv2.waitKey(30)>=0):breakha,img = capture.read()capture.release()cv2.imshow(camera,img)img = img[0:210,426:640]cv2.imwrite(wif.jpg,img)p1 = judge()pc = random.randint(0,2)#print p1, ,pc,\nprint 你出的是,fuck[p1], 电脑出的是,fuck[pc],\ncv2.destroyAllWindows()if(p1==pc):print 平局\nreturn 0if((p1==0 and pc ==1)or(p1==1 and pc ==2)or(p1==2 and pc ==0)):print 你赢了\nreturn 1else:print 你输了\nreturn -1def main():you_win=0pc_win=0print(这是通过摄像头来玩的剪刀石头布的游戏,请输入回车开始游戏\n)s = raw_input()while(1):print 比分(玩家:电脑) ,you_win,:,pc_win,\ns = raw_input()os.system(cls)ans =game()if(ans == 1):you_win+=1elif(ans == -1):pc_win+=1print 为了减少误判,请尽可能将手占据尽可能大的框框main()


关键词: opencv 手势识别

评论


相关推荐

技术专区

关闭