串口DMPL指令刻字机自动识别图形轮廓并刻绘py

来源:互联网 发布:电影制作软件 编辑:程序博客网 时间:2024/03/29 05:31
#coding=utf-8"""作者:JoshuaQQ:445621012"""import cv2import Imageimport numpy as npimport serialimport sysimport mathimport osdef 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 binarycontours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) #find edgesdel(contours[0])        #删除numpy数组中最外层的轮廓#print contourscv2.drawContours(img,contours,-1,(13,52,163),5)           #draw the contours#找到横坐标的最小值和最大值,为以后设定大小做准备left=[]right=[]top=[]foot=[]for c in contours:pentagram = cleftmost = 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",footleftm=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.sizecv2.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/9gray = 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"%perimeterprint type(contours[0])file=open("1.txt","w")file.write(";:H A L0 ECN U V10")  #初始化高精度绘图仪for c in contours:file.write(" ")a=0file.write(" U")for d in c:for tables in d:for item in tables:file.write(str(item))a+=1if(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=0file.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没什么技术含量,但是用起来很方便,简化了很多人工步骤,只是还没想出方案实现一个功能,就是在轮廓数组中识别直线,矩形,以及设置精度舍弃一些多余的点,等大学学了高数再说吧嘿嘿
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 三星s6进水黑屏怎么办 银行卡不支持快捷支付怎么办 路由器忘记管理员密码怎么办 云付没有推荐人怎么办 牛呗审核不通过怎么办 华硕笔记本很卡怎么办 淘宝换货没有货怎么办 用手机怎么办网银 手机销号支付宝怎么办 黑狗狗毛发红怎么办 裤子洗掉色了怎么办 没买快递收到怎么办 淘宝介入卖家不举证怎么办 买家拒收快递货怎么办 买家到付拒收怎么办 买家发顺丰到付又拒收怎么办 到付快递骗局怎么办 一年级学生上课走神怎么办 网购出现质量问题怎么办 小米分期没额度怎么办 小米预约错了怎么办 小米商城缺货要怎么办 LG显示器不满屏怎么办 网页页面放大了怎么办 JSP样式失效了怎么办? 客户故意不结账怎么办 面对刁难的顾客怎么办 万家乐燃气灶具不好打火怎么办 垃圾处理器堵了怎么办 银赫入伍金俊秀怎么办 孕妇被蜈蚣咬了怎么办 苹果id密码忘了怎么办 苹果6触屏失灵怎么办 阴道里长尖锐疣怎么办 毛笔笔尖泡弯了怎么办 长智齿了很疼怎么办 买彩票中大奖了怎么办 被小蜈蚣咬了怎么办 微信被盗刷钱了怎么办 眼结膜下出血该怎么办 金珠娜没有你该怎么办