Вопрос

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
Это было полезно?

Решение

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];

Другие советы

To save histogram result as a text file

save(filename, variable,'ascii');
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top