Python:如何OCR字符横线交叉 [英] Python: How to OCR characters crossed by a horizontal line
问题描述
我有一批要扫描的图像.他们中的一些人有一条横线,横过需要扫描的字符,如下所示:
I have a batch of images which I would like to scan. Some of them have got a horizontal line crossing the characters that have to be scanned, which would look like this:
我制作了一个能够删除水平线的程序:
I have made a program that is able to remove the 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)
这将返回以下图像:
那么,您是否知道如何对这些白线交叉的字符进行OCR?您会采取与上述方法不同的方法吗?
So, do you have any idea of how to make OCR to these characters that have the white line crossing them? Would you make a different approach than the one stated?
如果不清楚,请提出任何问题.谢谢.
Please ask any questions you have if something is not clear. Thank you.
推荐答案
按照@Rethunk建议,我执行了以下操作:
Following @Rethunk advice, I did the following:
# 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
函数返回水平线的起点和终点,因此我列出了图像开始和结尾处的点的y
坐标,因此我能够知道全线方程(因此如果它是倾斜的也是有效的),我可以迭代所有点.对于每个点,如果它被白色像素包围,则将其删除.结果如下:
So, as the HoughLinesP
function returns the initial and final point of horizontal lines, I made a list of the y
coordinates of the points that are in the begginning and end of the image and thus I am able to know the full line equation (so if it is inclined is valid as well) and I can iterate all its points. For each point, if it is surrounded by white pixels, I remove it. The outcome is the following:
如果您有更好的主意,请告诉我们!
If you have any better idea please tell!
这篇关于Python:如何OCR字符横线交叉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!