grayscale normalized histogram without using hist() Matlab

瘦欲@ 提交于 2019-12-12 04:45:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!