我在这里有黑/白图像,我准备准备放入OCR,即Tesseract。然而,Tesseract无法检测到任何噪音区域。
我在寻找什么样的解决方案来消除噪音?由于Tesseract无法识别它,我认为删除是最好的选择。
答案 0 :(得分:2)
您可以使用TextCleaner,一个ImageMagick脚本来清理文本背景。
答案 1 :(得分:0)
如果你正在寻找一个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循环中的~
符号