Challenge
Help! A zebra escaped from its enclosure. But where is it now?
一张 PNG
图片(斑马),斑马身上的条纹实际上是一个一维条码(barcode),但条码的条纹"越狱"了——bars
没有延伸到应有的边界。
Solution
条码的黑色条纹只在斑马身上,没有延伸到上下边界("escaped from its
enclosure"),所以 zbarimg 等工具直接解码会失败。
第一步,提取斑马条纹区域。用 Python
垂直平均条纹区域,再按阈值二值化,模拟"补全"条纹的效果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| from PIL import Image import numpy as np
img = Image.open('zebra.png') arr = np.array(img)
region = arr[46:258, :, :3].mean(axis=2)
col_dark = np.mean(region < 128, axis=0) > 0.4
barcode = np.ones((100, len(col_dark) + 40), dtype=np.uint8) * 255 barcode[:, 20:-20] = (1 - col_dark.astype(np.uint8)) * 255
h, w = barcode.shape big = np.repeat(np.repeat(barcode, 4, axis=0), 2, axis=1) Image.fromarray(big).save('zebra_fixed.png')
|
然后用 zbarimg 解码:
1 2
| $ zbarimg -S*.enable zebra_fixed.png QR-Code:The answer is saFFari
|
关键词就是 saFFari(斑马跑到 Safari 上了 🦓)。
saFFari