Pregunta

I have one signed 16 bit image (pixel range from 0 to over 256). I want to count probability of pixel and save to file. How to do it in matlab. I try to do it by my code. But it is not successfull. My style is

Pixel Value   | Prob
          1   | 0.1
          2   | 0.2
          3   | 0.03
    ...

This is my code

im=imread('test.bmp');
im=double(im);
[m n]=size(im);
p=zeros(2,max(im(:)));
im=im(:);
imsort=sort(im);
for j=1:max(imsort(:))
    p(1,j)=j;
    count=sum(j==im(:));
    p(2,j)=count/(m*n);
end
¿Fue útil?

Solución

Use histc:

values = (0:2^16-1).';
count = histc(im(:), values);
prob = count/sum(count);

The result in the desired format would be

result = [values prob];

Otros consejos

To save histogram result as a text file

save(filename, variable,'ascii');
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top