[英]grayscale normalized histogram without using hist() Matlab
Input : a grayscale img in [0..255] 输入 :[0..255]中的灰度img
Output : img histogram normalized - an array 1X256 divided by total number of pixels 输出 :img histogram标准化 - 数组1X256除以像素总数
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
: 无论如何,这是使用
accumarray
进行上述操作的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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.