convert grayscale image to color image
5 views (last 30 days)
Show older comments
How is convert grayscale image to color image As: color = imread('img'); Gray = rgb2ycbcr(color); I want the inverse of this function : how inverse (ycbcr to color). Thank you very much
2 Comments
Image Analyst
on 17 Sep 2011
You think wrong, probably because he incorrectly told you that he had a grayscale image.
Answers (2)
Image Analyst
on 17 Sep 2011
What was wrong with the ybcr2rgb() function given in the documentation for rgb2ybcr()?
By the way, your subject line and code are wrong. rgb2ybcr produces an MxNx3 image array which is the image in the YCbCr color space. It does not produce a gray image.
Also, be careful about using built-in function names for variables. Even though MATLAB is case sensitive, it's not good to use names like "Gray" (or Sum, Mean, I, etc.) for your own variables because one slip of the shift key and you've overwritten a built-in function.
Image Analyst
on 18 Sep 2011
Well you did something wrong if you say ycbcr2rgb() gave you an error. I did it and it works fine. Try my demo:
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 15;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(3, 4, 1);
imshow(rgbImage);
title('Original color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Display the individual red, green, and blue color channels.
subplot(3, 4, 2);
imshow(redChannel);
title('Red Channel', 'FontSize', fontSize);
subplot(3, 4, 3);
imshow(greenChannel);
title('Green Channel', 'FontSize', fontSize);
subplot(3, 4, 4);
imshow(blueChannel);
title('Blue Channel', 'FontSize', fontSize);
% Convert to YCbCr
ycbcrImage = rgb2ycbcr(rgbImage);
% Extract the individual Y, Cb, and Cr color channels.
YChannel = ycbcrImage(:, :, 1);
CbChannel = ycbcrImage(:, :, 2);
CrChannel = ycbcrImage(:, :, 3);
% Display the individual Y, Cb, and Cr color channels.
subplot(3, 4, 6);
imshow(YChannel);
title('Y Channel', 'FontSize', fontSize);
subplot(3, 4, 7);
imshow(CbChannel);
title('Cb Channel', 'FontSize', fontSize);
subplot(3, 4, 8);
imshow(CrChannel);
title('Cr Channel', 'FontSize', fontSize);
% Reconstruct the RGB image from the individual Y, Cb, and Cr color channels.
reconRGBImage = ycbcr2rgb(ycbcrImage);
subplot(3, 4, 11);
imshow(reconRGBImage);
title('Reconstructed RGB Image', 'FontSize', fontSize);
0 Comments
See Also
Categories
Find more on Blue in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!