MATLAB: How to make a color (rgb) image look grayish

Image Processing Toolboxrgb to gray gradual gradientsaturation

how do I make a color (rgb) image look grayish? not grayscale per se, but iteratively "washout" the color? I basically want to make a sequence of images that go from color to gray. any ideas?

Best Answer

You vary the saturation channel. See this demo:
% Demo to very the saturation on an image.
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';
% 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(2, 1, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
% Give a name to the title bar.
set(gcf,'name','Salt and Pepper Noise Removal Demo','numbertitle','off')
% Convert to hsv.
hsv = rgb2hsv(rgbImage);
% Desaturate. Change this factor to vary the amound of desaturation.
desaturationFactor = 0.2;
% Ask user for a number.
defaultValue = 0.2;
titleBar = 'Enter a value ( 0 - 3)'; % Any upper limit.
userPrompt = 'Enter the integer';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
desaturationFactor = str2double(cell2mat(caUserInput));
hsv(:, :, 2) = hsv(:, :, 2) * desaturationFactor;
% Convert back to RGB for display.
rgbImage2 = hsv2rgb(hsv);
subplot(2, 1, 2);
imshow(rgbImage2);
title('Desaturated Color Image', 'FontSize', fontSize);