2014-09-30 78 views
0

我試圖做一個圖像的直方圖而不使用imhist功能。我怎樣才能複製這個沒有使用imhist?沒有imhist做直方圖

originalImage = imread('hips.png'); 
imhist(originalImage); title('Histogram with Matlab'); 

我知道我應該創建一個2D數組,然後用2 for循環去每個像素,但我不知道下一步該怎麼做。

謝謝。

回答

3

您可以使用hist函數來獲取每個頻率值的像素數。然後你需要實現繪圖部分。

的代碼如下所示:

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