cmunique.m

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

M
103
字号
function [c,map] = cmunique(varargin)
%CMUNIQUE Find unique colormap colors and corresponding image.
%   [Y,NEWMAP] = CMUNIQUE(X,MAP) returns the indexed image Y and 
%   associated colormap NEWMAP that produce the same image as
%   (X,MAP) but with the smallest possible colormap. CMUNIQUE
%   removes duplicate rows from the colormap and adjusts the
%   indices in the image matrix accordingly.
%
%   [Y,NEWMAP] = CMUNIQUE(RGB) converts the truecolor image RGB
%   to the indexed image Y and its associated colormap
%   NEWMAP. NEWMAP is the smallest possible colormap for the
%   image, containing one entry for each unique color in
%   RGB. (Note that NEWMAP may be very large, as much as P-by-3
%   where P is the number of pixels in RGB.) 
%
%   [Y,NEWMAP] = CMUNIQUE(I) converts the intensity image I to an
%   indexed image Y and its associated colormap NEWMAP. NEWMAP is
%   the smallest possible colormap for the image, containing one
%   entry for each unique intensity level in I. 
%
%   Class Support
%   -------------
%   The input image can be of class uint8 or double. The class of
%   the output image Y is uint8 if the length of newmap is less
%   than or equal to 256. If the length of NEWMAP is greater than
%   256, Y is of class double.
%
%   See also RGB2IND, GRAY2IND.

%   Clay M. Thompson 10-5-92
%   Copyright 1993-1998 The MathWorks, Inc.  All Rights Reserved.
%   $Revision: 5.8 $  $Date: 1997/11/24 15:34:19 $

nargs = length(varargin);

error(nargchk(1,3,nargs));

% Convert all possible input arguments to an indexed image.
if nargs==1, % cmunique(I) or cmunique(RGB)
    arg1 = varargin{1};
    if ndims(arg1)==3, % cmunique(RGB)
        [c,map] = rgb2ind(arg1);
    else % cmunique(I)
        [c,map] = rgb2ind(arg1,arg1,arg1);
    end
elseif nargs==2, % cmunique(a,cm)
    c = varargin{1}; map = varargin{2};
elseif nargs==3, % cmunique(r,g,b)
    [c,map] = rgb2ind(varargin{1},varargin{2},varargin{3});
end

if isa(c, 'uint8')    % The promotion is necessary for the indexing into
    c = double(c)+1;  % pos below --  ...loc(pos(c))...
end

tol = 1/1024;

% Quantize colormap entries to help matching below.
map = round(map/tol)*tol;

%  
% Remove matching entries from colormap
%
 
% Sort colormap entries
[dum,ndx1] = sort(map(:,1));
[dum,ndx2] = sort(map(ndx1,2));
[dum,ndx3] = sort(map(ndx1(ndx2),3));
                % ndx maps from sorted cm to original cm
ndx = ndx1(ndx2(ndx3));
                % pos maps from original cm to sorted cm
pos = zeros(size(ndx)); pos(ndx) = 1:length(ndx);

% Find matching entries
                % d indicates the location of matching entries
d = all(abs(diff(map(ndx,:)))'<tol)';

% Mapping from full cm to compressed cm
                % loc maps from sorted cm to compressed cm
loc = [1:length(ndx)]' - [0;cumsum(d)]; 
c(:) = loc(pos(c));

% Remove matching entries (compress cm)
ndx(find(d)) = [];
map = map(ndx,:);

%
% Remove colormap entries that are not used in c
%
[n,x] = imhist(c,map);
d = (n==0); % Find unused colormap entries
loc = [1:length(map)]' - cumsum(d);

% Update image values
c(:) = loc(c);

% Remove unused entries (compress cm)
map(find(d),:) = [];

if max(c(:))<=256    % Output a uint8 array if we can
    c = uint8(c-1);
end

⌨️ 快捷键说明

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