Python OpenCV:文本处理和噪声消除

Python OpenCV:文本处理和噪声消除,python,opencv,opencv3.0,Python,Opencv,Opencv3.0,我想删除包含文本的图像的背景,使其成为白色背景上的文本 图像样本 到目前为止,我一直试图获得图像的HSV和上下边界,但我找不到可以去除所有背景效果的上下边界 至今使用的代码: import cv2 import numpy as np # Take each frame filename = 'img2.png' img = cv2.imread(filename, 1) # Convert BGR to HSV hsv = cv2.cvtColor(img, cv2.COLOR_BGR

我想删除包含文本的图像的背景,使其成为白色背景上的文本

图像样本

到目前为止,我一直试图获得图像的HSV和上下边界,但我找不到可以去除所有背景效果的上下边界

至今使用的代码:

import cv2
import numpy as np


# Take each frame
filename = 'img2.png'

img = cv2.imread(filename, 1)

# Convert BGR to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# define range of blue color in HSV
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])
# Threshold the HSV image to get only blue colors
image_final = cv2.inRange(hsv, lower_blue, upper_blue)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(img,img, mask= mask)
cv2.imshow('frame',img)
cv2.imwrite('mask.png',image_final)


cv2.waitKey(0)

有更好的方法吗?还是我必须组合多个上下边界才能达到我的目标?

您可以将图像读取为灰度并设置树状图:

import cv2

img = cv2.imread('img2.png', 0)     # 0 means grayscale
new_img = (img >= 230)*255          # 230 is the threshold, change as desired
cv2.imwrite('mask.png',new_img)
这会将左侧图片转换为右侧图片:

由于您的图片都有纯白色字母,您可能只需选择一个非常高的恒定阈值,0为黑色,255为白色,例如230

编辑 @Ishara Madhawa有一个很好的主意,就是用果仁去除中心条纹。但是,如果改用,则不会更改字母的厚度:

import cv2

img = cv2.imread('img2.png', 0)
new_img = ((img >= 230)*255).astype('uint8')
cv2.imwrite('mask.png',255-new_img)    # 255-... to get black on white

kernel = np.ones((5, 1), np.uint8)    
new_img2 = cv2.morphologyEx(new_img, cv2.MORPH_CLOSE, kernel)
cv2.imwrite('mask2.png',255-new_img2)

您可以将图像读取为灰度并设置树型:

import cv2

img = cv2.imread('img2.png', 0)     # 0 means grayscale
new_img = (img >= 230)*255          # 230 is the threshold, change as desired
cv2.imwrite('mask.png',new_img)
这会将左侧图片转换为右侧图片:

由于您的图片都有纯白色字母,您可能只需选择一个非常高的恒定阈值,0为黑色,255为白色,例如230

编辑 @Ishara Madhawa有一个很好的主意,就是用果仁去除中心条纹。但是,如果改用,则不会更改字母的厚度:

import cv2

img = cv2.imread('img2.png', 0)
new_img = ((img >= 230)*255).astype('uint8')
cv2.imwrite('mask.png',255-new_img)    # 255-... to get black on white

kernel = np.ones((5, 1), np.uint8)    
new_img2 = cv2.morphologyEx(new_img, cv2.MORPH_CLOSE, kernel)
cv2.imwrite('mask2.png',255-new_img2)

这个解决方案会解决你的问题

这是解决方案的完整代码:

import cv2
import numpy as np
image = cv2.imread('input.png',0)

retval, thresh_gray = cv2.threshold(image, thresh=200, maxval=255,type=cv2.THRESH_BINARY_INV)

cv2.bitwise_not(thresh_gray,thresh_gray)

kernel = np.ones((5, 1), np.uint8)
joined = cv2.dilate(thresh_gray, kernel, iterations=1)
cv2.imshow('joined', joined)
cv2.waitKey(0)
首先,您应该将图像读取为灰度

image = cv2.imread('input.png',0)
输出:

之后,您应该设置一个阈值,以消除背景噪声。在这种情况下,我设置了一个手动阈值200,以获得最优化的结果

retval, thresh_gray = cv2.threshold(image, thresh=200, maxval=255,type=cv2.THRESH_BINARY_INV)
输出:

然后,在执行不交换黑白的按位_之后,您应该使用一个5 x 1内核从中间连接分离的字符。字符中间的两条水平线将消失。

cv2.bitwise_not(thresh_gray,thresh_gray)
kernel = np.ones((5, 1), np.uint8)
joined = cv2.dilate(thresh_gray, kernel, iterations=1)
输出:


这个解决方案会解决你的问题

这是解决方案的完整代码:

import cv2
import numpy as np
image = cv2.imread('input.png',0)

retval, thresh_gray = cv2.threshold(image, thresh=200, maxval=255,type=cv2.THRESH_BINARY_INV)

cv2.bitwise_not(thresh_gray,thresh_gray)

kernel = np.ones((5, 1), np.uint8)
joined = cv2.dilate(thresh_gray, kernel, iterations=1)
cv2.imshow('joined', joined)
cv2.waitKey(0)
首先,您应该将图像读取为灰度

image = cv2.imread('input.png',0)
输出:

之后,您应该设置一个阈值,以消除背景噪声。在这种情况下,我设置了一个手动阈值200,以获得最优化的结果

retval, thresh_gray = cv2.threshold(image, thresh=200, maxval=255,type=cv2.THRESH_BINARY_INV)
输出:

然后,在执行不交换黑白的按位_之后,您应该使用一个5 x 1内核从中间连接分离的字符。字符中间的两条水平线将消失。

cv2.bitwise_not(thresh_gray,thresh_gray)
kernel = np.ones((5, 1), np.uint8)
joined = cv2.dilate(thresh_gray, kernel, iterations=1)
输出:


我可能只是将图像转换为灰度图像,然后在该通道上设置treshhold。设置颜色阈值以及如何将灰度转换为hsv。你能提供一个代码来说明这个想法吗?我可能只是把图像转换成灰度图像,然后在一个通道上设置一个树洞。设置哪种颜色的阈值以及如何将灰度转换成hsv。你能提供一个代码来说明这个想法吗?我在函数locateroi中加入这个错误=cv2.dims\u gray,kernel,iterations=1 cv2.error:OpenCV3.4.1/io/opencv/modules/core/src/matrix.cpp:760:error:-215 dims 0我加入这个错误=cv2.digms\u gray,kernel,迭代次数=1 cv2。错误:OpenCV3.4.1/io/opencv/modules/core/src/matrix。cpp:760:错误:函数locateROI中的215 dims 0