-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReproduction.m
80 lines (51 loc) · 2.63 KB
/
Reproduction.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
function [rgbImage] = Reproduction(queryImageLab)
load colorBase.mat colorBase
disp('Calculating reproduction image on colorspace...');
sizeDatabase = numel(colorBase);
% ----------------- Colors in LAB for all the regions ------------------
targetColors = zeros(200, 3);
% Target colors in all the regions
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);
% -------------- Find the images matching best for colors --------------
gridSize = 25; % Grid size
numRows = size(queryImageLab, 1) / gridSize;
numCols = size(queryImageLab, 2) / gridSize;
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(:))];
currentDiff = inf;
bestMatchIndex = 1;
% Calculate Euclidean distance for each image
for q = 1:sizeDatabase
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 < currentDiff
bestMatchIndex = q;
currentDiff = distance;
end
end
% ------------------------- Place the image -----------------------------
% Resize image based on region size and place the small image by LAB channels
Pixel_Image = imresize(colorBase{bestMatchIndex}.L, [numel(replaceRegionRows), numel(replaceRegionCols)]);
queryImageLab(replaceRegionRows, replaceRegionCols, 1) = Pixel_Image;
Pixel_Image = imresize(colorBase{bestMatchIndex}.A, [numel(replaceRegionRows), numel(replaceRegionCols)]);
queryImageLab(replaceRegionRows, replaceRegionCols, 2) = Pixel_Image;
Pixel_Image = imresize(colorBase{bestMatchIndex}.B, [numel(replaceRegionRows), numel(replaceRegionCols)]);
queryImageLab(replaceRegionRows, replaceRegionCols, 3) = Pixel_Image;
end
end
rgbImage = lab2rgb(queryImageLab); % Convert back to display
end