-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOptimizeDatabase.m
86 lines (59 loc) · 2.31 KB
/
OptimizeDatabase.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
function [] = OptimizeDatabase(TurnOnOpt, treshold, amountOfImages)
load dataBase.mat dataBase
if(TurnOnOpt == true)
disp('Optimizing database: option one');
end
%------------------------- PLACEHOLDER DATA -----------------------------
counter = 1;
colorBase = cell(amountOfImages, 1);
for i = 1:amountOfImages
colorBase{i} = struct('L', zeros(size(dataBase{1}, 1), size(dataBase{1}, 2)), ...
'A', zeros(size(dataBase{1}, 1), size(dataBase{1}, 2)), ...
'B', zeros(size(dataBase{1}, 1), size(dataBase{1}, 2)));
end
%----------------------------- OPTIMIZATION ---------------------------------
% Add the images that differentiate enough from other images in the database
for i = 1:amountOfImages
currentImage = dataBase{i};
labImage = rgb2lab(currentImage);
L = labImage(:, :, 1);
A = labImage(:, :, 2);
B = labImage(:, :, 3);
foundSimilar = false;
if TurnOnOpt == true
for j = 1:amountOfImages
delta = sqrt((colorBase{j}.L(:) - L(:)).^2 + ((colorBase{j}.A(:) - A(:)).^2 + ((colorBase{j}.B(:) - B(:)).^2)));
mean_delta = mean(mean(delta));
if mean_delta < treshold
foundSimilar = true;
break;
end
end
end
% If no similair and the option is turned on, add image
if TurnOnOpt == true && foundSimilar == false
if (counter <= amountOfImages)
colorBase{i} = struct('L', L, 'A', A, 'B', B);
counter = counter + 1;
end
end
% If option is turned off, always add (normal database).
if TurnOnOpt == false
colorBase{i} = struct('L', L, 'A', A, 'B', B);
end
end
%------------------------- REMOVE PLACEHOLDERS ----------------------------
% Remove placeholders that were not occupied
% (In case the treshold were tighter than allowed images.)
numImages = numel(colorBase);
imagesToRemove = [];
for i = 1:numImages
isBlack = all(colorBase{i}.L(:) == 0) && all(colorBase{i}.A(:) == 0) && all(colorBase{i}.B(:) == 0);
if isBlack
imagesToRemove = [imagesToRemove, i];
end
end
colorBase(imagesToRemove) = [];
fprintf('Size of dataset: %d\n', numel(colorBase));
save colorBase colorBase;
end