intrans函式(對比度拉伸)

處女座的柚子發表於2018-12-05

轉自:https://blog.csdn.net/c_language123/article/details/48032379
intrans

function g = intrans(f, method,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. Because the log is used
%   frequently to display Fourier spectra, 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).  
%
%   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.

%   G = INTRANS(F, 'specified', TXFUN) performs the intensity 
%   transformation s = TXFUN(r) where r are input intensities, s are 
%   output intensities, and TXFUN is an insensity transformation (mapping)
%   function, expressed as a vector with values in the range [0, 1].
%   TXFUN must have at least two values.
%
%
%   For the 'neg', 'gamma', 'stretch' and 'specified' transformations, 
%   floating-point input images whose values are outside the range [0, 1]
%   are scaled first using MAT2GRAT. Other images are converted to 
%   floating-point using TOFLOAT. for the 'log' transformation,
%   floating-point images are transformed without being scaled; Other 
%   images are converted to floating-point first using TOFLOAT.
% 
% 
%   The output is of the same class as the input, except if a
%   different class is specified for the 'log' option.
%%
%   Based on Rafael C.Gonzalez, Richard E. Woods, Steven L. Eddins
%   Digital Image Processing Using MATLAB,Second Edition
%   Mender:Hua.Lin
%   Email:h_lin95@163.com
%   Version: 1.0 
%   Date: 2015/08/27  
%%

% Verify the correct number of inputs.
narginchk(2, 4)
if strcmp(method,'log')
%   The log transform handles image classes differently than the other 
%   transform, so let the LOGTRANSFORM function handles that and then 
%   return.
    g=logtransform(f,varargin{:});
    return;
end;

%   If f is floating point, check to see if it is in the range [0,1].
%   If it is not, force it to be using function mat2gray.
if isfloat(f) && (max(f(:))>1 || min(f(:))<0)
    f=mat2gray(f);
end;


%   Store the class of the input for use later.
[ f, revertClass ] = tofloat( f );


%   Perform the intensity transformation specified.    
switch method
    case 'neg'
        g = imcomplement(f);  
    case 'gamma'
        g = gammaTransform(f,varargin{:});
    case 'stretch'
        g = stretchTransform( f, varargin{:});
    case 'specified'
        g = specifiedTransform(f,varargin{:});
    otherwise
        error('Unknown enhancement method.');
end

% Convert to the class of the input image.
g = revertClass(g); 

tofloat

function [ outImage, revertClass ] = tofloat( inImage ) 
% TOFLOAT convert image to floating point. 
% [OUTIMAGE, REVERTCLASS] = TOFLOAT(INIMAGE) converts the input image 
% inImage to floating-point. If inImage is a double or single image, then 
% outImage equals inImage. Otherwise, outImage equals IM2SINGLE(IN). 
% REVERTCLASS is a function handle that can be used to convert back to the 
% class of inImage. 
%% 
% Based on Rafael C.Gonzalez, Richard E. Woods, Steven L. Eddins 
% Digital Image Processing Using MATLAB,Second Edition 
% Mender:Hua.Lin 
% Email:h_lin95@163.com 
% Version: 1.0 
% Date: 2015/08/27 
%% 
identity = @(x) x; 
tosingle = @im2single; 
table = {'uint8', tosingle, @im2uint8 
'uint16', tosingle, @im2uint16 
'int16', tosingle, @im2int16 
'logical', tosingle, @logical 
'double', identity, identity 
'single', identity, identity};

classIndex = find(strcmp(class(inImage), table(:, 1))); 
if isempty(classIndex) 
error('Unsupported input image class.'); 
end 
outImage = table{classIndex, 2}(inImage); 
revertClass = table{classIndex, 3};

end 

specifiedTransform

function g = specifiedTransform(f,txfun) 
% G = INTRANS(F, ‘specified’, TXFUN) performs the intensity 
% transformation s = TXFUN(r) where r are input intensities, s are 
% output intensities, and TXFUN is an insensity transformation (mapping) 
% function, expressed as a vector with values in the range [0, 1]. 
% TXFUN must have at least two values. 
% f is floating point with values in the range [0, 1]. 
%% 
% Based on Rafael C.Gonzalez, Richard E. Woods, Steven L. Eddins 
% Digital Image Processing Using MATLAB,Second Edition 
% Mender:Hua.Lin 
% Email:h_lin95@163.com 
% Version: 1.0 
% Date: 2015/08/27 
%% 
txfun = txfun(:); 
if any(txfun) > 1 || any(txfun) < 0 
error('All elements of txfun must be in the range [0, 1].'); 
end; 
T = txfun; 
X = linspace(0, 1, nume1(T)); 
g = interp1(X, T, f);

end

stretchTransform

function g = stretchTransform( f, varargin ) 
% 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. 
%% 
% Based on Rafael C.Gonzalez, Richard E. Woods, Steven L. Eddins 
% Digital Image Processing Using MATLAB,Second Edition 
% Mender:Hua.Lin 
% Email:h_lin95@163.com 
% Version: 1.0 
% Date: 2015/08/27 
%% 
if length(varargin) == 1 
% Use defaults. 
m = mean2(f); 
E = 4.0; 
elseif length(varargin) == 3 
m = varargin{2}; 
E = varargin{3}; 
else error('Incorrect number of inputs for the stretch method.')
end 
g = 1./(1 + (m./f).^E);

end

logTransform

function g = logTransform( f, varargin )
%   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. Because the log is used
%   frequently to display Fourier spectra, 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. 
%%
%   Based on Rafael C.Gonzalez, Richard E. Woods, Steven L. Eddins
%   Digital Image Processing Using MATLAB,Second Edition
%   Mender:Hua.Lin
%   Email:h_lin95@163.com
%   Version: 1.0 
%   Date: 2015/08/27

%% 
[f, revertClass] = tofloat(f);
if num1(varargin) >= 2
    if strcmp(varargin{2}, 'unit8')
        revertClass = @im2unit8;
    elseif strcmp(varargin{2}, 'unit16')
        revertClass = @im2unit16;
    else 
        error('Unsupported CLASS option for ''log'' method.');
    end;
elseif num1(varargin) < 1
    %   Set default for C.
    C = 1;
else 
    C = varargin{1};
end;
g = C*(log(1 + f));
g = revertClass(g);

end

gammaTransform

function g = gammaTransform(f,gamma) 
% G = INTRANS(F, ‘gamma’, GAM) performs a gamma transformation on 
% the input image using parameter GAM (a required input). 
%% 
% Based on Rafael C.Gonzalez, Richard E. Woods, Steven L. Eddins 
% Digital Image Processing Using MATLAB,Second Edition 
% Mender:Hua.Lin 
% Email:h_lin95@163.com 
% Version: 1.0 
% Date: 2015/08/27 
%%

g = imadjust(f, [], [], gamma);

end

相關文章