在对手写文本执行文本检测和识别之前,我尝试从多个笔记本页面中删除规则和背景笑脸。
安earlier thread提供了有用的提示,但我的问题在几个方面有所不同。
- 要保留的文本写在要删除的背景项目上。
- 要删除的项目与文本的颜色不同,这可能是删除的关键。
- 要删除的线条不是很直,笑脸更不直。
我正在考虑使用 OpenCV 来完成此任务,但我愿意使用 ImageMagick 或命令行 GIMP,只要我可以一次处理整个批处理。由于我以前从未使用过这些工具,因此欢迎任何建议。谢谢。
最佳答案
这是一个简单的方法,假设文本是蓝色的
- 将图像转换为 HSV 格式并使用
cv2.inRange()
颜色阈值 - 执行morphological transformations平滑图像
- 隔离字符
- 为 OCR/Tesseract 重新着色字符
我们首先将图像转换为 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)
现在我们执行形态变换以消除小噪声
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2,2))
close = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=1)
我们拥有所需的文本轮廓,因此我们可以通过使用原始图像进行 mask 来隔离字符
result[close==0] = (255,255,255)
最后,为了准备 OCR/Tesseract 的图像,我们将字符更改为黑色
retouch_mask = (result <= [250.,250.,250.]).all(axis=2)
result[retouch_mask] = [0,0,0]
完整代码
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/