您的位置:首页 > 其它

串口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没什么技术含量,但是用起来很方便,简化了很多人工步骤,只是还没想出方案实现一个功能,就是在轮廓数组中识别直线,矩形,以及设置精度舍弃一些多余的点,等大学学了高数再说吧嘿嘿
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: