【发布时间】:2018-06-19 07:05:26
【问题描述】:
我正在尝试使用zbar 从以下文档中识别/检测条形码。这是我从tutorial 使用的代码,用于根据我拥有的数据测试库。
from __future__ import print_function
import pyzbar.pyzbar as pyzbar
import numpy as np
import cv2
import imutils
import argparse
def decode(im):
# Find barcodes and QR codes
decodedObjects = pyzbar.decode(im)
# Print results
for obj in decodedObjects:
print('Type : ', obj.type)
print('Data : ', obj.data, '\n')
return decodedObjects # Display barcode and QR code location
def display(im, decodedObjects):
# Loop over all decoded objects
for decodedObject in decodedObjects:
points = decodedObject.polygon
# If the points do not form a quad, find convex hull
if len(points) > 4:
hull = cv2.convexHull(np.array([point for point in points], dtype=np.float32))
hull = list(map(tuple, np.squeeze(hull)))
else:
hull = points
# Number of points in the convex hull
n = len(hull)
# Draw the convext hull
for j in range(0, n):
cv2.line(im, hull[j], hull[(j + 1) % n], (0, 255, 0), 50) # Display results
cv2.imshow("Results", imutils.resize(im, 500))
cv2.waitKey(0) # Main
def display_ppn(im, decoded_objects, draw='rect'):
if draw == 'rect':
all_barcodes = []
for decoded_object in decoded_objects:
points = [[x, y] for x, y in (decoded_object.polygon)]
all_barcodes.append(points)
print(all_barcodes)
else:
all_barcodes = []
for decoded_object in decoded_objects:
points = [[x, y] for x, y in (decoded_object.polygon)]
all_barcodes.append(points)
print(all_barcodes)
for barcode in all_barcodes:
cv2.polylines(im, [np.array(barcode)], True, (0, 255, 0), 3)
cv2.imshow("Results", imutils.resize(im, 500))
cv2.waitKey(0)
if __name__ == '__main__':
# Creates parser
parser = argparse.ArgumentParser()
# parser arguments:
parser.add_argument('image', type=str, help='Path to image of form')
args = parser.parse_args()
# Read image
im = cv2.imread(args.image)
decodedObjects = decode(im)
display_ppn(im, decodedObjects)
虽然有些文档可以正常工作,但大多数文档都不能正常工作。有人可以帮助我了解为什么会发生这种情况以及如何获得 100% 检测?增加条形码大小或类型会有帮助吗?我拥有的输入图像将始终被二值化。
工作示例
失败的样本
【问题讨论】:
-
如果你想看一张照片,你总是得看正确的角度。否则,您将失去深度的大小。清晰度在分色中非常重要。
-
@dsgdfg 你是在建议我旋转图像以使条形码是直的吗?
标签: python barcode barcode-scanner zbar