从 pylibdmtx.pylibdmtx.decode 获取解码值

Posted

技术标签:

【中文标题】从 pylibdmtx.pylibdmtx.decode 获取解码值【英文标题】:Getting decoded value from pylibdmtx.pylibdmtx.decode 【发布时间】:2021-06-02 03:38:39 【问题描述】:

我正在使用 pylibdmtx.pylibdmtx 读取数据矩阵图像,我能够成功读取图像,但结果我得到了解码格式,例如:[Decoded(data=b'05251255541/001430/HS21CS',矩形=矩形(左=193,顶部=138,宽度=280,高度=277))]。有人可以帮我从这种格式中提取文本吗?

image = cv2.imread(image_name, cv2.IMREAD_UNCHANGED)
print(image)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
msg = decode(thresh)
print(msg)```

【问题讨论】:

访问文本可能只是msg.data。 pylibdmtx 文档会告诉你返回的对象有哪些属性。 【参考方案1】:

这里是提取解码列表元素的示例代码:

import cv2
from pylibdmtx.pylibdmtx import decode

img = cv2.imread('datamatrix_multiple.png')
decodedList = decode(img)

print(decodedList)
print(len(decodedList))

for i in range(len(decodedList)):
    print(decodedList[i].data)
    print(str(decodedList[i].data, "utf-8")) #removes the b'... formatting
    print(decodedList[i].rect.left)
    print(decodedList[i].rect.top)
    print(decodedList[i].rect.width)
    print(decodedList[i].rect.height)

为带有两个示例数据矩阵代码的图像打印以下内容:

[Decoded(data=b'Hello World', rect=Rect(left=64, top=43, width=285, height=150)), Decoded(data=b'12340000001234', rect=Rect(left=58, top=267, width=70, height=70))]
2
b'Hello World'
Hello World
64
43
285
150
b'12340000001234'
12340000001234
58
267
70
70

【讨论】:

以上是关于从 pylibdmtx.pylibdmtx.decode 获取解码值的主要内容,如果未能解决你的问题,请参考以下文章

从PRISM开始学WPFMVVMViewModel?

在 python 中,为啥从数组读取比从列表读取慢?

从图库中挑选或从相机捕获的高质量图像

从PRISM开始学WPFMVVMCommand?

从PRISM开始学WPFPrism?

mysql 主-主-从-从