how do I make a color (rgb) image look grayish?

22 views (last 30 days)
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?

Accepted Answer

Image Analyst
Image Analyst on 12 Jul 2013
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);
  3 Comments
Image Analyst
Image Analyst on 12 Jul 2013
Here's another, different demo:
% Demo to cycle through a range of saturation
% on an image from extra vivid to grayscale.
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);
% Extract saturation component.
s = hsv(:, :, 2);
% Go from extra vivid to gray
for desaturationFactor = 2 : -0.2 : 0
% Multiply original s channel by the desaturationFactor.
hsv(:, :, 2) = s * desaturationFactor;
% Convert back to RGB for display.
rgbImage2 = hsv2rgb(hsv);
subplot(2, 1, 2);
cla;
imshow(rgbImage2);
caption = sprintf('Altered Saturation Image\nSaturation changed by a factor of %.1f', desaturationFactor);
title(caption, 'FontSize', fontSize);
message = sprintf('Do you want to continue');
button = questdlg(message, 'Continue?', 'Yes', 'No', 'Yes');
drawnow; % Refresh screen to get rid of dialog box remnants.
if strcmpi(button, 'No')
break;
end
end
msgbox('Done with demo');
David
David on 5 Aug 2013
that was perfect. sorry for the delay. thank you!

Sign in to comment.

More Answers (1)

Chris E.
Chris E. on 11 Jul 2013
You can change the RGB from the desired color to gray by playing with the background set of an object. This is kind of code you may end up using:
% type guide, then make a text box and a button. Name the text box (the tag) "text" and the button (again the tag) "pb". Press save and then exit guide. Then add this code beneath to the button callback, similar to how it is shown here:
% --- Executes on button press in pb.
function pb_Callback(hObject, eventdata, handles)
r = 0;
g = 0;
b = 0;
for c = 1:100
r = c/100;
g = c/100;
b = c/100;
set(handles.text1, 'background',[r g b])
pause(0.1)
end
(gray is about r = 0.702, g = 0.702, b = 0.702.) Well this will do a fade or wipe type run through colors to show from black to white (goes through gray). This is at least a start or a help to get you started, however the question did not specify if it was in a GUI or a plot or where ever this was being done in or at. So I guess that is the best I can do for you. I hope it helps!
  2 Comments
David
David on 11 Jul 2013
thank you for the fast reply and the code. There is no gui here, I just want to make image frames of a color image going from color to gray. I'll try to look over what you've put, i'm just not very familiar with GUI coding. thanks! veritas
Chris E.
Chris E. on 11 Jul 2013
Edited: Chris E. on 11 Jul 2013
Hello once more,
Well try this code out, just as is, goes through all colors then from black to white:
figure('Color',[0.8 0.8 0.8]);
for x = 1:10
for y = 1:10
for z = 1:10
r = x/10;
g = y/10;
b = z/10;
set(gca,'Color',[r g b]);
pause(0.1)
drawnow
end
end
end
for x = 1:10
c = x/10;
set(gca,'Color',[c c c]);
pause(0.1)
drawnow
end
Hope that helps!

Sign in to comment.

Categories

Find more on Image Processing Toolbox 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!