100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > python opencv 条形码及二维码检测识别

python opencv 条形码及二维码检测识别

时间:2023-10-16 16:30:02

相关推荐

python opencv 条形码及二维码检测识别

目录

条形码检测识别二维码检测识别

基于python opencv pyzbar 实现。

条形码检测识别

原图: 最后截取图: 直接上代码:

import cv2import numpy as npimport mathfrom pyzbar.pyzbar import decode#读图img=cv2.imread(r'XX.png',1) #读取图片gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #将图片变为灰度图片#使用Sobel算子,求得水平和垂直方向灰度图像的梯度差gradX = cv2.Sobel(gray,ddepth = cv2.CV_32F,dx = 1,dy = 0,ksize = -1)gradY = cv2.Sobel(gray,ddepth = cv2.CV_32F,dx = 0,dy = 1,ksize = -1)gradient = cv2.subtract(gradX, gradY)gradient = cv2.convertScaleAbs(gradient)images = cv2.resize(gradient,(480,640))cv2.imshow("closed1", images)cv2.waitKey(0)#均值滤波,消除高频噪声 (8*8)像素块blurred = cv2.blur(gradient,(8,8))#二值化ret,thresh = cv2.threshold(blurred, 225, 255, cv2.THRESH_BINARY)#闭运算 填充条码空隙 (参数自己调整)kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(25,25))closed1 = cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel, iterations = 2)# images = cv2.resize(closed1,(480,640))# cv2.imshow("closed1", images)# cv2.waitKey(0)#4次腐蚀 再4次膨胀:消除小斑点closed2 = cv2.erode(closed1, None, iterations = 4)closed3 = cv2.dilate(closed2, None, iterations = 4)# images = cv2.resize(closed3,(480,640))# cv2.imshow("closed3", images)# cv2.waitKey(0)#开运算ret,th2=cv2.threshold(closed3,0.1,255,cv2.THRESH_BINARY)kernel = np.ones((10,10),np.uint8)opening = cv2.morphologyEx(th2, cv2.MORPH_OPEN, kernel,iterations = 2)images = cv2.resize(opening,(480,640))cv2.imshow("contours", images)cv2.waitKey(0)#腐蚀kernel = np.ones((5,5),np.uint8)erosion = cv2.erode(opening,kernel,iterations = 2)opening = cv2.resize(erosion ,(480,640))cv2.imshow("contours",opening )cv2.waitKey(0)#找出边界contours, hierarchy = cv2.findContours(erosion.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)#获取最大轮廓c = sorted(contours, key = cv2.contourArea, reverse = True)[0]#图形旋转矫正 并截取最大边界区域#修改最小框大小 放大50rect = cv2.minAreaRect(c)b = list(rect)b[1] = list(b[1])b[1][0] = b[1][0] + 50b[1][1] = b[1][1] + 50new_rect = tuple(b)box = np.int0(cv2.boxPoints(new_rect)) #draw_img = cv2.drawContours(img.copy(), [box], -1, (0, 0, 255), 3)# 获取画框宽高(x=orignal_W,y=orignal_H)orignal_W = math.ceil(np.sqrt((box[3][1] - box[2][1])**2 + (box[3][0] - box[2][0])**2))orignal_H= math.ceil(np.sqrt((box[3][1] - box[0][1])**2 + (box[3][0] - box[0][0])**2))# 原图中的四个顶点,与变换矩阵pts1 = np.float32([box[0], box[1], box[2], box[3]])pts2 = np.float32([[int(orignal_W+1),int(orignal_H+1)], [0, int(orignal_H+1)], [0, 0], [int(orignal_W+1), 0]])# 生成透视变换矩阵;进行透视变换M = cv2.getPerspectiveTransform(pts1, pts2)result_img = cv2.warpPerspective(img, M*1.5, (int(orignal_W+20),int(orignal_H+20)))cv2.imshow("JIAOZHEN",result_img)cv2.waitKey(0)#二值化 筛掉一些错误ret,thresh = cv2.threshold(result_img,128, 255, cv2.THRESH_BINARY)#二维码编码检测barcodes = decode(thresh)result = []for barcode in barcodes:barcodeData = barcode.data.decode("utf-8")result.append(barcodeData)print(result)

最后会输出条码:

['6936281406719']

注:pyzbar模块可以识别水平竖直方向的条码,上述图像经过透视变换后基本都为竖直方向或者水平方向可以被识别出来,但是大角度的图像imshow效果不是很好,可能是倒着的。。。

二维码检测识别

代码同上

过程截图(这里测试一个移动行程码)

输出结果

['/activity/epidemicQuery/index.html']

注:对应带有一定角度的图像,识别效果可能不是很好,需要后续加一些图形操作来矫正图像。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。