[Saturation] Saturate or Desaturate an image in Matlab
This function written in Matlab lets you change the saturation of an image in Matlab. You can saturate the image by setting the factor >1, and if you want to desaturate the image in matlab, you set the factor to less than 2. If you want no saturation at all, you can set it to 0.
Assumptions
- The image is in the sRGB color profile (standard for web)
- The image is in RGB 8 bit (uint8) format
Methodology
I thought it would be best to convert the sRGB image to L*A*B* (CIE 1976). Then it decreases the a and b components by the factor.
Notes: i have not optimized this function at all.
function rgb_result=saturate(rgb,factor)
%Saturates of Desaturates an image
% J=saturate(I,factor) returns an image J that is factor times the
% saturation of 8bit color image I. So 0 is no saturation, 2 is double
% andsoforth. You get the idea. Otherwise visit me at: www.timzaman.nl
%
% Examples
% --------
% Removes all saturation
%
% I = imread('rice.png');
% J = saturate(I,0);
% figure, imshow(I), figure, imshow(J)
%
% Doubles the saturation
%
% I = imread('rice.png');
% J = saturate(I,2);
% figure, imshow(I), figure, imshow(J)
%
%
% Note
% ----
% This is the first version without much support
%
% Class Support
% -------------
% The input image rgb should be 8bit unsigned
%
% See also makecform, applycform
% Written by Tim Zaman, TU Delft, 2011
% This work, unless otherwise expressly stated, is licensed under a
% Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
% Obviously provided "AS-IS"
rgblab=makecform('srgb2lab');
labrgb=makecform('lab2srgb');
lab = applycform(rgb,rgblab);
lab=int16(lab);
lab=lab-128;
lab(:,:,2)=lab(:,:,2)*factor;
lab(:,:,3)=lab(:,:,3)*factor;
lab=uint8(lab+128);
rgb_result = applycform(lab,labrgb);
end