image - 去除历史文档中的噪声和污点以进行 OCR 识别

标签 image opencv image-processing ocr noise

您好,我正在尝试尽可能多地清除历史文档中的噪音。

这些文档在整个文档中都有像小点一样的污渍,并且正在影响 OCR 和手写识别。除了 OpenCV 的图像去噪之外,还有更有效的方法来清理此类图像吗?

enter image description here

最佳答案

一种潜在的方法是自适应阈值,执行一些形态学操作,并使用纵横比+轮廓区域过滤来去除噪声。从这里我们可以按位和生成的掩码和输入图像来获得干净的图像。结果如下:

enter image description here

因为你没有指定语言,我用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()

关于image - 去除历史文档中的噪声和污点以进行 OCR 识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59879611/

相关文章:

android - 带有水平 ScrollView 的图库

opencv - 在 opencv 中使用 cv::rect 提取 Roi

c++ - C++ 中具有 n 个控制点和 k 阶的 B 样条曲线

windows - 帧到视频 - Windows 8 中的图像处理(仅限 WinRT)

CSS object-fit 封面正在拉伸(stretch) Mac 上 Chrome 中的特定图像

android - 如何使用 "Share image using"共享 Intent 在 android 中共享图像?

python - 如何压缩从 url 获取的 PNG 图像而不更改其尺寸

android - 如何将字符串数组返回给java JNI

image - 将 GDIPlus::Bitmap 转换为 cv::Mat(OpenCV C++ 接口(interface))

python-3.x - Python 中的 3D Dicom 可视化