Python: 如何对水平线划过的字符进行OCR
Python: How to OCR characters crossed by a horizontal line
我有一批要扫描的图像。其中一些有一条横线穿过必须扫描的字符,看起来像这样:
我制作了一个可以去除水平线的程序:
import cv2
import numpy as np
img = cv2.imread('image.jpg',0)
# Applies threshold and inverts the image colors
(thresh, im_bw) = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
im_wb = (255-im_bw)
# Line parameters
minLineLength = 100
maxLineGap = 10
color = 255
size = 2
# Substracts the black line
lines = cv2.HoughLinesP(im_wb,1,np.pi/180,minLineLength,maxLineGap)[0]
for x1,y1,x2,y2 in lines:
cv2.line(img,(x1,y1),(x2,y2),color,size)
cv2.imshow('clean', img)
这个returns下图:
那么,你知道如何对这些有白线穿过的字符进行OCR吗?您会采用与上述方法不同的方法吗?
如有不明之处,请提问。谢谢。
按照@Rethunk 的建议,我做了以下事情:
# Line parameters
minLineLength = 100
maxLineGap = 10
color = 255
size = 1
# Substracts the black line
lines = cv2.HoughLinesP(im_wb,1,np.pi/180,minLineLength,maxLineGap)[0]
# Makes a list of the y's located at position x0 and x1
y0_list = []
y1_list = []
for x0,y0,x1,y1 in lines:
if x0 == 0:
y0_list.append(y0)
if x1 == im_wb.shape[1]:
y1_list.append(y1)
# Calculates line thickness and its half
thick = max(len(y0_list), len(y1_list))
hthick = int(thick/2)
# Initial and ending point of the full line
x0, x1, y0, y1 = (0, im_wb.shape[1], sum(y0_list)/len(y0_list), sum(y1_list)/len(y1_list))
# Iterates all x's and prints makes a vertical line with the desired thickness
# when the point is surrounded by white pixels
for x in range(x1):
y = int(x*(y1-y0)/x1) + y0
if im_wb[y+hthick+1, x] == 0 and im_wb[y-hthick-1, x] == 0:
cv2.line(img,(x,y-hthick),(x,y+hthick),colour,size)
cv2.imshow(clean', img)
因此,作为 HoughLinesP
函数 returns 水平线的起点和终点,我列出了起点和终点的 y
坐标图像的末尾,因此我能够知道完整的线方程(所以如果它倾斜也是有效的)并且我可以迭代它的所有点。对于每个点,如果它被白色像素包围,我将其删除。结果如下:
如果您有更好的想法请告诉!
我有一批要扫描的图像。其中一些有一条横线穿过必须扫描的字符,看起来像这样:
我制作了一个可以去除水平线的程序:
import cv2
import numpy as np
img = cv2.imread('image.jpg',0)
# Applies threshold and inverts the image colors
(thresh, im_bw) = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
im_wb = (255-im_bw)
# Line parameters
minLineLength = 100
maxLineGap = 10
color = 255
size = 2
# Substracts the black line
lines = cv2.HoughLinesP(im_wb,1,np.pi/180,minLineLength,maxLineGap)[0]
for x1,y1,x2,y2 in lines:
cv2.line(img,(x1,y1),(x2,y2),color,size)
cv2.imshow('clean', img)
这个returns下图:
那么,你知道如何对这些有白线穿过的字符进行OCR吗?您会采用与上述方法不同的方法吗?
如有不明之处,请提问。谢谢。
按照@Rethunk 的建议,我做了以下事情:
# Line parameters
minLineLength = 100
maxLineGap = 10
color = 255
size = 1
# Substracts the black line
lines = cv2.HoughLinesP(im_wb,1,np.pi/180,minLineLength,maxLineGap)[0]
# Makes a list of the y's located at position x0 and x1
y0_list = []
y1_list = []
for x0,y0,x1,y1 in lines:
if x0 == 0:
y0_list.append(y0)
if x1 == im_wb.shape[1]:
y1_list.append(y1)
# Calculates line thickness and its half
thick = max(len(y0_list), len(y1_list))
hthick = int(thick/2)
# Initial and ending point of the full line
x0, x1, y0, y1 = (0, im_wb.shape[1], sum(y0_list)/len(y0_list), sum(y1_list)/len(y1_list))
# Iterates all x's and prints makes a vertical line with the desired thickness
# when the point is surrounded by white pixels
for x in range(x1):
y = int(x*(y1-y0)/x1) + y0
if im_wb[y+hthick+1, x] == 0 and im_wb[y-hthick-1, x] == 0:
cv2.line(img,(x,y-hthick),(x,y+hthick),colour,size)
cv2.imshow(clean', img)
因此,作为 HoughLinesP
函数 returns 水平线的起点和终点,我列出了起点和终点的 y
坐标图像的末尾,因此我能够知道完整的线方程(所以如果它倾斜也是有效的)并且我可以迭代它的所有点。对于每个点,如果它被白色像素包围,我将其删除。结果如下:
如果您有更好的想法请告诉!