输入:[0..255]中的灰度img 输出:img histogram normalized - 一个数组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);
有更好的方法吗?
答案 0 :(得分: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);