1. 程式人生 > >【影象處理】MATLAB:亮度變換

【影象處理】MATLAB:亮度變換

亮度變換

函式imadjust

f = imread('breast_digital_Xray.tif');

g1 = imadjust(f,[0 1],[1 0]);                                       %陰暗反轉影象(負片影象),等同於 g1 = imcomplement(f) 
g2 = imadjust(f,[0.5 0.75],[0 1]);                                  %將0.5至0.75之間的灰度級擴充套件到範圍[0,1]
g3 = imadjust(f,[ ],[ ],2);                                         %有更多的灰色調,方法是壓縮灰度級的低端並擴充套件灰度級的高階
subplot(2,2,1);imshow(f);title('原始乳房腫瘤影象'); subplot(2,2,2);imshow(g1);title('負片影象'); subplot(2,2,3);imshow(g2);title('亮度範圍擴充套件為[0.5 0.75]後的影象'); subplot(2,2,4);imshow(g3);title('使用gamma=2增強影象後的影象');

對數和對比度拉伸變換

  對數與對比度拉伸是進行動態範圍處理的基本工具。對數變換通過如下表達式實現:

      g = c * log ( 1 + double ( f ) )

  當執行一個對數變換時,我們通常期望將壓縮值還原為顯示的全範圍。對8位元而言,可使用語句:

      gs = im2uint8 ( mat2gray ( g ) ) ;

  使用函式mat2gray可將值限定在範圍[0,1]內,使用函式im2uint8可將值限定在範圍[0,255]內。

使用對數變換減小動態範圍

f = imread('DFT.tif');
g = im2uint8(mat2gray(log(1+double(f))));
subplot(1,2,1);imshow(f);title('傅立葉頻譜');
subplot(1,2,2);imshow(g);title('執行對數變換後的結果');

實用M函式

nargin 和 nargout

亮度標定gscale函式

f = imread('skeleton.tif');
g = gscale(f,'full8');
subplot(1,2,1);imshow(f);title('骨骼掃描影象');
subplot(1,2,2);imshow(g);title('亮度標定影象');

亮度變換intrans函式

f = imread('skeleton.tif');
g = intrans(f,'stretch',mean2(im2double(f)),0.9);
subplot(1,2,1);imshow(f);title('骨骼掃描影象');
subplot(1,2,2);imshow(g);title('使用對比度拉伸變換增強的影象');

附上gscale函式具體程式碼:

function g=gscale(f,varargin)
if length(varargin)==0
  method='full8';
else method=varargin{1};
end
if strcmp(class(f),'double')&(max(f(:))>1 | min(f(:))<0)
   f=mat2gray(f);
end


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

       g=imadjust(f,[low_in high_in],[low high]);
otherwise
       error('Unknown method')
end

附上intrans函式具體程式碼:

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);