简体   繁体   中英

grayscale normalized histogram without using hist() Matlab

Input : a grayscale img in [0..255]
Output : img histogram normalized - an array 1X256 divided by total number of pixels

This is my solution:

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

Is there a better way to do it?

"Better" is always in the eye of the beholder. Anyway, here's a way to do the above using 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM