-
Notifications
You must be signed in to change notification settings - Fork 0
/
OCR.py
165 lines (126 loc) · 4.78 KB
/
OCR.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import cv2
import numpy as np
import matplotlib.pyplot as plot
def image_binarization( image ):
for row in range( 0, row_length ):
for column in range( 0, column_length ):
if image[row][column]>127:
image[row][column]=255
else:
image[row][column]=0
#if image[ row ][ column ] == bg_color:
#if bg_color > 127:
#image[ row ][ column ] = 255
#else:
#image[ row ][ column ] = 0
#else:
#if bg_color > 127:
# image[ row ][ column ] = 0
#else:
# image[ row ][ column ] = 255
return image
def image_row_split( image ):
bg_color = 255
row_length = len( image )
column_length = len( image[ 0 ] )
image_in_rows = []
is_last_blank_row = True
last_row_index = 0
for row in range( 0, row_length ):
is_row_blank = True
for column in range( 0, column_length ):
if image[ row ][ column ] == bg_color:
pass
else:
is_row_blank = False
break
if is_last_blank_row == is_row_blank:
pass
else:
if is_row_blank == False:
image[ row - 1 ] = [ 0 for column in range( 0, column_length ) ]
# 本行开始位置的行号
last_row_index = row
else:
image[ row ] = [ 0 for column in range( 0, column_length ) ]
# 已经找到本行开始位置和结束位置的行号,然后复制一行
image_in_rows.append( image[ last_row_index : row ] )
is_last_blank_row = is_row_blank
# 第一个返回值是划了分行线的完整图像
# 第二个返回值是一个List,每个元素都是分割出来的一行
return image, image_in_rows
def image_char_split( image, image_row_splited ):
image_save( "image_test.png", image )
bg_color = 255
row_length = len( image )
column_length = len( image[ 0 ] )
is_last_blank_column = True
last_left_line_index = 0
chars_splited = []
for column in range( 0, column_length ):
is_column_blank = True
for row in range( 0, row_length ):
if image[ row ][ column ] == bg_color:
pass
else:
is_column_blank = False
break
if is_last_blank_column == is_column_blank:
pass
else:
if is_column_blank == False:
for row in range( 0, row_length ):
image_row_splited[ row ][ column - 1 ] = 0
# 找到本列字符的开始列号
last_left_line_index = column
else:
single_char = []
for row in range( 0, row_length ):
image_row_splited[ row ][ column ] = 0
# 有了开始和结束的列号,然后逐行复制,形成一个完整的字符
single_char.append( image[ row ][ last_left_line_index : column ] )
# 将分割出的每一个字符加入List
# 注意需要将Python的List转换为numpy的多维数组(即png图像)
chars_splited.append( np.asarray( single_char ) )
is_last_blank_column = is_column_blank
return chars_splited
def main():
image = image_read( "C:/Users/Administrator/Desktop/3.jpg", mode = cv2.IMREAD_GRAYSCALE )
image_save( "number_gray.png", image )
image_binary = image_binarization( np.copy( image ) )
image_save( "number_binary.png", image_binary )
# 得到分好行的图像及划了分割线的完整图像
image_row_lined, image_row_splited = image_row_split( np.copy( image_binary ) )
# 逐行进行分割
for r in range( 0, len( image_row_splited ) ):
image_save("number_in_row_"+str(r)+".png",image_row_splited[r])
# 得到每一行所分割出的字符
image_char_splited =image_char_split(np.copy(image_row_splited[r]), np.copy(image_row_lined))
# 对每一个字符进行处理
for c in range( 0, len( image_char_splited ) ):
# 行假设图像的高>宽,则设定最长的边是高
max_length = len( image_char_splited[ c ] )
# 计算高宽比
ratio = max_length / len( image_char_splited[ c ][ 0 ] )
if len( image_char_splited[ c ][ 0 ] ) > max_length:
# 如果高<宽
# 设定最长的边是宽
max_length = len( image_char_splited[ c ][ 0 ] )
# 计算宽高比
ratio = max_length / len( image_char_splited[ c ] )
# 将高<宽的图像放大10倍,并使其高=宽
large_image = cv2.resize( image_char_splited[ c ],
( max_length * 10,
int(len(image_char_splited) * ratio * 10)))
else:
# 如果高>宽
# 将高>宽的图像放大10倍,并使其高=宽
large_image = cv2.resize( image_char_splited[ c ],
( max_length * 10,
int(len(image_char_splited[0])*ratio*10)))
# 放大之后的图像会再次变为灰度图(依图像放大的算法而定)
# 需要将放大之后的图像再次手动处理为二值图,然后保存
image_save( "number_" + str( r ) + "_" + str( c ) + ".png",
image_binarization( large_image ))
if __name__ == "__main__":
main()