Reputation: 233
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
Upvotes: 0
Views: 1558
Reputation: 112749
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];
Upvotes: 3
Reputation: 624
To save histogram result as a text file
save(filename, variable,'ascii');
Upvotes: 1