Matlab數字圖像亮度變換函數

摘自《數字圖像處理》 岡薩雷斯
一、處理可變數量的輸入和輸出
利用nargin和nargout進行檢測
T = testhv(4, 5)
該函數體中使用nargin返回2,使用nargout返回1.
函數nargchk可用於一個M函數體中,以檢測傳遞函數的參量數目是否正確。
msg = nargchk(low, high, number)
參量number介於low與high之間返回空矩陣,否則返回錯誤信息。通常可這樣使用:
function G = testhv2(x, y, z)
...
error(nargchk(2, 3, nargin));
...
鍵入僅有一個輸入變量的語句
testhv2(6);
將產生錯誤消息
Not enough input arguments
同時執行終止。
通常,寫出具有可變數目的輸入變量和輸出變量的函數時十分有用的。這裏,我們使用變量varargin和變量varargout。varargin和varargout鼻血使用小寫形式。例如,
function [m, n] = testhv3(varargin)
將輸入的變量數讀取到函數testhv3中,而
function [varargout] = testhv4(m, n, p)
則通過函數testhv4返回輸出的變量數。若函數testhv3有一個固定的輸入變量x,後跟輸入變量的可變數目,則調用
function [m, n] = testhv3(x, varargin)
函數時,會導致varargin由用戶提供的第二個輸入變量開始運行。varargout的情形與此類似。一個函數的輸入變量和輸出變量的個數是可變的。
當varargin用做一個函數的輸入變量時,Matlab會將其置入一個單元數組中,該數組接受由用戶輸入的變量數。由於varargin是一個單元數組,所以此類配置的一個重要方面是對函數的調用可包括輸入的混合集。例如,若我們要使用假設函數testhv3的代碼來處理此項操作,則它能很好的接受輸入的混合集,如
[m, n] = testhv3(f, [0 0.5 1.5], A, 'label');
其中,f是一幅圖像,下一個變量時一個長度爲3的行向量,A是一個矩陣,'label'是一個字符串。
二、亮度變換的另一個M函數
在這一節中,我們將開發一個計算如下變換功能的函數:負片變換、對數變換、gamma變換和對比度拉伸變換。選用這些變換是因爲隨後我們將用到它們。此外,我們將進一步說明編寫亮度變換M函數所涉及的機理。在編寫該函數時,我們將用到函數changeclass,其語法爲:
g = changeclass(newclass, f)
function image = changeclass(class, varargin)
%CHANGECLASS changes the storage class of an image.
%  I2 = CHANGECLASS(CLASS, I);
%  RGB2 = CHANGECLASS(CLASS, RGB);
%  BW2 = CHANGECLASS(CLASS, BW);
%  X2 = CHANGECLASS(CLASS, X, 'indexed');
 
%  Copyright 1993-2002 The MathWorks, Inc.  Used with permission.
%  $Revision: 1.2 $  $Date: 2003/02/19 22:09:58 $
 
switch class
case 'uint8'
   image = im2uint8(varargin{:});
case 'uint16'
   image = im2uint16(varargin{:});
case 'double'
   image = im2double(varargin{:});
otherwise
   error('Unsupported IPT data class.');
end
此函數將圖象f轉換成有參數newclass指定的類別,並輸出圖像g。newclass的有效值是'uint8', 'uint16', 和'double'。
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. 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.
%
%   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
%   transformed without being scaled; other images are converted to
%   double first using IM2DOUBLE.
%
%   The output is of the same class as the input, except if a
%   different class is specified for the 'log' option.
 
%   Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
%   Digital Image Processing Using MATLAB, Prentice-Hall, 2004
%   $Revision: 1.7 $  $Date: 2003/10/13 00:45:53 $
 
% Verify the correct number of inputs.
error(nargchk(2, 4, nargin))
 
% Store the class of the input for use later.
classin = class(f);
 
% If the input is of class double, and it is outside the range
% [0, 1], and the specified transformation is not 'log', convert the
% input to the range [0, 1].
if strcmp(class(f), 'double') & max(f(:)) > 1 & ...
      ~strcmp(varargin{1}, 'log')
   f = mat2gray(f);
else % Convert to double, regardless of class(f).
   f = im2double(f);
end
 
% Determine the type of transformation specified.
method = varargin{1};
 
% Perform the intensity transformation specified.   
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 inputs for the log option.')
   end
   g = c*(log(1 + double(f)));
 
case 'gamma'
   if length(varargin) < 2
      error('Not enough inputs for the gamma option.')
   end
   gam = varargin{2};
   g = imadjust(f, [ ], [ ], gam);
 
case 'stretch'
   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 option.')
   end
   g = 1./(1 + (m./(f + eps)).^E);
otherwise
   error('Unknown enhancement method.')
end
 
% Convert to the class of the input image.
g = changeclass(classin, g);
例3.3 函數intrans的說明
要說明函數intrans,我們可以利用下面的例子,左邊爲原始圖,右邊爲經對比度拉伸後的圖像。
>> f = imread('Fig0306(a)(bone-scan-GE).tif');
>> figure(1)
>> imshow(f)
>> g = intrans(f, 'stretch', mean2(im2double(f)), 0.9);
>> figure(2)
>> imshow(g)
Matlab數字圖像亮度變換函數
三、亮度標度的M函數
當處理圖像是,象素值域由負到正的現象是很普遍的。儘管在中間計算過程中沒有問題,但當我們想利用8bit或16bit格式保存或查看一幅圖像時,就會出現問題。在這種情況下,我們通常希望吧圖像標度在全尺度,即最大範圍[0, 255]或[0, 65535]。下列名爲gscale的M函數可以實現此功能。此外,此函數能將輸出映射到一個特定的範圍。
function g = gscale(f, varargin)
%GSCALE Scales the intensity of the input image.
%   G = GSCALE(F, 'full8') scales the intensities of F to the full
%   8-bit intensity range [0, 255].  This is the default if there is
%   only one input argument.
%
%   G = GSCALE(F, 'full16') scales the intensities of F to the full
%   16-bit intensity range [0, 65535].
%
%   G = GSCALE(F, 'minmax', LOW, HIGH) scales the intensities of F to
%   the range [LOW, HIGH]. These values must be provided, and they
%   must be in the range [0, 1], independently of the class of the
%   input. GSCALE performs any necessary scaling. If the input is of
%   class double, and its values are not in the range [0, 1], then
%   GSCALE scales it to this range before processing.
%
%   The class of the output is the same as the class of the input.
 
%   Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
%   Digital Image Processing Using MATLAB, Prentice-Hall, 2004
%   $Revision: 1.5 $  $Date: 2003/11/21 14:36:09 $
 
if length(varargin) == 0 % If only one argument it must be f.
   method = 'full8';
else
   method = varargin{1};
end
 
if strcmp(class(f), 'double') & (max(f(:)) > 1 | min(f(:)) < 0)
   f = mat2gray(f);
end
 
% Perform the specified scaling.
switch method
case 'full8'
   g = im2uint8(mat2gray(double(f)));
case 'full16'
   g = im2uint16(mat2gray(double(f)));
case 'minmax'
   low = varargin{2}; high = varargin{3};
   if low > 1 | low < 0 | high > 1 | high < 0
      error('Parameters low and high must be in the range [0, 1].')
   end
   if strcmp(class(f), 'double')
      low_in = min(f(:));
      high_in = max(f(:));
   elseif strcmp(class(f), 'uint8')
      low_in = double(min(f(:)))./255;
      high_in = double(max(f(:)))./255;
   elseif strcmp(class(f), 'uint16')
      low_in = double(min(f(:)))./65535;
      high_in = double(max(f(:)))./65535;   
   end
   % imadjust automatically matches the class of the input.
   g = imadjust(f, [low_in high_in], [low high]);  
otherwise
   error('Unknown method.')
end
調用語法爲:
g = gscale(f, method, low, high)
其中,f是將被標度的圖像,method的有效值爲'full8'和'full16',前者將輸出標度爲全範圍[0, 255],後者將輸出標度爲全範圍[0, 65535]。若使用這兩個有效值之一,則可在兩種變換中省略參數low與high。method的第三個有效值爲'minmax',此時我們必須給出low與high在範圍[0, 1]內的值。若選用的是'minmax',則映射的結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章