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

举报
海神之光 发表于 2022/05/29 01:30:04 2022/05/29
【摘要】 一、获取代码方式 获取代码方式1: 完整代码已上传我的资源:【图像转换】基于matlab灰度图像转换彩色图像【含Matlab 1233期】 获取代码方式2: 通过订阅紫极神光博客付费专栏,凭支付凭证,...

一、获取代码方式

获取代码方式1:
完整代码已上传我的资源:【图像转换】基于matlab灰度图像转换彩色图像【含Matlab 1233期】

获取代码方式2:
通过订阅紫极神光博客付费专栏,凭支付凭证,私信博主,可获得此代码。

备注:
订阅紫极神光博客付费专栏,可免费获得1份代码(有效期为订阅日起,三天内有效);

二、部分源代码

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

  
 
  • 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

三、运行结果

在这里插入图片描述

四、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)

文章来源: qq912100926.blog.csdn.net,作者:海神之光,版权归原作者所有,如需转载,请联系作者。

原文链接:qq912100926.blog.csdn.net/article/details/121321238

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。