我在这里有一张黑白图像,我正准备将其放入 OCR 中,即 Tesseract。然而,Tesseract 无法检测到任何噪声区域。
我在这里寻找什么样的解决方案来消除噪音?由于 Tesseract 无法识别它,我认为删除是最好的可行选择。
您可以使用TextCleaner,一个 ImageMagick 脚本来清理文本背景。
如果您正在寻找 python 代码,这里的代码将用于消除噪音
import cv2
import numpy as np
# load color image
im = cv2.imread('input.jpg')
# smooth the image with alternative closing and opening
# with an enlarging kernel
morph = im.copy()
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))
morph = cv2.morphologyEx(morph, cv2.MORPH_CLOSE, kernel)
morph = cv2.morphologyEx(morph, cv2.MORPH_OPEN, kernel)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
# take morphological gradient
gradient_image = cv2.morphologyEx(morph, cv2.MORPH_GRADIENT, kernel)
# split the gradient image into channels
image_channels = np.split(np.asarray(gradient_image), 3, axis=2)
channel_height, channel_width, _ = image_channels[0].shape
# apply Otsu threshold to each channel
for i in range(0, 3):
_, image_channels[i] = cv2.threshold(~image_channels[i], 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY)
image_channels[i] = np.reshape(image_channels[i], newshape=(channel_height, channel_width, 1))
# merge the channels
image_channels = np.concatenate((image_channels[0], image_channels[1], image_channels[2]), axis=2)
# save the denoised image
cv2.imwrite('output.jpg', image_channels)
如果您正在处理的图像是发票(或在白色背景上有大量文本),上述代码不会给出好的结果。为了在此类图像上获得良好的效果,请删除
gradient_image = cv2.morphologyEx(morph, cv2.MORPH_GRADIENT, kernel)
并将morph
obj 传递给 split 函数并删除~
for 循环内的符号