DIP10 - MATLAB Example - Utility M-Functions For Intensity Transformations
DIP10 - MATLAB Example - Utility M-Functions For Intensity Transformations
DIP10 - MATLAB Example - Utility M-Functions For Intensity Transformations
switch class
case 'uint8'
image = im2uint8 (varargin{:});
case 'uint16'
image = im2uint16 (varargin {:});
case 'double'
image = im2double(varargin {:});
otherwise
error ('Unsupported IPT data class');
end
An M-function for intensity
transformation
• Note in the following M-function, which we call intrans,
how function operations are formatted in the Help
section of the code, how a variable number of inputs is
handled, how error checking is interleaved in the code,
and how the class of the output image is matched to
the class of the input. Keep in mind when studying the
following code that varargin is a cell array, so its
elements are selected by using curly brackets {}.
An M-function for intensity
transformation
• function g = intrans (f, varargin)
• %INTRANS performs intensity (gray-level) transformations.
• % G = INTRANS (F, 'neg') computes the negative of input image F.
• %
• % G = INTRANS(F, 'log', C, CLASS) computes C*log(1+F) and multiplies
• % the result by (positive) constant C. If the last two parameters are
• % omitted, C defaults to 1. Parameter CLASS offers the option to specify
• % the class of the output as 'uint8' or 'uint16'. if parameter CLASS is
• % omitted, the output is of the same class as the input.
• %
• % G = INTRANS(F, 'gamma', GAM) performs a gamma transformation on
the
• % input image using parameter GAM (a required input).
An M-function for intensity
transformation
• %
• % G = INTRANS(F, 'stretch', M, E) computes a contrast-stretching
• % transformation using the expression 1./(1+ (M./(F + eps)).^E).
• % Parameter M must be in the range [0 1]. the default value for M is
• % mean2 (im2double(F)), and the default value for E is 4
• %
• % For the 'neg', 'gamma' and 'stretch' transformations, double input
• % images whose maximum value is greater than 1 are scaled first using
• % MAT2GRAY. Other images are converted to double first using
IM2DOUBLE.
• % for the 'log' transformation, double images are transfortmed without
• % being scaled;
• %
• % the output is of the same class as the input, except if a different
• % class is specified for the 'log' option.
An M-function for intensity
transformation
• % Verify the correct number of inputs
• error (nargchk (2, 4, nargin)