-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReproductionOnStructure.m
119 lines (76 loc) · 3.79 KB
/
ReproductionOnStructure.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
function [rgbImage] = ReproductionOnStructure(queryImageLab,QueyImageRGB)
load colorBase.mat colorBase
load dataBase.mat dataBase
disp('Calculating reproduction image on structure...');
sizeDatabase = numel(colorBase);
% ----------------- Colors in LAB for all the regions ------------------
targetColors = zeros(200, 3);
for q = 1:sizeDatabase
targetColors(q, 1) = mean(colorBase{q}.L(:));
targetColors(q, 2) = mean(colorBase{q}.A(:));
targetColors(q, 3) = mean(colorBase{q}.B(:));
end
queryImageLab = im2double(queryImageLab);
gridSize = 25;
numRows = size(queryImageLab, 1) / gridSize;
numCols = size(queryImageLab, 2) / gridSize;
% -------------- Find the images matching best for colors --------------
for i = 1:numRows
for j = 1:numCols
% Grid region
replaceRegionRows = (i-1)*gridSize+1 : i*gridSize;
replaceRegionCols = (j-1)*gridSize+1 : j*gridSize;
% LAB in current grid region
L_Region = queryImageLab(replaceRegionRows, replaceRegionCols, 1);
A_Region = queryImageLab(replaceRegionRows, replaceRegionCols, 2);
B_Region = queryImageLab(replaceRegionRows, replaceRegionCols, 3);
currentColor = [mean(L_Region(:)), mean(A_Region(:)), mean(B_Region(:))];
bestMatchIndices = [];
counter = 1;
% Calculate Euclidean distance for each image
for q = 1:200
targetColor = targetColors(q, :);
delta = sqrt((currentColor(1) - targetColor(1)).^2 + (currentColor(2) - targetColor(2)).^2 + (currentColor(3) - targetColor(3)).^2);
distance = mean(delta);
if distance < 20
bestMatchIndices{counter} = q;
counter = counter + 1;
end
end
% Retrieve the corresponding elements from dataBase
bestMatches = [];
for v = 1:length(bestMatchIndices)
bestMatches{v} = dataBase{bestMatchIndices{v}};
end
% -------------------- Decide on structure --------------------
bestMatchesGray = [];
for u = 1:length(bestMatches)
bestMatchesGray{u} = rgb2gray(bestMatches{u});
end
finalReplacement = imread("Test_Images/Skriet.jpg"); % Placeholder img
currentMatchDistance = -1; % Worse result SSIM can generate
queryRegionGray = im2gray(QueyImageRGB(replaceRegionRows, replaceRegionCols));
queryRegionGray = im2double(queryRegionGray);
for k = 1:length(bestMatchesGray)
currentMatch = im2double(bestMatchesGray{k});
currentMatchRGB = im2double(bestMatches{k});
ssimval = ssim(currentMatch, queryRegionGray);
if currentMatchDistance < ssimval
finalReplacement = currentMatchRGB;
currentMatchDistance = ssimval;
end
end
finalReplacementLAB = rgb2lab(finalReplacement);
finalReplacementL = finalReplacementLAB(:,:,1);
finalReplacementA = finalReplacementLAB(:,:,2);
finalReplacementB = finalReplacementLAB(:,:,3);
Pixel_Image = imresize(finalReplacementL, [numel(replaceRegionRows), numel(replaceRegionCols)]);
queryImageLab(replaceRegionRows, replaceRegionCols, 1) = Pixel_Image;
Pixel_Image = imresize(finalReplacementA, [numel(replaceRegionRows), numel(replaceRegionCols)]);
queryImageLab(replaceRegionRows, replaceRegionCols, 2) = Pixel_Image;
Pixel_Image = imresize(finalReplacementB, [numel(replaceRegionRows), numel(replaceRegionCols)]);
queryImageLab(replaceRegionRows, replaceRegionCols, 3) = Pixel_Image;
end
end
rgbImage = lab2rgb(queryImageLab);
end