首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >去除历史文档中的噪声和染色以进行OCR识别

去除历史文档中的噪声和染色以进行OCR识别
EN

Stack Overflow用户
提问于 2020-01-23 21:22:36
回答 1查看 598关注 0票数 2

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

这些文档的染色效果就像整个文档中的小点一样,会影响OCR和手写识别。除了OpenCV的图像去噪之外,有没有更有效的方法来清理这些图像?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-24 05:47:49

一种可能的方法是自适应阈值,执行一些形态学操作,并使用纵横比+轮廓区域滤波来消除噪声。从这里,我们可以对生成的蒙版和输入图像进行逐位与运算,以获得一个干净的图像。结果如下:

因为您没有指定语言,所以我用Python实现了它

代码语言:javascript
运行
复制
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()
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59879611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档