-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_extraction.py
175 lines (125 loc) · 5.19 KB
/
feature_extraction.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
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 14 08:53:55 2023
@author: Enzo
"""
import matplotlib.pyplot as plt
import numpy as np
from alive_progress import alive_bar
from skimage.feature import graycomatrix, graycoprops
from skimage.feature import local_binary_pattern
import pandas as pd
import seaborn as sns
sns.set_theme(style="ticks")
plt.rcParams['figure.dpi'] = 600
def extract_lbp(images):
lbps = []
with alive_bar(len(images), title="Extracting LBP", bar='smooth', spinner=None) as bar:
for image in images:
lbp = local_binary_pattern(image, P=8, R=2)
lbps.append(lbp)
bar()
return np.array(lbps)
def split_image(image, sub_images_num, bins_per_sub_images):
fig, ax = plt.subplots()
grid = np.arange(
0, image.shape[1]+1, image.shape[1]//sub_images_num)
sub_image_histograms = []
for i in range(1, len(grid)):
for j in range(1, len(grid)):
sub_image = image[grid[i-1]:grid[i], grid[j-1]:grid[j]]
sub_image_histogram = np.histogram(sub_image,
bins=bins_per_sub_images)[0]
sub_image_histograms.append(sub_image_histogram)
histogram = np.array(sub_image_histograms).flatten()
ax.hist(histogram)
plt.axis('off')
plt.show()
def create_histograms(images, sub_images_num, bins_per_sub_images):
all_histograms = []
# fig, ax = plt.subplots(3, 3)
with alive_bar(len(images), title="Creating Histograms", bar='smooth', spinner=None) as bar:
for image in images:
grid = np.arange(
0, image.shape[1]+1, image.shape[1]//sub_images_num)
sub_image_histograms = []
# temp = {}
# temp['image'] = [image]
# temp['class'] = label
# ctr = 1
for i in range(1, len(grid)):
for j in range(1, len(grid)):
sub_image = image[grid[i-1]:grid[i], grid[j-1]:grid[j]]
# temp[f'sub_image_{ctr}'] = [sub_image]
# ctr += 1
sub_image_histogram = np.histogram(
sub_image, bins=bins_per_sub_images)[0]
sub_image_histograms.append(sub_image_histogram)
histogram = np.array(sub_image_histograms).flatten()
# temp = pd.DataFrame(temp)
# sub_images_df = pd.concat([sub_images_df, temp], axis = 1)
all_histograms.append(histogram)
bar()
return np.array(all_histograms)
def extract_glcm(images,
sub_images_num,
dists=[5],
angles=[0, np.pi/4, np.pi/2, 3*np.pi/4],
lvl=256,
sym=True,
norm=True):
props = ['dissimilarity', 'correlation',
'homogeneity', 'contrast', 'ASM', 'energy']
glcms = []
with alive_bar(len(images), bar='smooth', spinner=None) as bar:
for image in images:
grid = np.arange(
0, image.shape[1]+1, image.shape[1]//sub_images_num)
image_features = []
sub_image_features = []
for i in range(1, len(grid)):
for j in range(1, len(grid)):
sub_image = image[grid[i-1]: grid[i], grid[j-1]: grid[j]]
glcm = graycomatrix(sub_image,
distances=dists,
angles=angles,
levels=lvl,
symmetric=sym,
normed=norm)
glcm_props = [prop for name in props for
prop in graycoprops(glcm, name)[0]]
for item in glcm_props:
sub_image_features.append(item)
image_features.append(sub_image_features)
features = np.array(image_features).flatten()
glcms.append(features)
bar()
return np.array(glcms)
def extract_glcm_noloop(images,
dists=[5],
angles=[0, np.pi/4, np.pi/2, 3*np.pi/4],
lvl=256,
sym=True,
norm=True):
props = ['dissimilarity', 'correlation',
'homogeneity', 'contrast', 'ASM', 'energy']
glcms = []
with alive_bar(len(images), bar='smooth', spinner=None) as bar:
for image in images:
channel_features = []
for c in range(3):
features = []
glcm = graycomatrix(image[:, :, c],
distances=dists,
angles=angles,
levels=lvl,
symmetric=sym,
normed=norm)
glcm_props = [prop for name in props for
prop in graycoprops(glcm, name)[0]]
for item in glcm_props:
features.append(item)
channel_features.append(features)
glcms.append(channel_features)
bar()
return np.array(glcms)