[英]Need help decompressing zlib data stored in Aztec barcode (Deutsche Bahn Ticket)
[英]Unable to decode Aztec barcode
未檢測到條形碼,因為黑條周圍有強烈的“振鈴”偽像(可能是“散焦”問題的結果)。
我們可以應用二進制閾值來獲得黑色和白色之間的更高對比度。
找到正確的閾值是困難的......
為了找到正確的閾值,我們可以從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
。
@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.