-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnormalize.py
128 lines (100 loc) · 3.59 KB
/
normalize.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
# -*- coding: utf-8 -*-
# @Author: amaneureka
# @Date: 2017-04-07 17:41:23
# @Last Modified by: amaneureka
# @Last Modified time: 2017-04-26 03:21:00
import cv2
import math
import numpy as np
import progressbar
import matplotlib.pyplot as plt
num_classes = 62
num_samples = 33
def find_samples_bounding_rect(path):
min_w = 0
min_h = 0
print ('finding bounding box:')
bar = progressbar.ProgressBar(maxval=num_classes*num_samples,
widgets=[
' [', progressbar.Timer(), '] ',
progressbar.Bar(),
' (', progressbar.ETA(), ') ',
])
bar.start()
counter = 0
for i in range(1, num_classes + 1):
for j in range(1, num_samples + 1):
filename = '{0}/Sample{1:03d}/img{1:03d}-{2:03d}.png'.format(path, i, j)
# opencv read -> Gray Image -> Bounding Rect
im = cv2.imread(filename)
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
imgray = cv2.bitwise_not(imgray)
_, contours, _ = cv2.findContours(imgray, cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
_, _, w, h = cv2.boundingRect(contours[len(contours) - 1])
# find maximum resolution
min_w = max(min_w, w)
min_h = max(min_h, h)
# update progress bar
counter = counter + 1
bar.update(counter)
bar.finish()
return min_w, min_h
def crop_images(filename, path, samplestart, width, height, showsamples, scaling):
print ('cropping images:')
bar = progressbar.ProgressBar(maxval=num_classes*num_samples,
widgets=[
' [', progressbar.Timer(), '] ',
progressbar.Bar(),
' (', progressbar.ETA(), ') ',
])
bar.start()
counter = 0
new_width = int(width * scaling + 0.5)
new_height = int(height * scaling + 0.5)
with open(path + filename, 'wb') as f:
# dump configs
f.write((num_classes).to_bytes(4, byteorder='little'))
f.write((num_samples).to_bytes(4, byteorder='little'))
f.write((new_width).to_bytes(4, byteorder='little'))
f.write((new_height).to_bytes(4, byteorder='little'))
# dump images
img_canvas = np.zeros((height, width), dtype=np.uint8)
for i in range(1, num_classes + 1):
for j in range(samplestart, samplestart + num_samples):
filename = '{0}/Sample{1:03d}/img{1:03d}-{2:03d}.png'.format(path, i, j)
# opencv read -> Gray Image -> Bounding Rect
im = cv2.imread(filename)
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
imgray = cv2.bitwise_not(imgray)
_, contours, _ = cv2.findContours(imgray, cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
x, y, w, h = cv2.boundingRect(contours[len(contours) - 1])
newimg = imgray[y : y + height, x : x + width]
factor = min(float(width/w), float(height/h))
factor = max(factor, 1.0)
newimg = cv2.resize(newimg, None, fx=factor, fy=factor, interpolation = cv2.INTER_CUBIC)[:height, :width]
# fix images which has lower dimensions
w = newimg.shape[1]
h = newimg.shape[0]
img_canvas.fill(0)
img_canvas[:h,:w] = newimg
# resize image
newimg = cv2.resize(img_canvas, None, fx=scaling, fy=scaling, interpolation = cv2.INTER_AREA)
# append ndarry to file
f.write(newimg.flatten())
# update progressbar
counter = counter + 1
bar.update(counter)
# preview if requested
if showsamples:
plt.imshow(newimg, cmap='gray')
plt.show()
f.close()
bar.finish()
return new_width, new_height
if __name__ == '__main__':
width, height = find_samples_bounding_rect('dataset')
print('Bounding Rectangle:: width: %d height: %d' % (width, height))
_, _ = crop_images('/normalized-train.bin', 'dataset', 1, width, height, False, 0.1)
num_samples = 22
width, height = crop_images('/normalized-val.bin', 'dataset', 14, width, height, False, 0.1)
print('Cropping:: width: %d height: %d' % (width, height))