-
Notifications
You must be signed in to change notification settings - Fork 2
/
SVM-GLCM-Color Moments.py
201 lines (154 loc) · 7.96 KB
/
SVM-GLCM-Color Moments.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
193
194
195
196
197
198
199
200
201
from PIL import Image
import numpy as np
import skimage
import skimage.io
import skimage.feature
from skimage.feature import greycoprops
import math
import statistics
import scipy
import scipy.stats
from scipy.stats import skew
from decimal import Decimal
from numpy import genfromtxt
import os
import svm_multiclass
from scipy import misc
train_feature = []
train_label = []
test_feature = []
test_label = []
root_train = 'C:\\Gio\\Skripsi\\image_train'
root_test = 'C:\\Gio\\Skripsi\\image_test'
def train(parent):
print("Training started!")
folder_index = 0
image_index = 0
image_total = 0
feature = []
label = []
index = 0
for folder in os.listdir(parent):
index = index + 1
current_path = "".join((parent, "/", folder))
print("Extracting Feature from", folder)
for file in os.listdir(current_path):
path = (current_path + "/" + file)
img = skimage.io.imread(path, as_gray=True)
img = skimage.img_as_ubyte(img)
img = np.asarray(img, dtype="int32")
g = skimage.feature.greycomatrix(img, [1], [0], levels=img.max()+1, symmetric=False, normed=True)
glcm_contrast = skimage.feature.greycoprops(g, 'contrast')[0][0]
glcm_energy = skimage.feature.greycoprops(g, 'energy')[0][0]
glcm_homogeneity = skimage.feature.greycoprops(g, 'homogeneity')[0][0]
glcm_correlation = skimage.feature.greycoprops(g, 'correlation')[0][0]
img = skimage.io.imread(path, as_gray=False)
img = np.asarray(img)
R = img[:, :, 0]
G = img[:, :, 1]
B = img[:, :, 2]
meanR = np.mean(R)
meanG = np.mean(G)
meanB = np.mean(B)
varianceR = np.var(R)
varianceG = np.var(G)
varianceB = np.var(B)
differenceR = 0.0
differenceG = 0.0
differenceB = 0.0
for i in range (len(img)):
for j in range (len(img[0])):
differenceR = differenceR - np.float_power((R[i][j] - meanR), 3)
differenceG = differenceG - np.float_power((G[i][j] - meanG), 3)
differenceB = differenceB - np.float_power((B[i][j] - meanB), 3)
N = len(img) * len(img[0])
skewnessR = np.float_power((differenceR/N), 1/3.)
skewnessG = np.float_power((differenceG/N), 1/3.)
skewnessB = np.float_power((differenceB/N), 1/3.)
if not glcm_contrast is None or not glcm_energy is None or not glcm_homogeneity is None or not glcm_correlation is None or not meanR is None or not meanG is None or not meanB is None or not varianceR is None or not varianceG is None or not varianceB is None or not skewnessR is None or not skewnessG is None or not skewnessB is None:
temp = [glcm_contrast, glcm_energy, glcm_homogeneity, glcm_correlation, meanR, meanG, meanB, varianceR, varianceG, varianceB, skewnessR, skewnessG, skewnessB]
# print(temp)
train_feature.append(temp)
train_label.append(index)
np.savetxt("SVM-GLCM-Color Moments_train_feature.csv", train_feature, delimiter=",")
np.savetxt("SVM-GLCM-Color Moments_train_label.csv", train_label, delimiter=",")
print("Training finish...")
def test(parent):
print("Testing started!")
folder_index = 0
image_index = 0
image_total = 0
feature = []
label = []
index = 0
for folder in os.listdir(parent):
index = index + 1
current_path = "".join((parent, "/", folder))
print("Extracting Feature from", folder)
for file in os.listdir(current_path):
path = (current_path + "/" + file)
img = skimage.io.imread(path, as_gray=True)
img = skimage.img_as_ubyte(img)
img = np.asarray(img, dtype="int32")
g = skimage.feature.greycomatrix(img, [1], [0], levels=img.max()+1, symmetric=False, normed=True)
glcm_contrast = skimage.feature.greycoprops(g, 'contrast')[0][0]
glcm_energy = skimage.feature.greycoprops(g, 'energy')[0][0]
glcm_homogeneity = skimage.feature.greycoprops(g, 'homogeneity')[0][0]
glcm_correlation = skimage.feature.greycoprops(g, 'correlation')[0][0]
img = skimage.io.imread(path, as_gray=False)
img = np.asarray(img)
R = img[:, :, 0]
G = img[:, :, 1]
B = img[:, :, 2]
meanR = np.mean(R)
meanG = np.mean(G)
meanB = np.mean(B)
varianceR = np.var(R)
varianceG = np.var(G)
varianceB = np.var(B)
differenceR = 0.0
differenceG = 0.0
differenceB = 0.0
for i in range (len(img)):
for j in range (len(img[0])):
differenceR = differenceR - np.float_power((R[i][j] - meanR), 3)
differenceG = differenceG - np.float_power((G[i][j] - meanG), 3)
differenceB = differenceB - np.float_power((B[i][j] - meanB), 3)
N = len(img) * len(img[0])
skewnessR = np.float_power((differenceR/N), 1/3.)
skewnessG = np.float_power((differenceG/N), 1/3.)
skewnessB = np.float_power((differenceB/N), 1/3.)
if not glcm_contrast is None or not glcm_energy is None or not glcm_homogeneity is None or not glcm_correlation is None or not meanR is None or not meanG is None or not meanB is None or not varianceR is None or not varianceG is None or not varianceB is None or not skewnessR is None or not skewnessG is None or not skewnessB is None:
temp = [glcm_contrast, glcm_energy, glcm_homogeneity, glcm_correlation, meanR, meanG, meanB, varianceR, varianceG, varianceB, skewnessR, skewnessG, skewnessB]
# print(temp)
test_feature.append(temp)
test_label.append(index)
np.savetxt("SVM-GLCM-Color Moments_test_feature.csv", test_feature, delimiter=",")
np.savetxt("SVM-GLCM-Color Moments_test_label.csv", test_label, delimiter=",")
print("Testing finish...")
def main():
X_train = genfromtxt('C:\\Gio\\PC-Riset\\Python\\SVM-GLCM-Color Moments_train_feature.csv', delimiter=',')
y_train = genfromtxt('C:\\Gio\\PC-Riset\Python\\SVM-GLCM-Color Moments_train_label.csv', delimiter=',')
X_train = np.nan_to_num(np.array(X_train))
y_train = np.nan_to_num(np.array(y_train)).astype(int)
X_test = genfromtxt('C:\\Gio\\PC-Riset\\Python\\SVM-GLCM-Color Moments_test_feature.csv', delimiter=',')
y_test = genfromtxt('C:\\Gio\\PC-Riset\Python\\SVM-GLCM-Color Moments_test_label.csv', delimiter=',')
X_test = np.nan_to_num(np.array(X_test))
y_test = np.nan_to_num(np.array(y_test)).astype(int)
# train(root_train)
# test(root_test)
# X_train = np.nan_to_num(np.array(train_feature))
# y_train = np.nan_to_num(np.array(train_label))
# X_test = np.nan_to_num(np.array(test_feature))
# y_test = np.nan_to_num(np.array(test_label))
# print("\nTraining Features\n")
# print("Training features with dimension:", X_train.shape)
# print("Training label with dimension:", y_train.shape)
# print("\nTest Features\n")
# print("Test features with dimension:", X_test.shape)
# print("Test label with dimension:", y_test.shape)
svm_multiclass.svm(X_train, X_test, y_train, y_test, 'linear', "GLCM-Color Moments")
svm_multiclass.svm(X_train, X_test, y_train, y_test, 'poly', "GLCM-Color Moments")
svm_multiclass.svm(X_train, X_test, y_train, y_test, 'sigmoid', "GLCM-Color Moments")
svm_multiclass.svm(X_train, X_test, y_train, y_test, 'rbf', "GLCM-Color Moments")
main()