復現掃描全能王的增強銳化
- 2020 年 3 月 17 日
- 筆記
掃描全能王的增強銳化其實是自適應二值化的變體。
直接用 OpenCV 的函數會讓背景變花,因為背景是漸變的,直接拿均值當閾值的話,總有一些背景像素在閾值下面。所以需要將閾值乘以一個係數,比如 0.9,過濾掉所有背景。同時,因為文字的像素值很小,不受影響。
import numpy as np from scipy import signal def adaptive_thres(img, win=9, beta=0.9): if win % 2 == 0: win = win - 1 # 邊界的均值有點麻煩 # 這裡分別計算和和鄰居數再相除 kern = np.ones([win, win]) sums = signal.correlate2d(img, kern, 'same') cnts = signal.correlate2d(np.ones_like(img), kern, 'same') means = sums // cnts # 如果直接採用均值作為閾值,背景會變花 # 但是相鄰背景顏色相差不大 # 所以乘個係數把它們過濾掉 img = np.where(img < means * beta, 0, 255) return img

