使用pyzbar在python中读取模糊的条形码

问题描述

我一直在尝试使用python和pyzbar从图像中读取一些条形码。不幸的是,在几个约束下,图像是从几英尺远的地方拍摄的,我无法更近地移动或变焦相机。是否可以使用任何现有的Python库读取这种模糊的条形码?

enter image description here

到目前为止,我已经尝试了一些预处理,包括阈值化,锐化,应用垂直关闭滤镜和Wiener滤镜,但似乎没有任何帮助。我可能想问一个奇迹,但是如果您有任何建议,我将不胜感激。

代码(有注释的部分是我尝试过的没有成功的预处理方法):

import numpy as np
import cv2 as cv
from pyzbar import pyzbar

barcode_img = cv.imread('barcode_example.jpg',cv.IMREAD_GRAYSCALE)

# threshold
# (_,barcode_img) = cv.threshold(barcode_img,255,cv.THRESH_BINARY + cv.THRESH_OTSU)

# close
# barcode_img = cv.morphologyEx(barcode_img,cv.MORPH_CLOSE,#                               cv.getStructuringElement(cv.MORPH_RECT,(1,21)))

# sharpen
# barcode_img_blur = cv.GaussianBlur(barcode_img,(15,1),3)
# barcode_img = cv.addWeighted(barcode_img,1.5,barcode_img_blur,-0.5,0)

# wiener filter
# from skimage import img_as_float
# from skimage.restoration import wiener,unsupervised_wiener
# dim = 3
# psf = np.ones((dim,dim)) / dim ** 2
# barcode_img = wiener(barcode_img,psf,1.0,clip=False)

barcodes = pyzbar.decode(barcode_img)
print(barcodes)

解决方法

  • 这不是最佳解决方案,如果您的想法有所改善,请与我们分享,以便大家学习。
  • 因此,根据一些统计信息,知道它是一个条形码,所以有一个垂直黑色区域代表每个条形码,所以我对行进行了汇总,并根据接近和的平均值的经验值对整个图像进行阈值处理,我通过以下方式解决了该问题:
#========================
# Import Libraies
#========================
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt 
from pyzbar import pyzbar

#------------------------
# Read Image
#========================
img = cv.imread('barcode_example.jpg',cv.IMREAD_GRAYSCALE)

# #------------------------
# # Morphology
# #========================
# # Closing
# #------------------------
closed = cv.morphologyEx(img,cv.MORPH_CLOSE,cv.getStructuringElement(cv.MORPH_RECT,(1,21)))

# #------------------------
# # Statistics
# #========================
print(img.shape)
dens = np.sum(img,axis=0)
mean = np.mean(dens)
print(mean)

#------------------------
# Thresholding
#========================
thresh = closed.copy()
for idx,val in enumerate(dens):
    if val< 10800:
        thresh[:,idx] = 0

(_,thresh2) = cv.threshold(thresh,128,255,cv.THRESH_BINARY + cv.THRESH_OTSU)

#------------------------
# plotting the results
#========================
plt.figure(num='barcode')

plt.subplot(221)
plt.imshow(img,cmap='gray')
plt.title('Original')
plt.axis('off')

plt.subplot(224)
plt.imshow(thresh,cmap='gray')
plt.title('Thresholded')
plt.axis('off')

plt.subplot(223)
plt.imshow(thresh2,cmap='gray')
plt.title('Result')
plt.axis('off')

plt.subplot(222)
plt.hist(dens)
plt.axvline(dens.mean(),color='k',linestyle='dashed',linewidth=1)
plt.title('dens hist')

plt.show()

#------------------------
# Printing the Output
#========================
barcodes = pyzbar.decode(thresh2)
print(barcodes)

solution

输出为:[Decoded(data=b'00004980072868003004',type='CODE128',rect=Rect(left=34,top=0,width=526,height=99),polygon=[Point(x=34,y=1),Point(x=34,y=99),Point(x=560,y=98),y=0)])]

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...