⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rgb2ind.m

📁 有关matlab的电子书籍有一定的帮助希望有用
💻 M
字号:
function [a,map] = rgb2ind(r,g,b,cm,dith)
%RGB2IND Convert RGB image to indexed image.
%   RGB2IND converts RGB images to indexed images using one of
%   four different methods: direct translation, uniform
%   quantization, minimum variance quantization, and colormap
%   approximation. For all methods except direct translation,
%   RGB2IND dithers the image unless you specify 'nodither' for
%   DITHER_OPTION.
%
%   [X,MAP] = RGB2IND(RGB) converts the RGB image in the array
%   RGB to an indexed image X with colormap MAP using direct
%   translation. The resulting colormap may be very long, as it
%   has one entry for each pixel in RGB. Do not set DITHER_OPTION
%   if you use this method.
%
%   [X,MAP] = RGB2IND(RGB,TOL) converts the RGB image to an
%   indexed image X using uniform quantization. MAP contains at
%   most (FLOOR(1/TOL)+1)^3 colors. TOL must be between 0.0 and
%   1.0.
%
%   [X,MAP] = RGB2IND(RGB,N) converts the RGB image to an indexed
%   image X using minimum variance quantization. MAP contains at
%   most N colors.
%
%   X = RGB2IND(RGB,MAP) converts the RGB image to an indexed
%   image X with colormap MAP by matching colors in RGB with the
%   nearest color in the colormap MAP.
%
%   [...] = RGB2IND(...,DITHER_OPTION) enables or disables
%   dithering. DITHER_OPTION is a string that can have one of
%   these values:
%
%       'dither'   dithers, if necessary, to achieve better color
%                  resolution at the expense of spatial
%                  resolution (default)
%
%       'nodither' maps each color in the original image to the
%                  closest color in the new map. No dithering is
%                  performed.
%
%   Class Support
%   -------------
%   The input image can be of class uint8 or double. The output
%   image is of class uint8 if the length of MAP is less than or
%   equal to 256, or double otherwise.
%
%   Example
%   -------
%       RGB = imread('flowers.tif');
%       [X,map] = rgb2ind(RGB,128);
%       imshow(X,map)
%
%   See also CMUNIQUE, DITHER, IMAPPROX, IND2RGB, RGB2GRAY.

%   Clay M. Thompson 9-29-92
%   Copyright 1993-1998 The MathWorks, Inc.  All Rights Reserved.
%   $Revision: 5.8 $  $Date: 1997/11/24 15:36:14 $

if nargin < 1,
  error('Need input arguments.')
end

threeD = (ndims(r)==3); % Determine if input includes a 3-D array

if threeD,
  
  error(nargchk(1,3,nargin));

  if nargin < 3, b = 'dither'; else b = [lower(b) ' ']; end
  if (b(1)~='d') & (b(1)~='n')
    error('''dither_flag'' must be either ''dither'' or ''nodither''.');
  end

  if nargin==1 % Convert the RGB image to an indexed image.
    a = zeros([size(r,1), size(r,2)]);
    a(:) = 1:(size(r,1)*size(r,2));
    map = reshape(r(:),size(r,1)*size(r,2),3);
  elseif prod(size(g))==1,
    if (g < 1.0), % Use uniform quantization
    
      % Determine colors by uniform quantization

      tol = g;
      if (tol < 0), error('Tolerance must be between 0.0 and 1.0'); end
      n = round(1/tol);

      % Create colormap
      [x,y,z] = meshgrid((0:n)/n,(0:n)/n,(0:n)/n);
      map = [x(:),y(:),z(:)];
 
      if b(1) == 'n'; % 'no dither'
        % Determine indexed image (with no dithering)
          if isa(r, 'uint8')
              r = double(r)/255;
          end
          r = round(r*n);
        a = r(:,:,3)*((n+1)^2) + r(:,:,1)*(n+1) + r(:,:,2) + 1;
        [a,map] = cmunique(a,map);
      else
        a = dither(r,map);
        [a,map] = cmunique(a,map);
      end
   
    else % Use variance minimization quantization
      [a,map] = vmquant(r,g,[5 5 5],'n');
      % Use standalone dither if map is an approximation.
      if (b(1)=='d') & (size(map,1) == g)
        a = dither(r,map);
      end
    end

  else 
    map = g;
    if b(1)=='n', % 'nodither'
        a = dither(r,map,5,4); % Use dither to do inverse colormap mapping.
    else
        a = dither(r,map);
    end
  end

else % RGB image entered as separate 2-D arrays

  error(nargchk(3,5,nargin));

  if nargin<5, dith = 'dither'; else dith = [lower(dith) ' ']; end
  if (dith(1)~='d') & (dith(1)~='n')
    error('''dither_flag'' must be either ''dither'' or ''nodither''.');
  end

  if nargin==3,  % Directly convert R,G,B to indexed image.
    a = zeros(size(r));
    a(:) = 1:prod(size(r));
    map = [r(:),g(:),b(:)];

  elseif prod(size(cm))==1, 
    if cm < 1.0, % Use uniform quantization

      %
      % Determine colors by uniform quantization
      %

      tol = cm;
      if (tol<0), error('Tolerance must be between 0.0 and 1.0'); end
      n = round(1/tol);

      % Create colormap
      [x,y,z] = meshgrid((0:n)/n,(0:n)/n,(0:n)/n);
      map = [x(:) y(:) z(:)];

      if dith(1) == 'n'; % 'nodither'
        % Determine indexed image (with no dithering)
        r = round(double(r)*n);
        g = round(double(g)*n);
        b = round(double(b)*n);
        a = b*((n+1)^2) + r*(n+1) + g + 1;
        [a,map] = cmunique(a,map);
      else
        a = dither(r,g,b,map);
        [a,map] = cmunique(a,map);
      end

    else % Use variance minimization quantization
      [a,map] = vmquant(r,g,b,cm,[5 5 5],'n');
      % Use standalone dither if map is an approximation.
      if (dith(1)=='d') & (size(map,1) == cm)
        a = dither(r,g,b,map);
      end
    end

  else 
    map = cm;
    if dith(1)=='n', % 'nodither'
      a = dither(r,g,b,map,5,4); % Use dither to do inverse colormap mapping.
    else
      a = dither(r,g,b,map);
    end
  end
end

% Return uint8 if there are <= 256 colors
if isa(a,'double') & max(a(:)) <= 256,     
    a = uint8(a-1);
end

if isa(map, 'uint8'),    % Make sure that the colormap is doubles
    map = double(map)/255;
end


⌨️ 快捷键说明

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