数字图像处理MATLAB实现(第2版)冈萨雷斯 书中代码-2.1 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); 
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

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`

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`

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

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
“`

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
### 回答1: 冈萨雷斯 数字图像处理 matlab 第二是一本关于数字图像处理的权威经典教材,它全面讲解了数字图像处理领域的理论知识、算法和工具。这本书的作者是Richard E. Gonzalez和Richard E. Woods,他们的专业背景和多年的教学经验保证了这本书的质量和实用性。 这本书的第二提供了更多的案例和实验,可以帮助读者更好地理解和应用数字图像处理技术。此外,这本书更加注重与MATLAB的结合,使用MATLAB代码示例和工具箱来帮助读者实现数字图像处理算法。这种结合让读者不仅可以学习数字图像处理的理论知识,还可以亲自动手实践并掌握如何使用MATLAB进行数字图像处理。 总之,冈萨雷斯 数字图像处理 matlab 第二是一本非常好的数字图像处理教材,它适合作为本科和硕士生数字图像处理课程的教材,同时也是从事数字图像处理相关工作和研究的专业人士必备的参考书。 ### 回答2: 《冈萨雷斯 数字图像处理 matlab. 第二》是一本介绍数字图像处理理论和实践的书籍,使用的软件为Matlab。本书是数字图像处理领域的经典之作,既有深厚的理论知识,又有实用的工具与技巧,适合从事数字图像处理和计算机视觉研究的学生和专业人员。 本书内容涵盖了数字图像处理的基本概念、灰度变换、空间域滤波、频域滤波、图像分割、图像恢复、形态学处理、特征提取和图像识别等方面的知识。每一章节都有大量实例和Matlab代码,方便读者理解和实践。 本书的第二相对于第一增加了很多新内容,例如分量处理、彩色图像处理、小波变换、图像压缩等方面。同时,本也使用了Matlab最新的本和工具箱,如图像处理工具箱、计算机视觉工具箱等,使读者能够更好地进行实践操作。 总之,《冈萨雷斯 数字图像处理 matlab. 第二》是一本经典的数字图像处理教材,在理论、实践和应用方面都有很高的参考价值。它不仅适合数字图像处理专业的学生和研究人员,同时也适合从事计算机、电子、通信等相关领域的专业人员。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值