Search code examples
pythonopencvzxingdatamatrix

Decoding Datamatrix as digits using pylibdmtx


I am trying to decode the digits from a Datamatrix. This is my code

import cv2
from pylibdmtx.pylibdmtx import decode
a = decode(cv2.imread(dmtx.jpg'))
print(a)

Image to be decoded

The code runs without error. But it doesn't print anything, it gives me an empty matrix.

I couldn't understand the decode() function.

Can someone suggest me to decode as digits using pylibdmtx lib?


Solution

  • Try to threshold the input image.

    import numpy as np
    import cv2
    from pylibdmtx import pylibdmtx
    
    if __name__ == '__main__':
    
        image = cv2.imread('image.jpg', cv2.IMREAD_UNCHANGED);
    
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
        ret,thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    
        msg = pylibdmtx.decode(thresh)
        print(msg)