回 帖 发 新 帖 刷新版面

主题:灰度直方图

function [yout,x] = imhist(varargin)

[a, n, isScaled, top, map] = parse_inputs(varargin{:});

if islogical(a)
    if (n ~= 2)
        messageId = 'Images:imhist:invalidParameterForLogical';
        message1 = 'N must be set to two for a logical image.'; 
        error(messageId, '%s', message1);
    end
    y(2) = sum(a(:));
    y(1) = numel(a) - y(2);
    y = y';
elseif isa(a,'int16')
    y = imhistc(int16touint16(a), n, isScaled, top); % Call MEX file to do work.
else
    y = imhistc(a, n, isScaled, top); % Call MEX file to do work.
end

range = getrangefromclass(a);
if islogical(a)
    x = range';
else
    % integer or float
    x = linspace(range(1), range(2), n)';
end

if (nargout == 0)
    plot_result(x, y, map, isScaled, class(a), range);
else
    yout = y;
end


%%%
%%% Function plot_result
%%%
function plot_result(x, y, cm, isScaled, classin, range)

n = length(x);
stem(x,y, 'Marker', 'none')
hs = gca;

limits = axis;
if n ~= 1
  limits(1) = min(x);
else
  limits(1) = 0;
end
limits(2) = max(x);
var = sqrt(y'*y/length(y));
limits(4) = 2.5*var;
axis(limits);

% Get axis position and make room for color stripe.
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,'xticklabel','')

% Create axis for stripe
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)];
    limits(1:2) = range;
    switch classin
     case {'uint8','uint16'}
        xdata = range(2)*xdata;
        C = (1:n)/n;
     case 'int16'
        xdata = 65535*xdata - 32768;
        C = (1:n)/n;
     case {'double','single'}
        C = (1:n)/n;
     case 'logical'
        C = [0 1];
     otherwise
        messageId = sprintf('Images:%s:internalError', mfilename);
        error(messageId, 'The input image must be uint8, uint16, %s', ...
            'double, or logical.');    
    end
    
    % image(X,Y,C) where C is the RGB color you specify. 
    image(xdata,[0 1],repmat(C, [1 1 3]));
else
    if length(cm)<=256
        image([1 n],[0 1],1:n); colormap(cm)
        limits(1) = 0.5;
        limits(2) = n + 0.5;
    else
        image([1 n],[0 1],permute(cm, [3 1 2]));
        limits(1) = 0.5;
        limits(2) = n + 0.5;
    end
end

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

% Put a border around the stripe.
j=line(limits([1 2 2 1 1]),limits([3 3 4 4 3]));set(j,'linestyle','-')
set(j,'color',get(gca,'xcolor'))

% Special code for a binary image
if strcmp(classin,'logical')
    % make sure that the stripe's X axis has 0 and 1 as tick marks.
    set(gca,'XTick',[0 1]);

    % remove unnecessary tick marks from axis showing the histogram
    axes(hs)
    set(gca,'XTick',0);
    
    % make the histogram lines thicker
    h = get(hs,'children');
    obj = findobj(h,'flat','Color','b');
    lineWidth = 10;
    set(obj,'LineWidth',lineWidth);
else
  axes(hs)
end

set(gcf,'Nextplot','replace')


%%%
%%% Function parse_inputs
%%%
function [a, n, isScaled, top, map] = parse_inputs(varargin)

iptchecknargin(1,2,nargin,mfilename);
a = varargin{1};
iptcheckinput(a, {'double','uint8','logical','uint16','int16','single'}, ...{'2d','nonsparse'}, mfilename, ['I' or 'X'],1);
n=256;
if isa(a,'double') || isa(a,'single')
    isScaled = 1;
    top = 1;
    map = []; 
    
elseif isa(a,'uint8')
    isScaled = 1; 
    top = 255;
    map = [];
    
elseif islogical(a)
    n = 2;
    isScaled = 1;
    top = 1;
    map = [];
    
else % int16 or uint16
    isScaled = 1; 
    top = 65535;
    map = [];
end
    
if (nargin ==2)
    if (numel(varargin{2}) == 1)
        % IMHIST(I, N)
        n = varargin{2};
        iptcheckinput(n, {'numeric'}, {'real','positive','integer'}, mfilename, ...
                      'N', 2);
        
    elseif (size(varargin{2},2) == 3)
      if isa(a,'int16')
        eid = sprintf('Images:%s:invalidIndexedImage',mfilename);
        msg1 = 'An indexed image can be uint8, uint16, double, single, or ';
        msg2 = 'logical.';
        error(eid,'%s %s',msg1, msg2);
      end

      % IMHIST(X,MAP) or invalid second argument
      n = size(varargin{2},1);
      isScaled = 0;
      top = n;
      map = varargin{2};
      
    else
        messageId = sprintf('Images:%s:invalidSecondArgument', mfilename);
        message4 = 'Second argument must be a colormap or a positive integer.';
        error(messageId, '%s', message4); 
    end
end
这是一个计算机上原有的求图像灰度直方图的程序。但就是不能运行。显示第二行有错,还有下面两行有错
iptcheckinput(a, {'double','uint8','logical','uint16','int16','single'}, ...{'2d','nonsparse'}, mfilename, ['I' or 'X'],1);
n=256;
我不知道该怎末改,请求各位帮一下,多谢了

回复列表 (共3个回复)

沙发

提示你输入的类型错误,你的输入图象矩阵格式有误

板凳


谢谢你。请再帮我指点一下。我的图像用的是jpg格式,为什么不行呢?

3 楼


我来回复

您尚未登录,请登录后再回复。点此登录或注册