5

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?

Sugantharaja
  • 121
  • 1
  • 3
  • 14
  • Are you sure your data matrix image is correctly encoded? Use an online decoder to test the image for correctness. – zindarod Aug 04 '18 at 12:09
  • Actually, I'm capturing the DataMatrix from a product and doing Image processing. Yes, an Online decoder decodes correctly. But pylibdmtx doesn't. I have uploaded the Image also. Kindly Look into it. @zindarod – Sugantharaja Aug 04 '18 at 15:04

1 Answers1

9

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)
zindarod
  • 6,328
  • 3
  • 30
  • 58