Skip to content

Instantly share code, notes, and snippets.

@jordanwesthoff
Created March 18, 2015 18:25
Show Gist options
  • Save jordanwesthoff/662c53c30d9b283f51a3 to your computer and use it in GitHub Desktop.
Save jordanwesthoff/662c53c30d9b283f51a3 to your computer and use it in GitHub Desktop.
MATLAB based saturation map function, REC.601 implementation
function custom_saturation(input_image, coefficient)
% USAGE
% Run the function with an input image as a string and with a coefficient
% for the level of saturation to apply to the image. Some common usages
% are:
%
% custom_saturation('panda.jpg', 0) -> This will entirely desaturate the
% image, panda.jpg
% custom_saturation('panda.jpg', 1) -> This will not modify the image sat.
% custom_saturation('panda.jpg', 200) -> This will totally oversaturate the
% image by %200
% Jordan Westhoff, RIT 2014
% Read in the image and ensure it is a double.
im = imread(input_image);
im = im2double(im);
% Find the size of the image to be filtered and saturated.
xyz = size(im);
numRows = xyz(1);
numColumns = xyz(2);
%REC.601 R'G'B -> Y'CbCr Matrix
Ycbcr_601 = [ 00.299 00.587 0.114;
-0.299 -0.587 0.886;
00.701 -0.587 -0.114];
% Convert from R'G'B to Y'CbCr
im = Ycbcr_601 * reshape(im, numRows * numColumns, 3)';
% For concisenes sake, append the input to make a better looking matrix.
coef = coefficient;
% Build the coefficient matrix based on the user supplied input.
co_mat = [1 0 0;
0 coef 0;
0 0 coef];
% Using the matrix, scale and saturate the image based on the coefficients.
P = co_mat * im;
% Revert all of the color space changes; Y'CbCr -> R'G'B
P = minv(Ycbcr_601) * P;
% Reshape all of the strings back into a MxNx3 matrix (image)
im_new(:,:,1) = reshape(P(1,:), numRows, numColumns);
im_new(:,:,2) = reshape(P(2,:), numRows, numColumns);
im_new(:,:,3) = reshape(P(3,:), numRows, numColumns);
% View the image
imshow(im_new)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment