im2uint8.m

来自「有关matlab的电子书籍有一定的帮助希望有用」· M 代码 · 共 57 行

M
57
字号
function u = im2uint8(img, typestr)
%IM2UINT8 Convert image to eight-bit unsigned integers.
%   IM2UINT8 takes an image as input, and returns an image of
%   class uint8.  If the input image is of class uint8, the
%   output image is identical to it.  If the input image is of
%   class double, im2uint8 returns the equivalent image of class
%   uint8, rescaling or offsetting the data as necessary.
%
%   I2 = IM2UINT8(I1) converts the intensity image I1 to uint8,
%   rescaling the data if necessary.
%
%   RGB2 = IM2UINT8(RGB1) converts the truecolor image RGB1 to
%   uint8, rescaling the data if necessary.
%
%   BW2 = IM2UINT8(BW1) converts the binary image BW1 to uint8.
%
%   X2 = IM2UINT8(X1,'indexed') converts the indexed image X1 to
%   uint8, offsetting the data if necessary.  Note that
%   MAX(X1(:)) must be 256 or less, or else X1 cannot be
%   converted to uint8.
%
%   See also DOUBLE, IM2DOUBLE, UINT8.

%   Chris Griffin 6-9-97
%   Copyright 1993-1998 The MathWorks, Inc.  All Rights Reserved.
%   $Revision: 1.5 $  $Date: 1997/11/24 15:35:14 $

if isa(img, 'uint8')
   u = img; 
elseif isa(img, 'double')
   if nargin==1
      if islogical(img)        % uint8 binary image
         u = uint8(img~=0);
      else                   % uint8 intensity image
         % clip data range to prevent wrapping
         img = min(1, max(0, img)); 
         u = uint8(round(img*255));
      end
   elseif nargin==2
      if ~ischar(typestr) | (typestr(1) ~= 'i')
         error('Invalid input arguments');
      else 
         if max(img(:))>=257 
            error('There are too many colors for 8-bit integer storage.');
         elseif min(img(:))<1
            error('Invalid indexed image: an index was less than 1.');
         else
            u = uint8(img-1);
         end
      end
   else
      error('Invalid input arguments.');
   end
else
   error('Unsupported input class.');
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?