问题
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?
回答1:
"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);
来源:https://stackoverflow.com/questions/13324840/grayscale-normalized-histogram-without-using-hist-matlab