-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUtils.py
executable file
·192 lines (136 loc) · 4.28 KB
/
Utils.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
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
from math import *
from numpy import *
import numpy as numpy
from Data import *
from glob import *
import cv2
from sys import*
#---------------------------------------Variables----------------------------------------
label_to_number = {'Populus_Nigra': 1, 'Acer_platanoides':2, 'Sorbus aucuparia': 3, 'Quercus_Rubra': 4,
'Ginkgo_Biloba': 5, 'Acer_Capillipes' : 6,'Tilia_Tomentosa': 7, 'Fagus_Sylvatica': 8,
'Olea_Europaea':9, 'Quercus_Shumardii':10, 'Prunus_Avium': 11, 'Ilex_Aquifolium': 12,
'Castanea_Sativa': 13, 'Acer_Circinatum' : 14, 'Acer_Platanoids': 15, 'Acer_Palmatum': 16,
'Liquidambar_Styraciflua': 17, 'Quercus_Vulcanica':18}
#-------------------------------------Image Processing Tools-----------------------------
def get_cnt(img):
ret,thresh = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
img,contours,hierarchy = cv2.findContours(thresh, 1, 2)
cnt = contours[0]
return cnt
def get_moments(img):
return cv2.moments(get_cnt(img))
def get_solidity(img):
cnt = get_cnt(img)
area = cv2.contourArea(cnt)
hull = cv2.convexHull(cnt)
hull_area = cv2.contourArea(hull)
if hull_area == 0:
hull_area = 0.0001
solidity = float(area)/hull_area
return solidity
def max_x_diff(img):
edge_points = get_edge_points(img)
min_x = maxint
max_x = -1
for point in edge_points:
x = point[0]
if x > max_x:
max_x = x
if x < min_x:
min_x = x
x_diff = 1.0*max_x - min_x
return x_diff
def max_y_diff(img):
edge_points = get_edge_points(img)
min_y = maxint
max_y = -1
for point in edge_points:
y = point[1]
if y > max_y:
max_y = y
if y < min_y:
min_y = y
y_diff = 1.0*max_y - min_y
return y_diff
def display_image(img):
cv2.namedWindow('Image', cv2.WINDOW_NORMAL)
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def get_edge_points(img):
edges = []
canny_image = cv2.Canny(img, 100, 200)
for i in range(len(canny_image)):
for j in range(len(canny_image[0])):
if canny_image[i][j] == 255:
edges.append((i,j))
return edges
def get_corner_points(img, maxFeat):
feature_params = dict( maxCorners = maxFeat, qualityLevel = 0.6, minDistance = 7, blockSize = 7 )
corners = cv2.goodFeaturesToTrack(img, mask = None, **feature_params)
return corners
def get_binary_image_contours(imgray):
ret,thresh = cv2.threshold(imgray,127,255,cv2.THRESH_BINARY_INV)
#se = ones((15,15), dtype='uint8')
#image_close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, se)
mask = zeros(imgray.shape[:2], uint8)
image,contours, hierarchy = cv2.findContours(thresh,1,2)
cv2.drawContours(mask,contours,-1,(200,200,0),3)
#display_image(mask)
return mask
def get_image_area(image):
nonzero = count_nonzero(image)
return nonzero
def read_csv_table(table_path):
headers = []
rows = []
f = open(table_path, 'r')
for line in f:
line = line.replace('\n', '')
splited_line = line.split(',')
splited_line = filter(lambda a: a != '', splited_line)
splited_line = filter(lambda a: a != '\r', splited_line)
if len(headers) == 0:
headers = splited_line
else:
rows.append(splited_line)
f.close()
return (headers, rows)
def read_training_table(table_path, to_number=None):
data = DataStructure()
feature_vectors = []
labels = []
ids = []
numeric_labels = []
(feature_names, feature_vectors_str) = read_csv_table(table_path)
feature_names = feature_names[2:len(feature_names)]
for row in feature_vectors_str:
labels.append(row[1])
if to_number == None:
numeric_labels.append(label_to_number[row[1]])
else:
numeric_labels.append(to_number[row[1]])
ids.append(row[0])
row = row[2:len(row)]
feature_vectors.append([float(feature) for feature in row])
data.set_feature_vectors(array(feature_vectors))
data.set_feature_names(array(feature_names))
data.set_labels(array(labels))
data.set_table_ids(array(ids))
data.set_numeric_labels(array(numeric_labels))
return data
def read_image_grayscale(image_path):
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
return image
def read_all_grayscale_images(images_directory_path):
files = glob(images_directory_path+'/*.jpg')
images = []
data = DataStructure()
ids = []
for f in files:
images.append(read_image_grayscale(f))
ids.append(f)
data.set_images_binary(array(images))
data.set_table_ids(array(ids))
return data