-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeFeaturesFromImagesAndMasks.m
200 lines (167 loc) · 6.07 KB
/
makeFeaturesFromImagesAndMasks.m
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
% Calculates features values based on images and masks, input images and masks paths
% and outputDataFile with will contain all features values that is saved in
% data folder. Function also returns calculated features.
function makeFeaturesFromImagesAndMasks(imagesPath, masksPath, outputDataFile)
%load column names
resultsColDesc = load("data/mat/FeaturesColNames.mat").x;
%calculate features
[resultsNames, resultsClasses, results] = calculateFeatures(length(resultsColDesc)-1,imagesPath,masksPath);
%format results
resultsNames = split(resultsNames, ".png");
resultsClasses = split(resultsClasses, ".png");
resultsNames = resultsNames(~cellfun('isempty',resultsNames));
resultsClasses = resultsClasses(~cellfun('isempty',resultsClasses));
results(~any(results,2),:) = [];
%normalization -1 to 1
for i = 2:size(results,2)
results(:,i) = 2 * mat2gray(results(:,i)) - 1;
end
results = num2cell(results);
results = [resultsClasses, results];
T = cell2table(results, 'VariableNames', resultsColDesc, 'RowNames', resultsNames);
%% output files
outputDestinationCSV = "data/csv/" + outputDataFile + ".csv";
outputDestinationMAT = "data/mat/" + outputDataFile + ".mat";
writetable(T, outputDestinationCSV);
save(outputDestinationMAT, 'T', '-v7');
%print generated features names
fprintf("\nFrom %i images generated %i image features: ", length(resultsColDesc));
resultsColDesc
end
function [resultsNames, resultsClasses, results] = calculateFeatures(numFeatures,imagesPath,masksPath)
%% files operations
%go to masks and get list of files
cd(masksPath);
imagesMasks = dir;
cd ..;
cd ..;
%go to original and get list of files
cd(imagesPath);
imagesOriginal = dir;
cd ..;
cd ..;
if length(imagesOriginal) ~= length(imagesMasks)
ME = MException('MyComponent:noSuchVariable', 'Number of input images and their masks are different!');
throw(ME)
end
%% starting variables
numOfAdditional = numFeatures;
results = zeros(length(imagesMasks), numOfAdditional);
resultsNames = [];
resultsClasses = [];
fprintf('\nCalculating image features values ');
parfor i = 1:length(imagesMasks)
try
%get mask image
maskPath = imagesMasks(i).folder + "/" + imagesMasks(i).name;
IMask = imread(maskPath);
%get original image
orgPath = imagesOriginal(i).folder + "/" + imagesOriginal(i).name;
IOrg = imread(orgPath);
catch
continue;
end
%string formatting
name = imagesOriginal(i).name;
class = regexprep(name, '[\d]', '');
class = erase(class,"_");
resultsRow = [];
IOrg = filterOriginalImage(IOrg);
% %% Color Features (I1I2I3)
I1I2I3 = rgb2i1i2i3(IOrg);
I1 = I1I2I3(:,:,1);
I2 = I1I2I3(:,:,2);
I3 = I1I2I3(:,:,3);
[result] = featuresFromColorChannelHistogram(I1, IMask);
resultsRow = [resultsRow, result];
[result] = featuresFromColorChannelHistogram(I2, IMask);
resultsRow = [resultsRow, result];
[result] = featuresFromColorChannelHistogram(I3, IMask);
resultsRow = [resultsRow, result];
%% xyz alternative features
[result] = momentsFeaturesXYZ(IOrg, IMask);
resultsRow = [resultsRow, result];
%% GLCM Features
[result] = GLCMFeatures(IOrg, IMask);
for j = 1:size(result,1)
resultsRow = [resultsRow, result(j,:)];
end
%% GLRLM Features
[result] = GLRLMFeatures(IOrg);
for j = 1:size(result,1)
resultsRow = [resultsRow, result(j,:)];
end
%% ADD ALL DATA TO RESULTS
results(i,:) = resultsRow;
resultsNames = [resultsNames, name];
resultsClasses = [resultsClasses, class];
end
end
%% FEATURES FOR SELECTED CHANNEL AND COLOR SPACE
function [x] = featuresFromColorChannelHistogram(I, IMask)
I_mean = mean(I(IMask));
I_std = std(I(IMask));
%histogram based
I_hist = imhist(I(IMask));
I_mean_h = mean(I_hist);
I_var_h = var(I_hist);
I_kurt = kurtosis(I_hist);
I_skew = skewness(I_hist);
I_entr = entropy(I_hist);
I_e = sum(I(IMask), 'all');
x = [I_mean, I_std, I_mean_h, I_var_h, I_kurt, I_skew, I_entr, I_e];
end
% function [x] = featuresFromColorChannelStatistics(I, IMask)
% Im = I(IMask);
% I_mean = mean(Im);
% I_std = std(Im);
% I_me = median(Im);
% I_ad = mean(abs(Im - I_mean)); %avarage deviation
% I_q1 = quantile(Im,0.25);
% I_q3 = quantile(Im,0.75);
% I_moment_1 = sum([mean(Im.^2),mean(Im.^3),mean(Im.^4),mean(Im.^5)]);%sum of moments of 2nd to 5th degree
% I_moment_2 = sum([mean(Im.^6),mean(Im.^7),mean(Im.^8),mean(Im.^9)]);%sum of moments of 6th to 10th degree
%
% x = [I_mean, I_std, I_me, I_ad, I_q1, I_q3, I_moment_1, I_moment_2];
% end
%% calculate moments features in xyz color space
function [x] = momentsFeaturesXYZ(I, IMask)
I = rgb2xyz(I);
x = [];
for i = 1:3
Ii = I(:,:,i);
Im = Ii(IMask);
I_mean = mean(Im);
I_std = std(Im);
I_kurt = kurtosis(Im);
I_skew = skewness(Im);
x = [x, I_mean, I_std, I_skew, I_kurt];
end
end
% function [x] = centroidsCalculations(I, k)
% Ik = im2uint8(I);
% idx_map = imsegkmeans(Ik,k);
% x = zeros(1, k);
% for i = 1:k
% x(i) = mean(I(idx_map==i));
% end
% end
%% filter original image
function I = filterOriginalImage(I)
%adaptive histeq
LAB = rgb2lab(I);
L = LAB(:,:,1)/100;
L = adapthisteq(L);
LAB(:,:,1) = L*100;
I = lab2rgb(LAB);
%
I = imgaussfilt(I);
I = im2double(I);
end
%% convert rgb image to i1i2i3 color space
function i1i2i3 = rgb2i1i2i3(rgb)
tm = [1/3 1/3 1/3; 0.5 0 -0.5; -0.25 0.5 -0.25];
[w, h, c] = size(rgb);
rgb = reshape(im2double(rgb), [], c)';
i1i2i3 = (reshape((tm*rgb)', w, h, c)+1)/2;
end