从图像中选择数据矩阵并对其进行解码

2024-03-28 21:36:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有图片作为附件。我尝试使用下面的代码,它输出了大多数图像的正确值。但是,解码需要很长时间

import cv2
from pylibdmtx.pylibdmtx import decode
import ctypes  
from PIL import Image
decode(Image.open("1591106831_festo.jpg"))

我相信,如果我只选择包含数据矩阵的图像的特定部分,并将其输入到pylibdmtx库,可能会更准确、更快

但目前我还不知道如何使用数据矩阵选择图像的部分。你能帮帮我吗。谢谢

所附数据矩阵的预期输出为(91)4608


Tags: 数据代码from图像imageimport附件pil
1条回答
网友
1楼 · 发布于 2024-03-28 21:36:50

简单地使用^{}^{}怎么样

import cv2
from pylibdmtx.pylibdmtx import decode

image = cv2.imread("1591106831_festo.jpg")
h, w  = image.shape[:2]
decdd = decode((image[:, :, :1].tobytes(), w, h))
print(decdd)
# [Decoded(data=b'(91)4608', rect=Rect(left=650, top=522, width=-82, height=86))]

这可能比您目前手头的更快,因为我们1)提供了decode不可猜测的参数和2)do image[:, :, :1],这意味着只处理第一个BGR层,即蓝色层,显然足以处理条形码


为了进行比较,decode(Image.open("1591106831_festo.jpg"))返回
[Decoded(data=b'(91)4608', rect=Rect(left=650, top=522, width=-82, height=86))]

即完全相同的推论


您可以使用max_count参数限制要获取的条形码数量,
>>> decode(Image.open("1591106831_festo.jpg"), max_count=1)
[Decoded(data=b'(91)4608', rect=Rect(left=522, top=629, width=86, height=82))]

相关问题 更多 >