clear all %经典的Reinhard颜色迁移算法
close all
clc
input = im2double(imread('p5.jpg'));
target = im2double(imread('p8.jpg'));
input_temp = zeros(3,1); %创建一个三行一列的零矩阵
target_temp = zeros(3,1);
thresh = -10;
% rgb2lab = makecform('srgb2lab');
% lab2rgb = makecform('lab2srgb');
a = 1/sqrt(3);
b = 1/sqrt(6);
c = 1/sqrt(2);
%transformation matrices 变换矩阵
rgb2lms_ratio = [0.3811 0.5783 0.0402; 0.1967 0.7244 0.0782; 0.0241 0.1288 0.8444];
lms2rgb_ratio = [4.4679 -3.5873 0.1193; -1.2186 2.3809 -0.1624; 0.0497 -0.2439 1.2045];
lms2lab_Ratio = [a 0 0; 0 b 0; 0 0 c]*[1 1 1; 1 1 -2; 1 -1 0];
lab2lms_Ratio = [1 1 1; 1 1 -1; 1 -2 0]*[a 0 0; 0 b 0; 0 0 c];
%converting RGB color space to uncorelated lab color space for the source 将RGB转化为lab
[m n k] = size(input);
in_lab = zeros(m,n,k);
% in_lab = applycform(input,rgb2lab);
for i = 1 : 1 : m
for j = 1 : 1 : n
input_temp(1,1) = double(input(i,j,1));
input_temp(2,1) = double(input(i,j,2));
input_temp(3,1) = double(input(i,j,3));
input_temp = rgb2lms_ratio*input_temp;
input_temp = log10(input_temp);
input_temp = lms2lab_Ratio*input_temp;
ind1 = find(isnan(input_temp));
ind2 = find(input_temp < thresh);
input_temp(ind1)= thresh;
input_temp(ind2)= thresh;
in_lab(i,j,:) = input_temp;
end
end
%calculating mean and standard deviation for the source separately for each
%channel 为每个通道分别计算源的平均值和标准偏差
mean_l_in = mean(mean(in_lab(:,:,1)));
mean_a_in = mean(mean(in_lab(:,:,2)));
mean_b_in = mean(mean(in_lab(:,:,3)));
sig_l_in = sqrt(var(double(reshape(in_lab(:,:,1),m*n,1))));
sig_a_in = sqrt(var(double(reshape(in_lab(:,:,2),m*n,1))));
sig_b_in = sqrt(var(double(reshape(in_lab(:,:,3),m*n,1))));
%converting RGB color space to uncorelated lab color space for the target
[m n k] = size(target);
tar_lab = zeros(m,n,k);
% tar_lab = applycform(target,rgb2lab);
for i = 1 : 1 : m
for j = 1 : 1 : n
target_temp(1,1) = double(target(i,j,1));
target_temp(2,1) = double(target(i,j,2));
target_temp(3,1) = double(target(i,j,3));
target_temp = rgb2lms_ratio*target_temp;
target_temp = log10(target_temp);
target_temp = lms2lab_Ratio*target_temp;
ind1 = find(isnan(target_temp));
ind2 = find(target_temp < thresh);
target_temp(ind1)= thresh;
target_temp(ind2)= thresh;
tar_lab(i,j,:) = target_temp;
end
end
%calculating mean and standard deviation for the target separately for each
%channel
mean_l_tar = mean(mean(tar_lab(:,:,1)));
mean_a_tar = mean(mean(tar_lab(:,:,2)));
mean_b_tar = mean(mean(tar_lab(:,:,3)));
sig_l_tar = sqrt(var(double(reshape(tar_lab(:,:,1),m*n,1))));
sig_a_tar = sqrt(var(double(reshape(tar_lab(:,:,2),m*n,1))));
sig_b_tar = sqrt(var(double(reshape(tar_lab(:,:,3),m*n,1))));
%perfomring color transfer as color process in lab color space
[m n k] = size(input);
% lab_res = zeros(m,n,k);
lab_res = in_lab;%结构图象lab空间的值赋给lab_res
lab_res(:,:,1) = in_lab(:,:,1) - mean_l_in;
lab_res(:,:,2) = in_lab(:,:,2) - mean_a_in;
lab_res(:,:,3) = in_lab(:,:,3) - mean_b_in;
lab_res(:,:,1) = (sig_l_tar/sig_l_in).*lab_res(:,:,1);
lab_res(:,:,2) = (sig_a_tar/sig_a_in).*lab_res(:,:,2);
lab_res(:,:,3) = (sig_b_tar/sig_b_in).*lab_res(:,:,3);
lab_res(:,:,1) = lab_res(:,:,1) + mean_l_tar;
lab_res(:,:,2) = lab_res(:,:,2) + mean_a_tar;
lab_res(:,:,3) = lab_res(:,:,3) + mean_b_tar;
for i = 1:1:m
for j = 1:1:n
for l = 1:1:k
if isnan(lab_res(i,j,l))
lab_res(i,j,l) = in_lab(i,j,l);
end
end
end
end
%converting the result back to RGB color space to display 将结果转换回RGB颜色空间以显示
[m n k] = size(input);
fin_res = zeros(m,n,k);
% fin_res = applycform(lab_res,lab2rgb);
for i = 1 : 1 : m
for j = 1 : 1 : n
input_temp(1,1) = double(lab_res(i,j,1));
input_temp(2,1) = double(lab_res(i,j,2));
input_temp(3,1) = double(lab_res(i,j,3));
input_temp = lab2lms_Ratio*input_temp;
input_temp = 10.^input_temp;
fin_res(i,j,:) = lms2rgb_ratio*input_temp;
end
end
figure
subplot(1,3,1);
imshow(input);
subplot(1,3,2);
imshow(target);
subplot(1,3,3);
imshow(uint8(fin_res));
评论0