串口DMPL指令刻字机自动识别图形轮廓并刻绘py
2017-08-20 16:31
1056 查看
#coding=utf-8 """ 作者:Joshua QQ:445621012 """ import cv2 import Image import numpy as np import serial import sys import math import os def getinfo(path): img = cv2.imread(path) gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #把图像转换成灰度图 ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) #hold the binary contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) #find edges del(contours[0]) #删除numpy数组中最外层的轮廓 #print contours cv2.drawContours(img,contours,-1,(13,52,163),5) #draw the contours #找到横坐标的最小值和最大值,为以后设定大小做准备 left=[] right=[] top=[] foot=[] for c in contours: pentagram = c leftmost = tuple(pentagram[:,0][pentagram[:,:,0].argmin()]) rightmost = tuple(pentagram[:,0][pentagram[:,:,0].argmax()]) topmost = tuple(pentagram[:,0][pentagram[:,:,1].argmin()]) footmost = tuple(pentagram[:,0][pentagram[:,:,1].argmax()]) l=leftmost[0] r=rightmost[0] t=topmost[0] f=footmost[0] left.append(l) right.append(r) top.append(t) foot.append(f) print left,"\n",right,"\n",top,"\n",foot leftm=min(left) rightm=max(right) topm=min(top) footm=max(foot) print '\npeak at:',leftm,rightm,topm,footm #画出矩形 (x, y, w, h) = cv2.boundingRect(contours[0]) cv2.rectangle(img,(leftm, y), (rightm, y + 2), (0, 255, 0), 1) print "hhhhhh" print (rightm-leftm) # print "width is:",(rightm-leftm) # print "size of the original imageis :",im.size cv2.imshow("img", img) cv2.waitKey(0) return (leftm,rightm,(rightm-leftm),topm,footm) def vecnzo(ob,size): image=cv2.imread(ob) info=getinfo(path) height,width=image.shape[:2] w=info[2] rate=float(255)/float(w) rate2=13.68*size/9 gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) #转换成灰度图 es=cv2.resize(gray,(int(width*rate*rate2),int(height*rate*rate2)),interpolation=cv2.INTER_CUBIC) ret, binary = cv2.threshold(es,127,255,cv2.THRESH_BINARY) #hold the binary[gray] contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) #查找边缘 del(contours[0]) #删除numpy数组中最外层的轮廓 perimeter = cv2.arcLength(contours[0],True) print "longth %s"%perimeter print type(contours[0]) file=open("1.txt","w") file.write(";:H A L0 ECN U V10") #初始化高精度绘图仪 for c in contours: file.write(" ") a=0 file.write(" U") for d in c: for tables in d: for item in tables: file.write(str(item)) a+=1 if(a==2): file.write(" D") else: file.write(",") tail=c[0][0] file.write(str(tail[0])) file.write(",") file.write(str(tail[1])) a=0 file.close() #grave() def grave(): file = open("1.txt","r") con = file.read() t = serial.Serial('com1',9600) n = t.write(con) path="1.jpg" print "get your image cut and put you image file here then name it 1.jpg\n" size=float(raw_input("input the size you want your grave be alright! :")) vecnzo(path,size) os.system("pause")由于家里开的印刷厂,近年改了方法,对于要求不非常细致的丝网印刷单子,就用刻字机刻黏纸,然后直接贴在版上,不用再感光什么的编程语言就是给懒人准备的由于接了单子还要用photoshop处理成对比度高的,细节清楚,高像素的位图,然后再用刻字机自带的软件识别现在什么都不用做了,只要准备好压缩图,不用位图,设定好大小,就刻出来了用到了python第三方库opencv numpy还有serial源代码在百度网盘http://pan.baidu.com/s/1eSmN8Gi没什么技术含量,但是用起来很方便,简化了很多人工步骤,只是还没想出方案实现一个功能,就是在轮廓数组中识别直线,矩形,以及设置精度舍弃一些多余的点,等大学学了高数再说吧嘿嘿
相关文章推荐
- 枚举所有串口,并自动识别硬件变动
- 使用VC6.0 自动识别可用串口 添加至combox 并打开。
- 自动识别串口号
- MFC下自动识别串口的实现
- 串口类的DateReceived事件---发送一次指令,就会自动触发此事件接收数据
- VC6.0 自动识别可用串口 添加至combox 并打开
- 自动识别串口号|打开COM9之后的串口|XXX.exe已停止工作|Qt textBrowser滚动条设置到底部
- VC6.0 自动识别可用串口 添加至combox 并打开
- wins下串口 的自动识别
- MFC自动识别串口
- 自动识别图形验证码
- 自动识别图形验证码
- 串口注册表的自动识别
- 图形验证码一定安全吗?自动识别图形验证码
- 自动识别图形验证码
- VC6.0 自动识别可用串口 添加至combox 并打开
- 自动识别图形验证码
- 自动识别图形验证码
- MyScript 手写识别数学公式、图形 自动计算
- 使用VC6.0 自动识别可用串口 添加至combox 并打开。