本文主要是介绍python 实现工业生成批号的旋转,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
问题提出:流水线的旋转批号 进行识别 ,识别的基础就是对图片进行旋转到上方,这样有助于ocR识别
面对这个问题,提出的思路是 提出一个正确的图案,使用sift匹配 输入图案与模版的特征图,生成匹配好的特征对,然后根据特征独取出坐标 ,对坐标进行欧式聚类计算,形成n个距离,使用方差来判定是否正确匹配
为了更好的处理特征对,对图片 形态学处理,找出字符的外接矩形框
最后截图 并保存,
下面给出部分主要代码:
1、图片输入 并计算sift特征
sift = cv2.xfeatures2d.SURF_create()kp1, des1 = sift.detectAndCompute(img1_gray, None)kp2, des2 = sift.detectAndCompute(imgRot, None)# BFmatcher with default parmsbf = cv2.BFMatcher(cv2.NORM_L2)matches = bf.knnMatch(des1, des2, k = 2) goodMatch = [] #利用sift算子 进行筛选匹配for m,n in matches:if m.distance < 0.6*n.distance:#可以调节的参数 特征点匹配约束 数字越小 效果越精确goodMatch.append(m)p1 = [kpp.queryIdx for kpp in goodMatch] #解析出 相似的一对 点的坐标p2 = [kpp.trainIdx for kpp in goodMatch] post1 = np.int32([kp1[pp].pt for pp in p1])# post2 = np.int32([kp2[pp].pt for pp in p2]) + (w1, 0)post2 = np.int32([kp2[pp].pt for pp in p2])list=[]for (x1, y1), (x2, y2) in zip(post1, post2):
# print(x1,y1,x2,y2)p1=np.array([x1,y1]) #利用相似对应点 距离相同的原理,利用距离方差大小判断 是否旋转到合理的角度p2=np.array([x2 ,y2 ])p3=p2-p1p4=math.hypot(p3[0],p3[1])list.append(p4)listvar=np.var(list)ave=listvar/len(list)
2、其中不断旋转图片,使其符合筛选需要
le=imgRot.shape[1]l1=int(le/2-150)l2=int(le/2+150)imgR=imgRot[l1:l2,l1:l2]gray = cv2.cvtColor(imgR, cv2.COLOR_BGR2GRAY)(_, thresh) = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY)closed = cv2.erode(thresh, None, iterations = 12)closed1=255-closed# find the contours in the thresholded image, then sort the contours# by their area, keeping only the largest onecnts = cv2.findContours(closed1.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)cnts = imutils.grab_contours(cnts)c = sorted(cnts, key = cv2.contourArea, reverse = True)[0]# compute the rotated bounding box of the largest contourrect = cv2.minAreaRect(c)box_origin = cv2.cv.BoxPoints(rect) if imutils.is_cv2() else cv2.boxPoints(rect)box1 = np.int0(box_origin)cv2.drawContours(imgR, [box1], -1, (0,255,0), 3)cv2.imshow("Image", imgR)cv2.waitKey(0)
3、最终裁剪图片下面把涉及到的函数贴一下
def imagecrop(image,box):xs = [x[1] for x in box]ys = [x[0] for x in box]cropimage = image[min(xs):max(xs),min(ys):max(ys)] cv2.imwrite('cropimage2.png',cropimage)return cropimage
def Nrotate(angle,valuex,valuey,pointx,pointy):angle = (angle/180)*math.pivaluex = np.array(valuex)valuey = np.array(valuey)nRotatex = (valuex-pointx)*math.cos(angle) - (valuey-pointy)*math.sin(angle) + pointxnRotatey = (valuex-pointx)*math.sin(angle) + (valuey-pointy)*math.cos(angle) + pointyreturn (nRotatex, nRotatey)
#顺时针旋转
def Srotate(angle,valuex,valuey,pointx,pointy):angle = (angle/180)*math.pivaluex = np.array(valuex)valuey = np.array(valuey)sRotatex = (valuex-pointx)*math.cos(angle) + (valuey-pointy)*math.sin(angle) + pointxsRotatey = (valuey-pointy)*math.cos(angle) - (valuex-pointx)*math.sin(angle) + pointyreturn (sRotatex,sRotatey)
#将四个点做映射
def rotatecordiate(angle,rectboxs,pointx,pointy):output = []for rectbox in rectboxs:if angle>0:output.append(Srotate(angle,rectbox[0],rectbox[1],pointx,pointy))else:output.append(Nrotate(-angle,rectbox[0],rectbox[1],pointx,pointy))return outputdef rotate_bound_white_bg(image, angle):# grab the dimensions of the image and then determine the# center(h, w) = image.shape[:2](cX, cY) = (w // 2, h // 2)# grab the rotation matrix (applying the negative of the# angle to rotate clockwise), then grab the sine and cosine# (i.e., the rotation components of the matrix)# -angle位置参数为角度参数负值表示顺时针旋转; 1.0位置参数scale是调整尺寸比例(图像缩放参数),建议0.75M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)cos = np.abs(M[0, 0])sin = np.abs(M[0, 1]) # compute the new bounding dimensions of the imagenW = int((h * sin) + (w * cos))nH = int((h * cos) + (w * sin)) # adjust the rotation matrix to take into account translationM[0, 2] += (nW / 2) - cXM[1, 2] += (nH / 2) - cY # borderValue 缺失背景填充色彩,此处为白色,可自定义return cv2.warpAffine(image, M, (nW, nH),borderValue=(0,0,0))# borderValue 缺省,默认是黑色(0, 0 , 0)# return cv2.warpAffine(image, M, (nW, nH))
如果需要可以下载我的完成程序,交流请联系哦
完整代码https://download.csdn.net/download/weixin_44576543/12594632
这篇关于python 实现工业生成批号的旋转的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!