本文實(shí)例為大家分享了opencv實(shí)現(xiàn)靜態(tài)手勢(shì)識(shí)別的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)是一家專業(yè)提供雁塔企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)、H5建站、小程序制作等業(yè)務(wù)。10年已為雁塔眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。要想運(yùn)行該代碼,請(qǐng)確保安裝了:python 2.7,opencv 2.4.9
效果如下:
算法如下:
把圖片先進(jìn)行處理,處理過(guò)程:
1.用膨脹圖像與腐蝕圖像相減的方法獲得輪廓。
2.用二值化獲得圖像
3. 反色
經(jīng)過(guò)如上的處理之后,圖片為:
這之后就簡(jiǎn)單了,設(shè)計(jì)一個(gè)辦法把三種圖像區(qū)分開(kāi)來(lái)即可。
代碼如下:
# -*- coding: cp936 -*- import cv2 import numpy import time import random import os def judge( ): #構(gòu)造一個(gè)3×3的結(jié)構(gòu)元素 # return 0 stone ,1 jiandao, 2 bu img = cv2.imread("wif.jpg",0) element = cv2.getStructuringElement(cv2.MORPH_RECT,(11,11)) dilate = cv2.dilate(img, element) erode = cv2.erode(img, element) #將兩幅圖像相減獲得邊,第一個(gè)參數(shù)是膨脹后的圖像,第二個(gè)參數(shù)是腐蝕后的圖像 result = cv2.absdiff(dilate,erode); #上面得到的結(jié)果是灰度圖,將其二值化以便更清楚的觀察結(jié)果 retval, result = cv2.threshold(result, 40, 255, cv2.THRESH_BINARY); #反色,即對(duì)二值圖每個(gè)像素取反 result = cv2.bitwise_not(result); result =cv2.medianBlur(result,23) a=[] posi =[] width =[] count = 0 area = 0 for i in range(result.shape[1]): for j in range(result.shape[0]): if(result[j][i]==0): area+=1 for 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+=1 width.append(0) posi.append(i) if(result[5*result.shape[0]/16][i]==0): width[count-1]+=1 """ print 'the pic width is ',result.shape[1],'\n' for i in range(count): print 'the ',i,'th',' ','is'; print 'width ',width[i] print 'posi ',posi[i],'\n' print count,'\n' print 'area is ',area,'\n' cv2.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) """ #判定時(shí)間 width_length=0 width_jiandao = True for i in range(count): if width[i]>45: #print 'bu1'; return 2; if width[i]<=20 or width[i]>=40: width_jiandao= False width_length += width[i] if width_jiandao==True and count==2: return 1; if(area <8500): #print 'shi tou'; return 0; print "width_leng",width_length if(width_length<35): #這個(gè)時(shí)候說(shuō)明照片是偏下的,所以需要重新測(cè)定。 a=[] posi =[] width =[] count = 0 for 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+=1 width.append(0) posi.append(i) if(result[11*result.shape[0]/16][i]==0): width[count-1]+=1 """ print 'the pic width is ',result.shape[1],'\n' for i in range(count): print 'the ',i,'th',' ','is'; print 'width ',width[i] print 'posi ',posi[i],'\n' print count,'\n' print 'area is ',area,'\n' """ width_length=0 width_jiandao = True for i in range(count): if width[i]>45: #print 'bu1'; return 2; if width[i]<=20 or width[i]>=40: width_jiandao= False width_length += width[i] if width_jiandao==True and count==2: return 1; if(area>14000 or count>=3): #print 'bu2'; return 2; if(width_length<110): #print 'jian dao'; return 1; else: #print 'bu3'; return 2; """ print("這是通過(guò)攝像頭來(lái)玩的剪刀石頭布的游戲,輸入y開(kāi)始\n") s = raw_input() capture = cv2.VideoCapture(0) cv2.namedWindow("camera",1) start_time = time.time() print("給你5秒的時(shí)間把手放到方框的位置\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): break if(cv2.waitKey(30)>=0): break ha,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 "fuck" """ def game(): fuck =[] fuck.append("石頭") fuck.append("剪刀") fuck.append("布") capture = cv2.VideoCapture(0) cv2.namedWindow("camera",1) start_time = time.time() print("給你5秒的時(shí)間把手放到方框的位置\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): break if(cv2.waitKey(30)>=0): break ha,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,'\n' print "你出的是",fuck[p1]," 電腦出的是",fuck[pc],"\n" cv2.destroyAllWindows() if(p1==pc): print "平局\n" return 0 if((p1==0 and pc ==1)or(p1==1 and pc ==2)or(p1==2 and pc ==0)): print '你贏了\n' return 1 else: print '你輸了\n' return -1 def main(): you_win=0 pc_win=0 print("這是通過(guò)攝像頭來(lái)玩的剪刀石頭布的游戲,請(qǐng)輸入回車開(kāi)始游戲\n") s = raw_input() while(1): print "比分(玩家:電腦) ",you_win,":",pc_win,'\n' s = raw_input() os.system('cls') ans =game() if(ans == 1): you_win+=1 elif(ans == -1): pc_win+=1 print "為了減少誤判,請(qǐng)盡可能將手占據(jù)盡可能大的框框" main()
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
標(biāo)題名稱:opencv實(shí)現(xiàn)靜態(tài)手勢(shì)識(shí)別opencv實(shí)現(xiàn)剪刀石頭布游戲-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://www.rwnh.cn/article36/ccegsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、企業(yè)網(wǎng)站制作、品牌網(wǎng)站設(shè)計(jì)、Google、企業(yè)建站、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容