当前位置:首页 > 消费电子 > 触控感测
[导读] 要想运行该代码,请确保安装了:python 2.7,opencv 2.4.9 效果如下: 算法如下:

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

效果如下:

算法如下:

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

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

2.用二值化获得图像

3. 反色

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

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

代码如下:

# -*- coding: cp936 -*- import cv2 import numpy import TIme import random import os def judge( ): #构造一个3&TImes;3的结构元素 # 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) #将两幅图像相减获得边,第一个参数是膨胀后的图像,第二个参数是腐蚀后的图像 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 = 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],' ' for i in range(count): print 'the ',i,'th',' ','is'; print 'width ',width[i] print 'posi ',posi[i],' ' print count,' ' print 'area is ',area,' ' 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) """ #判定时间 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): #这个时候说明照片是偏下的,所以需要重新测定。 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],' ' for i in range(count): print 'the ',i,'th',' ','is'; print 'width ',width[i] print 'posi ',posi[i],' ' print count,' ' print 'area is ',area,' ' """ 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("这是通过摄像头来玩的剪刀石头布的游戏,输入y开始 ") s = raw_input() capture = cv2.VideoCapture(0) cv2.namedWindow("camera",1) start_TIme = TIme.time() print("给你5秒的时间把手放到方框的位置 ") 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秒的时间把手放到方框的位置 ") 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,' ' print "你出的是",fuck[p1]," 电脑出的是",fuck[pc]," " cv2.destroyAllWindows() if(p1==pc): print "平局 " return 0 if((p1==0 and pc ==1)or(p1==1 and pc ==2)or(p1==2 and pc ==0)): print '你赢了 ' return 1 else: print '你输了 ' return -1 def main(): you_win=0 pc_win=0 print("这是通过摄像头来玩的剪刀石头布的游戏,请输入回车开始游戏 ") s = raw_input() while(1): print "比分(玩家:电脑) ",you_win,":",pc_win,' ' s = raw_input() os.system('cls') ans =game() if(ans == 1): you_win+=1 elif(ans == -1): pc_win+=1 print "为了减少误判,请尽可能将手占据尽可能大的框框" main()
本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

2023年12月21日 – 提供超丰富半导体和电子元器件™的业界知名新品引入 (NPI) 代理商贸泽电子 (Mouser Electronics) 即日起供货英飞凌的BGT60ATR24C XENSIV™ 60GHz雷达...

关键字: 雷达 模数转换器 手势识别

(全球TMT2022年11月14日讯)近日,思享无限旗下秀色直播及嗨秀直播入选"2021高成长企业TOP100"榜单。2021年度"高成长企业TOP100"活动通过对符合北京市政策支持方向的2269家非上市企业在营收成...

关键字: TOP P10 人脸识别 手势识别

摘 要 :针对传统神经网络需要人工对参数进行提取的问题,提出基于 Leap Motion 结合卷积神经网络的手势识别方法。首先利用 Leap Motion 获取高精度手势图像,然后对图像进行灰度处理,采用卷积神经网络算法...

关键字: 手势识别 高精度 Leap Motion 灰度处理 卷积神经网络 深度学习

摘 要:针对高质量、远距离无线鼠标的研究,设计出基于CC3200模块的WiFi无形态式鼠标,系统通过CC3200控制前端感知点,五个指端感知点分别包括不同的基础功能。采用IEEE802.11无线局域网通用标准进行控制类数...

关键字: CC3200 手势识别 WiFi通信 无形态鼠标 TCP

摘要:给出了采用ADXL335加速度传感器来采集五个手指和手背的加速度三轴信息,并通过ZigBee无线网络传输来提取手势特征量,同时利用BP神经网络算法进行误差分析来实现手势识别的设计方法。最后,通过Matlab验证,结...

关键字: Zigbee BP神经网络 手势识别 加速度传感器

摘 要:针对目前电子设备对手势识别功能的需求,提出了一种非接触式手势识别系统设计方案。该系统将红外线和环境光传感器Si1143与C8051F700、C8051F800等电容式触摸感应微控制器相结合,实现非接触式手势识别,...

关键字: 手势识别 非接触 红外 传感器

摘 要:本课题结合市场的实际需求,论述了一种基于STM32的智能手表系统的设计方案。本设计基于STM32硬件开发平台,通过移植 C/OS-III实时操作系统以及emWin图形软件库设计了手表的GUI界面。整个系统采用MP...

关键字: 可穿戴 嵌入式 计步器 蓝牙通信 手势识别

MLX 75026提供与VGA 3D ToF摄像头传感器IC MLX 75027相同的性能,但尺寸缩小一半

关键字: melexis 飞行时间传感器 手势识别

  北京时间2月20日早间消息,黑莓公司最近申请了一项专利,可以在不接触屏幕的情况下,通过在屏幕上方移动手指来选择照片。   该技术可以合成一组手机探测到的图像,以此判断用户的目标区域,

关键字: 黑莓 手势识别

  上海2013年3月28日电 -- Life Technologies CorporaTIon 今日宣布,其旗下美国应用生物系统公司的 3500xL Dx 系列基因分析仪已获中国国家食品药品监

关键字: 英特尔 手势识别 手势控制
关闭
关闭