09
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
return cv2.imread( filename, mode )
else:
return cv2.imread( filename )
#
def image_save( filename, image ):
return cv2.imwrite( filename, image )
#
def image_binarization( image ):
#
row_length = len( image )
column_length = len( image[ 0 ] )
bg_color = 0
front_color = 0
#
for row in range( 0, row_length ):
bg_color_is_found = True
color = image[ row ][ 0 ]
for column in range( 0, column_length ):
if color == image[ row ][ column ]:
pass
else:
bg_color_is_found = False
break
if bg_color_is_found:
bg_color = color
break
#
for row in range( 0, row_length ):
for column in range( 0, column_length ):
#
if image[ row ][ column ] == bg_color:
# 127 255
if bg_color > 127:
image[ row ][ column ] = 255
# 127 0
else:
image[ row ][ column ] = 0
#
else:
#
if bg_color > 127:
image[ row ][ column ] = 0
else:
image[ row ][ column ] = 255
return image
def main():
#
image = image_read( "input/number.png", mode = cv2.IMREAD_GRAYSCAL
#
image_save( "output/number_gray.png", image )
#