-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path2_computeHaralickFeatures.py
More file actions
executable file
·87 lines (62 loc) · 2.09 KB
/
2_computeHaralickFeatures.py
File metadata and controls
executable file
·87 lines (62 loc) · 2.09 KB
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
import cv2
import pandas as pd
import os
import numpy as np
from skimage.feature import greycomatrix, greycoprops
def computeHaralick(path):
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
distances = [1, 2, 3]
angles = [0, np.pi / 4, np.pi / 2, 3 * np.pi / 4]
properties = ['energy', 'homogeneity']
glcm = greycomatrix(gray,
distances=distances,
angles=angles,
symmetric=True,
normed=True)
feats = np.hstack([greycoprops(glcm, prop).ravel() for prop in properties])
return feats
folderImages = 'db_faces/{}/'
print("Training db ..")
xTrain = []
yTrain = []
currentFolder = folderImages.format('train')
print('Compute train features ')
folders = ['0', '1']
for folder in folders:
print('Processing folder ' + folder)
images = [os.path.join(currentFolder, folder, file) for file in os.listdir(currentFolder + folder)]
i = 0
for path in images:
feats = computeHaralick(path)
xTrain.append(feats)
yTrain.append(int(folder))
if i % 100 == 0:
print(i)
i += 1
xTest = []
yTest = []
currentFolder = folderImages.format('test')
print('Compute test features ')
folders = ['0', '1']
for folder in folders:
print('Processing folder ' + folder)
images = [os.path.join(currentFolder, folder, file) for file in os.listdir(os.path.join(currentFolder, folder))]
i = 0
for path in images:
feats = computeHaralick(path)
xTest.append(feats)
yTest.append(int(folder))
if i % 100 == 0:
print(i)
i += 1
if not os.path.exists('features'):
os.mkdir('features')
df = pd.DataFrame(xTrain)
df.to_csv("features/xTrain_glcm.csv", index=False)
df = pd.DataFrame(xTest)
df.to_csv("features/xTest_glcm.csv", index=False)
df = pd.DataFrame(yTrain)
df.to_csv("features/yTrain_glcm.csv", index=False)
df = pd.DataFrame(yTest)
df.to_csv("features/yTest_glcm.csv", index=False)