matlab 数字图像处理 intrans函数 学习笔记

intrans函数如下:
function g = intrans(f,varargin)

error (nargchk(2,4,nargin))
%check input
classin = class(f);
%stroe the class of the input for use later.
if strcmp(class(f),'double') & max(f(:))>1 & ~strcmp(varargin{1},'log')
    f = mat2gray(f);
%if all the 3 conditions is filling the need .
else 
% make sure the class(f) is in the class of double , f(:) means all the
% elemnets in the martix F, and the max(f(:))>1 means if the max(f(:))>1 so
% convert them into double , in this way they are all less then1.        
% strcmp(varargin[1],'log') is the string compare, and the varargin {1} 
% compares with log. 
    f = im2double(f);
end
method = varargin{1};

switch method
    case 'neg'
        g = imcomplement(f);
    case  'log'
        if length(varargin) == 1
            c = 1;
        elseif length(varargin) == 2
            c = varargin{2};
        elseif length(varargin) == 3
            c = varargin{2};
            classin = varargin{3};
        else
            error('Incorrect number of input for the log option.')
        end
        g = c*(log(1+double(f)));
    case  'gamma'
        if length(varargin) < 2
            error('not enough input for the gamma option')
        end
        gam = varargin{2};
        g = imadjust (f, [], [], gam);
    case 'stretch'
        if length(varargin) == 1
            %defaults vaule
            m = mean2(f);
            E = 4.0;
        elseif length(varargin) == 3
            m = varargin{2};
            E = varargin{3};
        else error('incorrect number of inputs for the srtetch option.')
        end
        g = 1./(1 + (m./(f+eps)).^E);
    otherwise
        error('unkown enhancement method.')
end
  %     g = changeclass(classin , g);
 

说实话,对matlab的代码风格不是很习惯,后来看着看着感觉和才非常相似,也就看起来舒服多了

这段代码刚开始没看懂,尤其是varargin{}的一直不明白,但加断点后,将整个程序跑了一遍,基本就明白了,这里不得不说,中文版书上将的不是很明白。
在书上的例子中,matlab里面输入的代码是>>g = intrans(f,log,mean2(im2double(f)),0.9);

我们看到代码中,if strcmp(class(f),'double') & max(f(:))>1 & ~strcmp(varargin{1},'log') f = mat2gray(f);

首先对输入的数据类型检测,if strcmp(class(f),'double') 是检测输入的矩阵是否是double型的;

其次对输入的数据大小检测,max(f(:))>1 是确定矩阵f中是否有大于1的元素;

最后对输入的数据的varargin的第一项是否为log。

插入断点后,打开varargin这个矩阵,读出的数据为:01 对比输入的命令,就可以发现输入的log出现这第一个数据位,0.5181对f矩阵做的均方根的值。

然后到了 method = varargin{1}; 就是把varargin这个矩阵里面的第一个值(因为他是一个1X3的矩阵)于case里面的众多数据进行对比,如果一直,就执行其中代码,后面和C就没有什么区别了。
classin = class(f); 是将f的类型赋给classin;后面还有个changeclass的函数是转换数据类型的,一般主要是用于数据类型转换,例如unit8, unit16,double等类型。

 
原文地址:https://www.cnblogs.com/zhongnanshan/p/1810167.html