Pyzbar not decoding a clear barcode

138 Views Asked by At

I have an image with a fairly clear barcode. Online barcode readers get it without issue, as does the app on my phone, but I've not been able to decode it using Pyzbar.

Here's the unedited image: unedited image

This yields nothing:

    pyzbar.decode(cv2.imread(path, cv2.IMREAD_GREYSCALE)

I've tried a variety of gaussian blur and adaptive threshold settings but still get no results - here was my best attempt:

    img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    blur = cv2.GaussianBlur(img, (23, 23), 0)
    img_adaptive = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, blockSize=15, C=2)
    result = pyzbar.decode(img_adaptive)

The resulting pre-processed image is below, but but result still == []. Does anyone have any other suggestions that might improve success on this one? (I've also tried reducing the size of the image, rotating it)

pre-processed image

Updated 2023/09/15 10.23am BST: Following suggestions in the comments from @statemachine, I tried manually straightening the image (-10 degrees got it pretty close to straight) and the decode got a result with a quality of 84.

enter image description here

I took this a step further and tested scan results from a rotation angle of -20 to 0 degrees and found that -16 to -2 worked, giving a range of 14 degrees, or +/- 7 degrees from level.

I also tried rotating it so the image was vertically true and found the same pattern with successful decodes from -106 to -92 degrees.

So, the problem seems to be that pyzbar isn't picking out the code unless it's close to 0 or 90 degrees.

I looked at the suggestion to 'use a 4-point perspective rectification with OpenCV', but unfortunately this won't work as my aim is part of larger project to pull multiple barcodes out of a single image (I'm trying to log and categorise used automotive parts).

I've also tried 'zooming out' so the barcode is a smaller portion of the image, and cropping in so the barcode fills the image, but pyzbar still fails to read it unless it's close to level/vertical.

So, are there any ways of telling pyzbar to look for barcodes at all angles? thanks!

0

There are 0 best solutions below