2016-12-14 11 views
2

私はスキャンしたい画像のバッチを持っています。私は水平線を削除することができ、プログラムを作ったPython:OCR文字を横線で横切る方法

Raw Image

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) 
そのうちのいくつかは、次のようになり、スキャンする必要がある文字を横切る水平線を、持っていますこれは下の画像を返す

:だから

Clean Image

は、あなたがどのように任意のアイデアを持っています白い線を横切るこれらの文字にOCRを適用するには?あなたが述べたものとは異なるアプローチをしますか?

ご不明な点がございましたら、ご質問ください。ありがとうございました。 @Rethunkアドバイスに従い

+2

交差する文字ストローク外の黒線の部分だけを削除するアルゴリズムを作成しようとしましたか?私はそれに焦点を当てることをお勧めします。線の太さがわかれば(線の太さが一定であると仮定して)、線の上下に黒いピクセルがあるかどうかを確認し、上下のピクセルが白い場合は一度に1つの線のみを削除します。 – Rethunk

答えて

1

は、私は次のようでした:

だから、
# 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座標のリストを作りました画像の始まりと終わりで、したがって、私は完全な線方程式を知ることができます(傾けられていれば有効です)、すべての点を繰り返すことができます。各点について、白いピクセルで囲まれている場合、それを削除します。

enter image description here

あなたが任意のより良いアイデアを持っている場合は教えてください:結果は次のようです!

+0

x0、x1、y0、y1 =(0、im_wb.shape [1]、sum(y0_list)/ len(y0_list)、sum(y1_list)/ len(y1_list)]にエラー「ZeroDivisionError: )) ' – Link

+0

@Silencerはなぜか分かりますか? – Link