【问题标题】:Python: How to OCR characters crossed by a horizontal linePython:如何通过水平线交叉 OCR 字符
【发布时间】:2017-04-30 04:56:39
【问题描述】:

我有一批图像要扫描。其中一些有一条横过要扫描的字符的横线,如下所示:

我做了一个可以去掉水平线的程序:

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 处理吗?您会采用与所述方法不同的方法吗?

如果有不清楚的地方,请提出任何问题。谢谢。

【问题讨论】:

  • 您是否尝试过编写一种算法,只删除它所跨越的字符笔划之外的黑线部分?我建议专注于这一点。一旦知道线条粗细(假设它具有一致的粗细),您可以检查线条上方和下方是否有黑色像素,如果上方和下方的像素都是白色的,则一次只删除一列。跨度>

标签: python opencv ocr


【解决方案1】:

按照@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 函数返回水平线的初始点和最终点时,我列出了图像开头和结尾的点的y 坐标,因此我能够知道全线方程(所以如果它倾斜也是有效的),我可以迭代它的所有点。对于每个点,如果它被白色像素包围,我将其删除。结果如下:

如果你有更好的想法,请告诉!

【讨论】:

  • x0, x1, y0, y1 = (0, im_wb.shape[1], sum(y0_list)/len(y0_list), sum(y1_list)/len(y1_list)) 上出现错误ZeroDivisionError: division by zero
  • @Silencer 知道为什么吗?
猜你喜欢
  • 2016-12-11
  • 2020-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-11
  • 2017-04-03
  • 1970-01-01
相关资源
最近更新 更多