-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_crop.py
More file actions
203 lines (155 loc) · 6.56 KB
/
image_crop.py
File metadata and controls
203 lines (155 loc) · 6.56 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# -*- coding: utf-8 -*-
from PIL import Image
import numpy
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import glob
import os
# Functions
def binarize_image(img_path, threshold):
"""Preprocess the image."""
image_file = Image.open(img_path)
image = image_file.convert('L') # convert image to monochrome
image = numpy.array(image)
image = binarize_array(image, threshold)
return image
def binarize_array(numpy_array, threshold):
"""Binarize a numpy array."""
for i in range(len(numpy_array)):
for j in range(len(numpy_array[0])):
if numpy_array[i][j] > threshold:
numpy_array[i][j] = 1
else:
numpy_array[i][j] = 0
return numpy_array
def palauta_indexi_vaaka_j(numpy_array):
pysty_vektori = numpy_array.sum(axis = 0).tolist()
for indexi in range(len(pysty_vektori)):
if pysty_vektori[indexi] > 10:
return indexi
def palauta_indexi_pysty_i(numpy_array):
vaaka_vektori = numpy_array.sum(axis = 1).tolist()
for indexi in range(len(vaaka_vektori)):
if vaaka_vektori[indexi] > 10:
return indexi
def center_to_left_corner(numpy_array):
# Detect the corner
indj = palauta_indexi_vaaka_j(numpy_array)
indi = palauta_indexi_pysty_i(numpy_array)
temp_array = numpy.delete(numpy_array, numpy.s_[0:indi+1], axis=0)
temp_array = numpy.delete(temp_array, numpy.s_[0:indj+1], axis=1)
return temp_array
def siisti_reunat(numpy_array):
"""Binarize a numpy array."""
for i in range(len(numpy_array)):
for j in range(len(numpy_array[0])):
if numpy_array[i][0] > 0:
numpy_array[i][0] = 0
if numpy_array[0][j] > 0:
numpy_array[0][j] = 0
if numpy_array[len(numpy_array)-1][j] > 0:
numpy_array[len(numpy_array)-1][j] = 0
if numpy_array[i][len(numpy_array[0])-1] > 0:
numpy_array[i][len(numpy_array[0])-1] = 0
return numpy_array
def sliding_window(file_destination,temp_array, folder, photo_name):
leveyslista = width_of_pictures(temp_array)
korkeuslista = height_of_pictures(temp_array)
valit = paljon_siirrytaan(temp_array)
#ikkunan kokoon alustus
i_max = korkeuslista[0]
i_min = 0
for i_siirtyma in range(0, 8):
j_max = leveyslista[i_siirtyma] #leveys
j_min = 0
if i_siirtyma > 0:
i_min = i_max + valit[i_siirtyma-1]
i_max = i_max + korkeuslista[i_siirtyma]+ valit[i_siirtyma-1]
for j_siirtyma in range(0, 15):
kuva = siisti_reunat(temp_array[i_min:i_max, j_min:j_max])
kuva = make_image_right_size(kuva)
kuvan_nimi = str(folder)+str(photo_name) + str(i_siirtyma)+ str(j_siirtyma) + '.png'
plt.imsave(file_destination +str(kuvan_nimi), numpy.array(kuva).reshape(28,28), cmap=cm.gray)
j_min = j_max + 2
j_max = j_max + leveyslista[j_siirtyma] + 2
def make_image_right_size(imag):
# 1 niin levitystä tulee oikealle ja 0 niin vasemmalle
if len(imag[0]) < 28:
for i in range(28-len(imag[0])):
imag = increase_width(imag)
if len(imag) < 28:
for i in range(28-len(imag)):
difference = (28-len(imag))
if difference % 2 == 0:
imag = increase_height(imag, 1)
else:
imag = increase_height(imag, 0)
return imag
# Photos need to be 28x28 for this CNN-model
def increase_width(imag):
imag = numpy.c_[imag,numpy.zeros(len(imag))]
return imag
def increase_height(imag,side):
if side == 0:
imag = numpy.r_[[imag[0]],imag]
if side == 1:
imag = numpy.r_[imag,[imag[0]]]
return imag
def width_of_pictures(temp_array):
# Check what is the width of the picture, returns size 15 list
montakonollaa = 0
lista_leveyksista = []
for kaylapi in range(len(temp_array[2])):
if temp_array[2][kaylapi] == 0:
montakonollaa = montakonollaa + 1
if temp_array[2][kaylapi] == 1 and temp_array[2][kaylapi-1] == 0:
#print(montakonollaa)
lista_leveyksista.append(montakonollaa)
montakonollaa = 0
return lista_leveyksista
def height_of_pictures(temp_array):
# Check what is the height of the picture, return size 8 list
montakonollaa = 0
lista_korkeuksista = []
for kaylapi in range(len(temp_array)):
if temp_array[kaylapi][0] == 0:
montakonollaa = montakonollaa + 1
if temp_array[kaylapi][0] == 1:
#print(montakonollaa)
if montakonollaa > 12:
lista_korkeuksista.append(montakonollaa)
montakonollaa = 0
return lista_korkeuksista
def paljon_siirrytaan(temp_array):
montakonollaa = 0
lista_valeista = []
for kaylapi in range(len(temp_array)):
if temp_array[kaylapi][0] == 0:
montakonollaa = montakonollaa + 1
if temp_array[kaylapi][0] == 1:
#print(montakonollaa)
if montakonollaa < 12:
lista_valeista.append(montakonollaa+2)
montakonollaa = 0
return lista_valeista
def run():
# List of folders
subfolder = []
subfolder.append('Roulette')
# Threshold for binarizing the image
threshold = 70;
for i in range(len(subfolder)):
kansio = subfolder[i]
# Path mihin pilkotut kuvat seivataan
os.chdir(r'C:/Users/tero7/OneDrive/Työpöytä/project/'+ kansio)
path = os.getcwd()
file_destination = r'C:/Users/tero7/OneDrive/Työpöytä/project/image_database/'
print('Cropping photos from the table: ' + kansio)
for filename in glob.glob(path +'/*.png'):
head, photo_name = os.path.split(filename)
numpy_array = binarize_image(photo_name, threshold)
temp_array = center_to_left_corner(numpy_array)
sliding_window(file_destination,temp_array,kansio,photo_name)
print('Table ok')
print('Done')
run()