簡體   English   中英

無法解碼 Aztec 條形碼

[英]Unable to decode Aztec barcode

我正在嘗試使用以下腳本解碼 Aztec 條形碼:

import zxing
reader = zxing.BarCodeReader()
barcode = reader.decode("test.png")
print(barcode)

這是輸入圖像:

在此處輸入圖像描述

以下是 output:

條碼(原始=無,解析=無,路徑='/Users/dhiwatdg/Desktop/test.png',格式=無,類型=無,點=無)

我確定它是有效的 Aztec 條形碼。 不是腳本無法解碼。

未檢測到條形碼,因為黑條周圍有強烈的“振鈴”偽像(可能是“散焦”問題的結果)。
我們可以應用二進制閾值來獲得黑色和白色之間的更高對比度。
找到正確的閾值是困難的......

為了找到正確的閾值,我們可以從cv2.THRESH_OTSU返回的自動閾值開始,並增加閾值直到檢測到條形碼。
(請注意,需要增加 [而不是減少] 閾值特定於上圖)。


筆記:

  • 建議的解決方案有點過擬合,而且效率不高。
    我試過的其他解決方案,比如銳化不起作用......

代碼示例:

import cv2
import zxing

reader = zxing.BarCodeReader()

img = cv2.imread('test.png', cv2.IMREAD_GRAYSCALE)  # Read the image as grayscale.

thresh, bw = cv2.threshold(img, 0, 255, cv2.THRESH_OTSU)  # Apply automatic binary thresholding.

while thresh < 245:
    print(f'thresh = {thresh}')
    cv2.imshow('bw', bw)  # Show bw image for testing
    cv2.waitKey(1000)  # Wait 1 second (for testing)
    cv2.imwrite('test1.png', bw)  # Save bw as input image to the barcode reader.
    barcode = reader.decode("test1.png", try_harder=True, possible_formats=['AZTEC'], pure_barcode=True)  # Try to read the barcode

    if barcode.type is not None:
        break  # Break the loop when barcode is detected.

    thresh += 10  # Increase the threshold in steps of 10
    thresh, bw = cv2.threshold(img, thresh, 255, cv2.THRESH_BINARY)  # Apply binary threshold

cv2.imwrite('bw.png', bw)  # Save bw for debugging

cv2.destroyAllWindows()
print(barcode)

thresh的最后一個值 = 164

最后一張bw圖:
在此處輸入圖像描述

@Rotem 的解決方案非常有效。 我還發現以下解決方案可以通過應用維納濾波器來實現。

import cv2
import zxing

img = cv2.imread("test.png")

dst = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)
cv2.imwrite("test_tmp.png", dst)

reader = zxing.BarCodeReader()
barcode = reader.decode("test_tmp.png")
print(barcode)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM