一、獲取代碼方式
獲取代碼方式1:
完整代碼已上傳我的資源:【影像轉換】基于matlab灰度影像轉換彩色影像【含Matlab 1233期】
獲取代碼方式2:
通過紫極神光博客主頁開通CSDN年度會員,憑支付憑證,私信博主,可獲得此代碼,
獲取代碼方式3:
通過訂閱紫極神光博客付費專欄,憑支付憑證,私信博主,可獲得此代碼,
備注:開通CSDN年度會員,僅只能免費獲得1份代碼(有效期為開通日起,三天內有效);
訂閱紫極神光博客付費專欄,可免費獲得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
三、運行結果

四、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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/357219.html
標籤:其他
上一篇:JavaCV調整影像陰影
下一篇:YOLOV3訓練環境的搭建
