Image 图像数据的Matlab hist函数
我是Matlab新手,我想制作自己的函数,与imhist(显示图像数据的直方图)的工作相同,但我对此完全是新手,我不知道如何开发这样的函数。。 我开始做一些东西,但它非常不完整Image 图像数据的Matlab hist函数,image,matlab,function,image-processing,plot,Image,Matlab,Function,Image Processing,Plot,我是Matlab新手,我想制作自己的函数,与imhist(显示图像数据的直方图)的工作相同,但我对此完全是新手,我不知道如何开发这样的函数。。 我开始做一些东西,但它非常不完整 function [ output_args ] = myhist( x ) %MYHIST Summary of this function goes here %Detailed explanation goes here x=imread('flower.jpg'); imshow(x); [c,d]=his
function [ output_args ] = myhist( x )
%MYHIST Summary of this function goes here
%Detailed explanation goes here
x=imread('flower.jpg');
imshow(x);
[c,d]=hist(x(:),0:1:255);
figure,plot(d,c);
figure,plot(c,d);
%figure,imhist(x);
end
如果您能给我一些有用的建议,我将不胜感激。我不确定是否理解您的目标。试着替换
figure,plot(d,c);
与
下面是我实现该功能的尝试。它并不像官方职能部门那样处理所有情况,但在大多数情况下,它应该会产生非常相似的结果:
function myimhist(img)
img = im2uint8(img);
[count,bin] = hist(img(:), 0:255);
stem(bin,count, 'Marker','none')
hAx = gca;
set(hAx, 'XLim',[0 255], 'XTickLabel',[], 'Box','on')
%# create axes, and draw grayscale colorbar
hAx2 = axes('Position',get(hAx,'Position'), 'HitTest','off');
image(0:255, [0 1], repmat(linspace(0,1,256),[1 1 3]), 'Parent',hAx2)
set(hAx2, 'XLim',[0 255], 'YLim',[0 1], 'YTick',[], 'Box','on')
%# resize the axis to make room for the colorbar
set(hAx, 'Units','pixels')
p = get(hAx, 'Position');
set(hAx, 'Position',[p(1) p(2)+26 p(3) p(4)-26])
set(hAx, 'Units','normalized')
%# position colorbar at bottom
set(hAx2, 'Units','pixels')
p = get(hAx2, 'Position');
set(hAx2, 'Position',[p(1:3) 26])
set(hAx2, 'Units','normalized')
%# link x-limits of the two axes
linkaxes([hAx;hAx2], 'x')
set(gcf, 'CurrentAxes',hAx)
end
让我们用一个示例图像来测试它:
I = imread('coins.png');
figure(1), myimhist(I), title('myimhist')
figure(2), imhist(I), title('imhist')
请注意,IMHIST显然是如何调整y限制,以便处理直方图中的两个不同峰值
I = imread('coins.png');
figure(1), myimhist(I), title('myimhist')
figure(2), imhist(I), title('imhist')