2012-11-10 68 views
0

輸入:在[0..255]灰度IMG
輸出:IMG直方圖歸一化 - 由像素灰度標準化直方圖不使用HIST()Matlab的

此的總數除以陣列1X256是我的解決方案:

function [h] = histImage(img) 
    h=zeros(1,256) 
    for i=1:size(h,2) 
     h(i) = length(find(img==i)); 
    end 
    h = h./sum(h); 

有沒有更好的方法來做到這一點?

回答

2

「更好」總是在旁觀者的眼中。總之,這裏的一個的方法來做到上述使用accumarray

%# each pixel contributes 1/nPixels to the counts of the histogram 
%# to use graylevels as indices, we have to add 1 to make it 1...256 

nPix = numel(img); 
h = accumarray(img(:)+1,ones(nPix,1)/nPix,[256 1],@sum,0); 
+0

H = accumarray(IMG(:)+ 1,一(nPix,1)/ nPix,[256 1],@總和,0);這是我猜的 – Gilad

+0

@Androidy:我想是的。 :) – Jonas