-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
167 lines (134 loc) · 4.91 KB
/
main.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
166
167
import cv2 as cv
import numpy as np
import os
import time
import errno
# Globals
# FOLDER_TO_READ = './output/new-pro'
# FOLDER_TO_READ = 'D:/Ryan/Dropbox/Sharing/project data/128_mes'
# FOLDER_TO_READ = '/Users/Ryan/Dropbox/Sharing/project data/128_pro'
FOLDER_TO_READ = '/Users/Ryan/Desktop/128-pro'
START_TIME = time.strftime("%Y%m%d-%H%M")
OUTPUT_PATH = "./output/output-{}/".format(START_TIME)
# Image processing parameters
BLUR_KERNEL = 255
def main():
# Call function to run
# partition_imgs(128)
generate_csv_from_imgs()
def generate_csv_from_imgs():
raw_imgs, filenames = get_images_in_dir(FOLDER_TO_READ)
for x in raw_imgs:
res = x.flatten()
#print(len(res))
row = ""
for pix_val in res:
row += str(pix_val) + ","
row += "1"
print(row)
def partition_imgs(dim_size):
# Create output environment
create_output_environment(START_TIME)
raw_imgs, filenames = get_images_in_dir(FOLDER_TO_READ)
# Normalize
raw_imgs = normalize_all_images(raw_imgs, BLUR_KERNEL)
# Placeholder because I'm bored and want to print the shapes
for i in range(len(raw_imgs)):
print("Original shape: {}".format(raw_imgs[i].shape))
subdivisions_of_img = get_subdivisions(raw_imgs[i], dim_size,
dim_size)
print("Size of subdivisions of img array:"
"{}".format(subdivisions_of_img.size))
# Save the subdivisions
for j in range(len(subdivisions_of_img)):
img_name = "{}_{}".format(j, filenames[i])
save_image(subdivisions_of_img[j], img_name, OUTPUT_PATH)
# print("Flattening subdivisions...")
# res = np.array(flatten_imgs(subdivisions_of_img))
# print("Shape of result: {}".format(res.shape))
# print('\n')
print("Finished with whatever we were supposed to be doing.")
def normalize_all_images(img_array, kernel_size):
normalized_array = []
len_img_array = len(img_array)
for i in range(len_img_array):
print("Normalizing image {}/{}".format(i, len_img_array))
normalized_img = normalize_lighting(img_array[i], kernel_size)
normalized_array.append(normalized_img)
return normalized_array
def normalize_lighting(img, kernel_size):
blur_kernel_size = (kernel_size, kernel_size)
sigma_x = 0 # stdev for blur operation
blur = cv.GaussianBlur(img, blur_kernel_size, sigma_x)
normalized_img = cv.subtract(img, blur)
return normalized_img
def get_subdivisions(arr, nrows=40, ncols=40):
"""
Return an array of shape (n, nrows, ncols) where
n * nrows * ncols = arr.size
If arr is a 2D array, the returned array should look like n subblocks with
each subblock preserving the "physical" layout of arr.
"""
lx, ly = arr.shape
return (arr.reshape(lx//nrows, nrows, -1, ncols)
.swapaxes(1,2)
.reshape(-1, nrows, ncols))
def flatten_imgs(tiles):
# flatten divided each sub-image into a 1D array
result = []
for x in tiles:
x = np.array(x)
result.append(x.flatten())
result = np.array(result)
return result
def get_images_in_dir(dir):
'''
Traverses files in dir specified, getting the images and returning them
as a list of opencv objects
'''
# Get all image files in the folder specified
#print('Searching directory {} for images...'.format(dir))
img_files = []
for subdir, dirs, files in os.walk(dir):
for file_name in files:
img_files.append(file_name)
#print("Found file: {}".format(file_name))
#if (len(img_files) == 0):
#print("No images found, or directory doesn't exist!")
# For each file, determine if it's an image. If so, open it as an opencv
# object
imgs = []
for f in img_files:
if (len(f) < 4 or f[-4:] != ".tif"):
continue
path_to_f = '{}/{}'.format(dir, f)
imgs.append(cv.imread(path_to_f, 0)) # Read with openCV in grayscale
return (imgs, img_files)
def save_image(image_to_save, file_name, output_path):
print ("Writing {} to {}".format(file_name, output_path))
cv.imwrite((output_path + file_name), image_to_save)
print ("Done\n")
def create_output_environment(start_time):
try:
os.mkdir('./output/')
except OSError as e:
if e.errno == errno.EEXIST:
print("OutputHandler: output folder already created. Doing "
"nothing...")
else:
raise # raise exception if it is not the not exist error
try:
os.mkdir('./output/output-{}/'.format(start_time))
except OSError as e:
print("Something weird is happening. Find me...")
# must run on img in its 2D state. returns array of all four rotations for a given image.
def create_img_rots(img):
res = []
res.append(img)
res.append(np.rot90(img))
res.append(np.rot90(res[1]))
res.append(np.rot90(res[2]))
res = np.array(res)
return res
if __name__ == '__main__':
main()