消除手写文字的周围线条和背景图形噪音

时间:2019-08-14 05:52:22

标签: python opencv image-processing computer-vision imagemagick

在尝试对手写文本进行文本检测和识别之前,我试图从多个笔记本页面中删除规则和背景笑脸。

enter image description here

earlier thread提供了有用的提示,但是我的问题在几个方面有所不同。

  1. 要保留的文本写在要删除的背景项目上。
  2. 要删除的项目与文本具有不同的颜色,这可能是删除它们的关键。
  3. 要删除的线条不是很直,笑脸更是如此。

我正在考虑将OpenCV用于此任务,但是只要我可以一次处理整个批处理,就可以使用ImageMagick或命令行GIMP。由于我以前从未使用过这些工具,因此欢迎提出任何建议。谢谢。

1 个答案:

答案 0 :(得分:2)

这是一种简单的方法,假设文本为蓝色


我们首先将图像转换为HSV格式,然后创建一个遮罩以隔离字符

image = cv2.imread('1.png')
result = image.copy()
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([21,0,0])
upper = np.array([179, 255, 209])
mask = cv2.inRange(image, lower, upper)

enter image description here

现在我们执行形态转换以去除小噪声

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2,2))
close = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=1)

enter image description here

我们具有所需的文本轮廓,因此我们可以通过用原始图像遮罩来隔离字符

result[close==0] = (255,255,255)

enter image description here

最后,为OCR / Tesseract准备图像,我们将字符更改为黑色

retouch_mask = (result <= [250.,250.,250.]).all(axis=2)
result[retouch_mask] = [0,0,0]

enter image description here

完整代码

import numpy as np
import cv2

image = cv2.imread('1.png')
result = image.copy()
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([21,0,0])
upper = np.array([179, 255, 209])
mask = cv2.inRange(image, lower, upper)

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2,2))
close = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=1)

result[close==0] = (255,255,255)

cv2.imshow('cleaned', result)

retouch_mask = (result <= [250.,250.,250.]).all(axis=2)
result[retouch_mask] = [0,0,0]

cv2.imshow('mask', mask)
cv2.imshow('close', close)
cv2.imshow('result', result)
cv2.waitKey()