[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.

  1. function rgb_result=saturate(rgb,factor)
  2. %Saturates of Desaturates an image
  3. %   J=saturate(I,factor) returns an image J that is factor times the
  4. %   saturation of 8bit color image I. So 0 is no saturation, 2 is double
  5. %   andsoforth. You get the idea. Otherwise visit me at: www.timzaman.nl
  6. %
  7. %   Examples
  8. %   --------
  9. %   Removes all saturation
  10. %
  11. %       I = imread('rice.png');
  12. %       J = saturate(I,0);
  13. %       figure, imshow(I), figure, imshow(J)
  14. %
  15. %   Doubles the saturation
  16. %
  17. %       I = imread('rice.png');
  18. %       J = saturate(I,2);
  19. %       figure, imshow(I), figure, imshow(J)
  20. %
  21. %
  22. %   Note
  23. %   ----
  24. %   This is the first version without much support
  25. %
  26. %   Class Support
  27. %   -------------
  28. %   The input image rgb should be 8bit unsigned
  29. %
  30. %   See also makecform, applycform
  31. %   Written by Tim Zaman, TU Delft, 2011
  32. %   This work, unless otherwise expressly stated, is licensed under a
  33. %   Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  34. %   Obviously provided "AS-IS"
  35.  
  36. rgblab=makecform('srgb2lab');
  37. labrgb=makecform('lab2srgb');
  38. lab = applycform(rgb,rgblab);
  39. lab=int16(lab);
  40. lab=lab-128;
  41. lab(:,:,2)=lab(:,:,2)*factor;
  42. lab(:,:,3)=lab(:,:,3)*factor;
  43. lab=uint8(lab+128);
  44. rgb_result = applycform(lab,labrgb);
  45.  
  46. end

Tim Zaman

MSc Biorobotics. Specialization in computer vision and deep learning. Works at NVIDIA.

You may also like...