MATLAB Answers

Aseel H

convert grayscale image to color image

Asked by Aseel H
on 17 Sep 2011

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


I think logically impossible.

You think wrong, probably because he incorrectly told you that he had a grayscale image.

Log in to comment.

2 Answers

Answer by 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.

  1 Comment

Ok, it is wrong from me when used "gray" expression.
but ycbcr2rgb() give error
also i wante split y,cb and cr
thank you very much

Log in to comment.

Answer by 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);
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);
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);
title('Red Channel', 'FontSize', fontSize);
subplot(3, 4, 3);
title('Green Channel', 'FontSize', fontSize);
subplot(3, 4, 4);
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);
title('Y Channel', 'FontSize', fontSize);
subplot(3, 4, 7);
title('Cb Channel', 'FontSize', fontSize);
subplot(3, 4, 8);
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);
title('Reconstructed RGB Image', 'FontSize', fontSize);


Log in to comment.

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today