0

I am using pylibdmtx.pylibdmtx to read a data matrix image, I am able to successfully read the image but in the result i am getting in decoded format like: [Decoded(data=b'05251255541/001430/HS21CS ', rect=Rect(left=193, top=138, width=280, height=277))]. Can someone help me to extract text from this format?

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)```
  • It would probably just be `msg.data` to access the text. The pylibdmtx documentation will tell you what attributes the returned object has. – jasonharper Mar 03 '21 at 14:42

1 Answers1

0

Here is example code to extract the decode list elements:

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)

Which prints out the following for an image with two sample datamatrix codes:

[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
BT99
  • 1