如何从手写文本图像中去除背景噪音?

问题描述

enter image description here

我尝试了这些方法,但没有得到任何真正的改变。实际上,我正在尝试使用 Google Cloud Vision API 构建手写 OCR。请向我建议我可以为预处理步骤做些什么。

1.

image = cv2.fastNlMeansDenoisingColored(image,None,10,7,15)
    kernel  = np.ones((5,5),np.uint8)
    image   = cv2.dilate(image,kernel,iterations = 1) 
    kernel  = np.ones((5,np.uint8)
    image   = cv2.erode(image,iterations = 1)

解决方法

另一种方式是 HSV 滤色器。因为您使用的是蓝色笔,所以我们可以选择我们想要的颜色。示例代码:

import cv2
import numpy as np

image = cv2.imread('9rS31.jpg')
hsv = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
lower_green = np.array([100,43,20])
upper_green = np.array([130,255,255])
mask = cv2.inRange(hsv,lower_green,upper_green)
res = cv2.bitwise_and(image,image,mask=mask)

gray = cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)
ret,generator = cv2.threshold(gray,1,cv2.THRESH_BINARY)

cv2.imwrite("img.jpg",generator)

生成的图像: enter image description here

,

噪音包括教科书中的水平线。所以一种方法是使用

cv2.getStructuringElement

您可以在互联网上找到更多信息。示例代码:

import cv2

# Load image
image = cv2.imread('9rS31.jpg')
img=image.copy()

# Remove border
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(50,1))
temp2 = 255 - cv2.morphologyEx(image,cv2.MORPH_CLOSE,horizontal_kernel)
result = cv2.add(temp2,image)

# Convert to grayscale and Otsu's threshold
gray = cv2.cvtColor(result,cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray,(5,5),0)
_,thresh = cv2.threshold(gray,cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)

cv2.imwrite('img.jpg',thresh)
cv2.imshow('img',thresh)
cv2.waitKey()

生成的图像:enter image description here