OpenCV:文本处理和噪声去除
OpenCV :Text Processing and Noise Removal
我想删除包含文本的图像的背景,使其成为白色背景的文本。
图片样本
到目前为止,我一直在尝试获取图像的 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 有一个非常好的想法,即使用内核来摆脱中央条纹。但是,如果您改用 cv2.morphologyEx
,则不会更改字母的粗细:
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)
输出:
然后,在执行 bitwise_not
(交换黑白)后,您应该使用 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)
输出:
我想删除包含文本的图像的背景,使其成为白色背景的文本。
图片样本
到目前为止,我一直在尝试获取图像的 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 有一个非常好的想法,即使用内核来摆脱中央条纹。但是,如果您改用 cv2.morphologyEx
,则不会更改字母的粗细:
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)
输出:
然后,在执行 bitwise_not
(交换黑白)后,您应该使用 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)
输出: