一、获取代码方式

 

二、部分源代码

tic
rslt = gray2rgb('test1_destination.jpg','test1_source.jpg');
gray = imread('test1_destination.jpg');
color = imread('test1_source.jpg');

figure
subplot(1,3,1); imshow(uint8(gray)); title('gray image');
subplot(1,3,2); imshow(uint8(color)); title('color source image');
subplot(1,3,3); imshow(uint8(rslt)); title('colored image');
toc
function R=gray2rgb(dest,src)
%gray2rgb converts a gray image to RGB based on the colors of the source
%image
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function converts a gray image to RGB based on the colors of the
% source image.
%
% R = gray2rgb(dest, src)
% dest - destination or target (grayscale) image that you want to color
% src - source (color image) that you want to use as a color pallet
%
% You can use the attached test images. Use the following combinations:
% gray2rgb('test1_destination.jpg', 'test1_source.jpg')
% gray2rgb('nature_desitnation.jpg', 'nature_source.jpg')
%
% This code was originally inspired by the code gray2rgb by Jeny Rajan and
% Chandrashekar P.S. The code was optimized and rewritten to more closely
% achieve what was described in the paper "Transfering Color to Grayscale
% Images" by Welsh, Ashikhmin and Mueller. Identical results to Rajan's
% code are achieved much more quickly, especially for large images.
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

imt = imread(dest); % read target image
ims = imread(src); % read source image
[tx, ty, tz] = size(imt); % get size of target image
[~, ~, sz] = size(ims); % get 3rd dim of source
if tz ~= 1 % convert the destination image to grayscale if not already
imt = rgb2gray(imt);
end
if sz ~= 3 % check to see that the source image is RGB
disp ('img2 must be a color image (not indexed)');
else
imt(:, :, 2) = imt(:, :, 1); % add green channel to grayscale img
imt(:, :, 3) = imt(:, :, 1); % add blue channel to grayscale img

% Converting to ycbcr color space
% ycbcr, y: luminance, cb: blue difference chroma, cr: red difference chroma
% s - source, t - target
nspace1 = rgb2ycbcr(ims); % convert source img to ycbcr color space
nspace2 = rgb2ycbcr(imt); % convert target img to ycbcr color space

% Get unique values of the luminance
[ms, ics, ~] = unique(double(nspace1(:, :, 1))); % luminance of src img
mt = unique(double(nspace2(:, :, 1))); % luminance of target img
% Establish values for the cb and cr content from the source
% image
cbs = nspace1(:, :, 2);
cbs = cbs(ics);
crs = nspace1(:, :, 3);
crs = crs(ics);

% get max and min luminance of src and target
m1 =max(ms);
m2 = min(ms);
m3 = max(mt);
m4 = min(mt);
d1 = m1 - m2; % get difference between max and min luminance
d2 = m3 - m4;
% Normalization
dx1 = ms;
dx2 = mt;
dx1 = (dx1 * 255) / (255 - d1); % normalize source
dx2 = (dx2 * 255) / (255 - d2); % normalize target
[mx, ~] = size(dx2);
% luminance and normalization of target image
nimage_norm = double(nspace2(:, :, 1));
nimage_norm =(nimage_norm * 255) / (255 - d2);

% Luminance Comparison
nimage = nspace2;


% reshape cb and cr channels to be column vector
nimage_cb = reshape(nimage_cb, numel(nimage_cb), 1);
nimage_cr = reshape(nimage_cr, numel(nimage_cr), 1);

% CHANGE: Loop through dx2 luminance values and find location of
% corresponding luminance values in nimage_norm. Assign cb and cr
% values to nimage's cb and cr channels for matching values

for i = 1:mx
iy = dx2(i);
tmp = abs(dx1 - iy); % calculate absolute difference between
% specific normalized target luminance value and normalized
% source luminance values

% finds min value of absolute diff. between specific
% normalized target luminance value and normalized source
% luminance values
r = find(tmp == ck); % finds row and column where tmp = ck

mtch = find(nimage_norm == iy); % find linear indicies of matching
% luminance values
nimage_cb(mtch) = cb(1); % set cb values based on matching lum vals
nimage_cr(mtch) = cr(1); % set cr values based on matching lum vals
end

三、运行结果

【图像转换】基于matlab灰度图像转换彩色图像【含Matlab 1233期】_matlab

四、matlab版本及参考文献

1 matlab版本

2014a

2 参考文献

[1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020.

[2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013.

[3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013.

[4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.

[5]梁东云,吴晓云,刘萌.基于MATLAB的数字图像加密研究[J].系统仿真技术. 2020,16(04)