Matlab 把圖像標度在全尺度gscale()

Matlab 把圖像標度在全尺度gscale()

當處理圖像是,象素值域由負到正的現象是很普遍的。儘管在中間計算過程中沒有問題,但當我們想利用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’,則映射的結果

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