2011-09-13 72 views
2

我是Matlab新手,我想讓自己的功能與imhist做相同的工作(顯示圖像數據的直方圖),但我是一個新手,並且我沒有有任何線索我將如何開發這樣的功能.. 我開始做的東西,但它非常不完整。圖像數據的Matlab hist功能

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 

我會很感激,如果你能給我任何有用的技巧..

回答

2

更換

figure,plot(d,c); 

這是我在實現IMHIST功能的嘗試。它不處理的所有案件的官方職能的做法,但它應該產生在大多數情況下是非常相似的結果:

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

myimhist imhist

請注意IMHIST如何顯然調整y-限制以處理直方圖中的兩個不同峯值。

0

我不知道如果我理解你的目標。試着用

figure,bar(d,c);