python - 从手写文本中去除周围的线条和背景图形噪音

标签 python opencv image-processing computer-vision imagemagick

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

enter image description here

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

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

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

最佳答案

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


我们首先将图像转换为 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

我们拥有所需的文本轮廓,因此我们可以通过使用原始图像进行 mask 来隔离字符

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()

关于python - 从手写文本中去除周围的线条和背景图形噪音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57488816/

相关文章:

python - 当我们在Django中使用opencv时如何处理请求

python - 在python中有效地将颜色转换为透明度

python - 套接字编程 readStream!.takeUnretainedValue() 需要无限时间

python - ImportError : numpy. core.multiarray 导入失败(cv2)

c++ - OpenCV findcontours 为每个圆返回 2 个轮廓

opencv - 在ubuntu安装上安装opencv3而不删除opencv2包

python - 有没有办法使用 openpyxl 或 xlsxwriter 保护工作簿?

python - 正确实现 Cherrypy 的 autoreload 模块的方法

c# - 如何避免 MinAreaRect 检测到的矩形旋转?

matlab - openCV 双三次 imresize 创建负值