简体   繁体   English

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

[英]Remove surrounding lines and background graphic noise from handwritten text

I am trying to remove rules and a background smiley face from multiple notebook pages before performing text detection and recognition on the handwritten text.在对手写文本执行文本检测和识别之前,我试图从多个笔记本页面中删除规则和背景笑脸。

在此处输入图片说明

An earlier thread offers helpful hints, but my problem is different in several respects. 较早的线程提供了有用的提示,但我的问题在几个方面有所不同。

  1. The text to keep is written over the background items to be removed.要保留的文本写在要删除的背景项目上。
  2. The items to be removed have distinct colors from that of the text, which may be the key to their removal.要删除的项目与文本的颜色不同,这可能是删除的关键。
  3. The lines to be removed are not very straight, and the smiley face even less so.要去除的线条不是很直,笑脸更不直。

I'm thinking of using OpenCV for this task, but I'm open to using ImageMagick or command-line GIMP so long as I can process the entire batch at once.我正在考虑使用 OpenCV 来完成这项任务,但我愿意使用 ImageMagick 或命令行 GIMP,只要我可以一次处理整个批次。 Since I have never used any of these tools before, any advice would be welcome.由于我以前从未使用过这些工具中的任何一个,因此欢迎提出任何建议。 Thank you.谢谢你。

Here's a simple approach with the assumption that the text is blue这是一个简单的方法,假设文本是蓝色的


We begin by converting the image to HSV format and create a mask to isolate the characters我们首先将图像转换为 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)

在此处输入图片说明

Now we perform morphological transformations to remove small noise现在我们执行形态变换以去除小噪声

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

在此处输入图片说明

We have the desired text outlines so we can isolate characters by masking with the original image我们有所需的文本轮廓,因此我们可以通过使用原始图像进行屏蔽来隔离字符

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

在此处输入图片说明

Finally to prepare the image for OCR/Tesseract, we change the characters to black最后为 OCR/Tesseract 准备图像,我们将字符更改为黑色

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

在此处输入图片说明

Full code完整代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM