【图像转换】灰度图像转换彩色图像【含Matlab 1233期】

⛄一、获取代码方式

获取代码方式1:
完整代码已上传我的资源:【图像转换】基于matlab灰度图像转换彩色图像【含Matlab 1233期】
(https://download.csdn.net/download/TIQCmatlab/62925370)
点击上面蓝色字体,直接付费下载,即可。

获取代码方式2:
付费专栏Matlab图像处理(初级版)

备注:
点击上面蓝色字体付费专栏Matlab图像处理(初级版),扫描上面二维码,付费29.9元订阅海神之光博客付费专栏Matlab图像处理(初级版),凭支付凭证,私信博主,可免费获得1份本博客上传CSDN资源代码(有效期为订阅日起,三天内有效);
点击CSDN资源下载链接:1份本博客上传CSDN资源代码

⛄二、部分源代码

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版本及参考文献

1 matlab版本
2014a

2 参考文献
[1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020.
[2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013.
[3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013.
[4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.

3 备注
简介此部分摘自互联网,仅供参考,若侵权,联系删除

  • 17
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Matlab领域

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值