📄 imhist.m
字号:
function [yout,x] = imhist(varargin)
%IMHIST Display histogram of image data.
% IMHIST(I,N) displays a histogram with N bins for the
% intensity image I above a grayscale colorbar of length N. If
% you omit the argument, IMHIST uses a default value of N=256
% if I is a grayscale image, or N=2 if I is a binary image.
%
% IMHIST(X,MAP) displays a histogram for the indexed image
% X. This histogram shows the distribution of pixel values
% above a colorbar of the colormap MAP. The colormap must be at
% least as long as the largest index in X. The histogram has
% one bin for each entry in the colormap.
%
% [COUNTS,X] = imhist(...) returns the histogram counts in
% COUNTS and the bin locations in X so that stem(X,COUNTS)
% shows the histogram. For indexed images, it returns the
% histogram counts for each colormap entry; the length of
% COUNTS is the same as the length of the colormap.
%
% Class Support
% -------------
% The input image can be of class uint8 or double.
%
% Example
% -------
% I = imread('pout.tif');
% imhist(I)
%
% See also HISTEQ, HIST.
% Clay M. Thompson 7-7-92, partially mex-ified by
% Joseph M. Winograd, 7-93
% Updated for MATLAB 5, IPT 2 by Steven L. Eddins, 1996
% Copyright 1993-1998 The MathWorks, Inc. All Rights Reserved.
% $Revision: 5.10 $ $Date: 1997/11/24 15:35:25 $
[a, n, isScaled, top, map, err_msg] = parse_inputs(varargin{:});
if (~isempty(err_msg))
error(err_msg);
end
y = imhistc(a, n, isScaled, top); % Call MEX file to do work.
x = (1:n)';
if (nargout == 0)
isUint8 = isa(a, 'uint8');
plot_result(x, y, map, isScaled, isUint8);
else
yout = y;
end
%%%
%%% Function plot_result
%%%
function plot_result(x, y, cm, isScaled, isUint8)
n = length(x);
stem(x,y)
hs = gca;
h = get(hs,'children');
delete(findobj(h,'flat','Marker','o')) % Remove 'o's from stem plot
limits = axis;
limits(1) = 0.5;
limits(2) = n+0.5;
var = sqrt(y'*y/length(y));
limits(4) = 2.5*var;
axis(limits);
% Get axis position and make room for color strip.
pos = get(hs,'pos');
stripe = 0.075;
set(hs,'pos',[pos(1) pos(2)+stripe*pos(4) pos(3) (1-stripe)*pos(4)])
set(hs,'xticklabelmode','manual')
set(hs,'xticklabel','')
% Create axis for stripe
ax = axes('Position', [pos(1) pos(2) pos(3) stripe*pos(4)]);
limits = axis;
% Create color stripe
if isScaled,
binInterval = 1/n;
xdata = [binInterval/2 1-(binInterval/2)];
if isUint8,
xdata = 255*xdata;
limits(1) = 0;
limits(2) = 255;
else
limits(1) = 0;
limits(2) = 1;
end
image(xdata,[0 1],repmat((1:n)/n,[1 1 3]));
else
image([1 n],[0 1],1:n); colormap(cm)
limits(1) = 0.5;
limits(2) = n + 0.5;
end
set(gca,'yticklabelmode','manual')
set(gca,'yticklabel','')
axis(limits);
j=line(limits([1 2 2 1 1]),limits([3 3 4 4 3]));set(j,'linestyle','-')
set(j,'color',get(gca,'xcolor'))
% Set active axis to histogram
axes(hs)
set(gcf,'Nextplot','replace')
%%%
%%% Function parse_inputs
%%%
function [a, n, isScaled, top, map, err_msg] = parse_inputs(varargin)
defaultN = 256;
err_msg = '';
switch nargin
case 0
a = [];
n = [];
isScaled = [];
top = [];
map = [];
err_msg = 'Too few inputs to IMHIST';
case 1
a = varargin{1};
if (isa(a,'double'))
n = defaultN;
isScaled = 1;
top = 1;
map = [];
elseif (isa(a,'uint8'))
if (islogical(a))
n = 2;
isScaled = 1;
top = 1;
map = [];
else
n = 256;
isScaled = 1;
top = 255;
map = [];
end
end
case 2
a = varargin{1};
if (prod(size(varargin{2})) == 1)
% IMHIST(I, N)
if (isa(a,'uint8'))
if (islogical(a))
a = varargin{1};
n = varargin{2};
isScaled = 1;
top = 1;
map = [];
else
a = varargin{1};
n = varargin{2};
isScaled = 1;
top = 255;
map = [];
end
else
a = varargin{1};
n = varargin{2};
isScaled = 1;
top = 1;
map = [];
end
elseif (size(varargin{2},2) == 3)
a = varargin{1};
n = size(varargin{2},1);
isScaled = 0;
top = n;
map = varargin{2};
else
a = [];
n = [];
isScaled = [];
top = [];
map = [];
err_msg = 'Invalid second input argument';
end
otherwise
a = [];
n = [];
isScaled = [];
top = [];
map = [];
err_msg = 'Too many input arguments';
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -