-
Notifications
You must be signed in to change notification settings - Fork 0
/
reduccionDimensionalidad.py
189 lines (149 loc) · 5.61 KB
/
reduccionDimensionalidad.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
##########################################################
## Reduccion de dimensionalidad para Hu, Haralick y HOG ##
##########################################################
import matplotlib.patches as mpatches
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams['image.cmap'] = 'gray'
from skimage import io, color, img_as_float, img_as_ubyte, filters
import cv2
def escala_grises(image):
gris = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
return gris
def normalizacion(image):
image = cv2.resize(image, (500, 400))
return image
def gaussian(image):
image = cv2.GaussianBlur(image, (3, 3), 0)
return image
def sobel(image):
image = filters.sobel(image)
return image
#HOG: Histograma de gradientes orientados
from skimage.feature import hog
def histograma_hog(image):
caracteristica = hog(image, block_norm='L2-Hys').ravel()
return caracteristica
#Haralick Textura
import mahotas
def haralick(image):
caracteristica = mahotas.features.haralick(image).mean(axis=0)
return caracteristica
#Momentos de Hu
def hu(image):
caracteristica = cv2.HuMoments(cv2.moments(image)).flatten()
return caracteristica
#Determinacion de la base de datos
grapeWhite = io.ImageCollection('./Imagenes/Train/grapeWhite/*.png:./Imagenes/Train/grapeWhite/*.jpg')
grapeBlue = io.ImageCollection('./Imagenes/Train/grapeBlue/*.png:./Imagenes/Train/grapeBlue/*.jpg')
grapePink = io.ImageCollection('./Imagenes/Train/grapePink/*.png:./Imagenes/Train/grapePink/*.jpg')
grapeWhite_gray = []
grapeWhite_n = []
grapeWhite_edge = []
grapeBlue_gray = []
grapeBlue_n = []
grapeBlue_edge = []
grapePink_gray = []
grapePink_n = []
grapePink_edge = []
i = 0
for i in range(0, len(grapeWhite)):
aux = normalizacion(grapeWhite[i])
#aux = gaussian(aux)
grapeWhite_n.append(aux)
grapeWhite_gray.append(escala_grises(grapeWhite_n[i]))
grapeWhite_edge.append(sobel(grapeWhite_gray[i]))
i = 0
for i in range(0, len(grapeBlue)):
aux = normalizacion(grapeBlue[i])
#aux = gaussian(aux)
grapeBlue_n.append(aux)
grapeBlue_gray.append(escala_grises(grapeBlue_n[i]))
grapeBlue_edge.append(sobel(grapeBlue_gray[i]))
i = 0
for i in range(0, len(grapePink)):
aux = normalizacion(grapePink[i])
#aux = gaussian(aux)
grapePink_n.append(aux)
grapePink_gray.append(escala_grises(grapePink_n[i]))
grapePink_edge.append(sobel(grapePink_gray[i]))
#Estadistica -> Aca vamos a analizar la frecuencia de aparicion para cada uva, y para eso hacemos uso de la media aritmetica y la desviacion estandar
def estadistica(array):
sum = 0
for value in array:
sum += value
media = sum / len(array)
sum = 0
for value in array:
sum += np.power((value - media), 2)
desviacion = np.sqrt(sum / (len(array) - 1))
return media, desviacion
#HOG
grafico_hog, ax = plt.subplots()
for objeto in grapeWhite_gray:
grapeWhite_fhog = histograma_hog(objeto)
media, desviacion = estadistica(grapeWhite_fhog)
ax.plot(media, desviacion, 'o', color='yellow')
for objeto in grapeBlue_gray:
grapeBlue_fhog = histograma_hog(objeto)
media, desviacion = estadistica(grapeBlue_fhog)
ax.plot(media, desviacion, 'o', color='blue')
for objeto in grapePink_gray:
grapePink_fhog = histograma_hog(objeto)
media, desviacion = estadistica(grapePink_fhog)
ax.plot(media, desviacion, 'o', color='green')
ax.grid(True)
ax.set_title("Reduccion de dimensionalidad para HOG")
yellow_patch = mpatches.Patch(color='yellow', label='grapeWhite')
red_patch = mpatches.Patch(color='blue', label='grapeBlue')
blue_patch = mpatches.Patch(color='green', label='grapePink')
plt.legend(handles=[yellow_patch, red_patch, blue_patch])
plt.ylabel('Desviacion estandar')
plt.xlabel('Media aritmetica')
plt.show()
#Hu
grafico_hu, ax = plt.subplots()
for objeto in grapeWhite_edge:
grapeWhite_fhm = hu(objeto)
media, desviacion = estadistica(grapeWhite_fhm)
ax.plot(media, desviacion, 'o', color='yellow')
for objeto in grapeBlue_edge:
grapeBlue_fhm = hu(objeto)
media, desviacion = estadistica(grapeBlue_fhm)
ax.plot(media, desviacion, 'o', color='blue')
for objeto in grapePink_edge:
grapePink_fhm = hu(objeto)
media, desviacion = estadistica(grapePink_fhm)
ax.plot(media, desviacion, 'o', color='green')
ax.grid(True)
ax.set_title("Reduccion de Dimensionalidad para Hu")
yellow_patch = mpatches.Patch(color='yellow', label='grapeWhite')
red_patch = mpatches.Patch(color='blue', label='grapeBlue')
blue_patch = mpatches.Patch(color='green', label='grapePink')
plt.legend(handles=[yellow_patch, red_patch, blue_patch])
plt.ylabel('Desviacion estandar')
plt.xlabel('Media aritmetica')
plt.show()
#Haralick
grafica_haralick, ax = plt.subplots()
for objeto in grapeWhite_gray:
grapeWhite_fht = haralick(objeto)
media, desviacion = estadistica(grapeWhite_fht)
ax.plot(media, desviacion, 'o', color='yellow')
for objeto in grapeBlue_gray:
grapeBlue_fht = haralick(objeto)
media, desviacion = estadistica(grapeBlue_fht)
ax.plot(media, desviacion, 'o', color='blue')
for objeto in grapePink_gray:
grapePink_fht = haralick(objeto)
media, desviacion = estadistica(grapePink_fht)
ax.plot(media, desviacion, 'o', color='green')
ax.grid(True)
ax.set_title("Reduccion de Dimensionalidad para Haralick")
yellow_patch = mpatches.Patch(color='yellow', label='grapeWhite')
red_patch = mpatches.Patch(color='blue', label='grapeBlue')
blue_patch = mpatches.Patch(color='green', label='grapePink')
plt.legend(handles=[yellow_patch, red_patch, blue_patch])
plt.ylabel('Desviacion estandar')
plt.xlabel('Media aritmetica')
plt.show()