image - 去除历史文档中的噪音和污点以进行 OCR 识别
问题描述
嗨,我正在尝试从历史文件中清除尽可能多的噪音。
这些文档在整个文档中都有像小点一样的污点,影响 OCR 和手写识别。除了来自 OpenCV 的图像去噪之外,还有更有效的方法来清理这些图像吗?
解决方案
一种潜在的方法是自适应阈值,执行一些形态学操作,并使用纵横比 + 轮廓区域过滤去除噪声。从这里我们可以按位和生成的掩码和输入图像得到一个干净的图像。结果如下:
由于您没有指定语言,因此我用 Python 实现了它
import cv2
import numpy as np
# Load image, create blank mask, convert to grayscale, Gaussian blur
# then adaptive threshold to obtain a binary image
image = cv2.imread('1.jpg')
mask = np.zeros(image.shape, dtype=np.uint8)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (7,7), 0)
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,51,9)
# Create horizontal kernel then dilate to connect text contours
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,2))
dilate = cv2.dilate(thresh, kernel, iterations=2)
# Find contours and filter out noise using contour approximation and area filtering
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.04 * peri, True)
x,y,w,h = cv2.boundingRect(c)
area = w * h
ar = w / float(h)
if area > 1200 and area < 50000 and ar < 6:
cv2.drawContours(mask, [c], -1, (255,255,255), -1)
# Bitwise-and input image and mask to get result
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
result = cv2.bitwise_and(image, image, mask=mask)
result[mask==0] = (255,255,255) # Color background white
cv2.imshow('thresh', thresh)
cv2.imshow('mask', mask)
cv2.imshow('result', result)
cv2.waitKey()
推荐阅读
- javascript - 设置价格是选择选项等于
- javascript - HTML 在我的 JS 文件中的某个点之后不显示任何内容?
- c# - WPF如何在组合框选择上将焦点设置为整个数据网格
- python - 在 Python 上使用 if 语句和追加的问题
- html - html css tooltips on hover event 我可以制作特定的宽度吗?
- python - Python比较两个列表的变化
- javascript - 对于在 python 到 javascript 语言中
- vhdl - 接收 VHDL 代码错误:使用 GHDL 的对象子类型不是本地静态的
- java - 最小重量的背包
- javascript - Z-index 阻止了 gif 动画的运行 | jQuery CSS HTML JS