at what range histogram of 16 and 8 bins fall.

2 views (last 30 days)
when i am trying to make a histogram of an image without imhist(). my values does not match.
i=imread('lena.tif');
[count,b]=imhist(i,16);
what is the value of b? is b a range or interval ? if yes then what it is for each bin?
my program without imhist()
imData=imread('lena.tif');
for i=1:m
for j=1:n
histo=imData(i,j);
if ((histo==0) || (histo<=15))
count(1)=count(1)+1;
elseif ((histo==16) || (histo<=31))
count(2)=count(2)+1;
:
:
:
elseif((histo==224) || (histo<=239))
count(15)=count(15)+1;
else
count(16)=count(16)+1;
end
end
my value of this 'count' doesn't match with previous by using imhist().

Accepted Answer

Image Analyst
Image Analyst on 31 Dec 2012
They are the bin centers. Look at this code to prove it:
myImage = uint8(0:255)
[pixelCounts grayValues] = imhist(myImage, 16)
pixelCounts =
9
17
17
17
17
17
17
17
17
17
17
17
17
17
17
9
grayValues =
0
17
34
51
68
85
102
119
136
153
170
187
204
221
238
255
Note how the first and last bins have only half the number of counts as the other bins? That's because they're centered at 0 and 255 and are only half the width of the other bins - they're 9 gray levels wide instead of 17 gray levels wide. So bin1 goes from 0 to 8, bin2 goes from 9 to 25, ... bin 16 goes from 247 to 255. It actually might go further but there are no 8 bit values past 255 so it ends there.
  6 Comments
preet
preet on 3 Jan 2013
there is any logic to divide the values in this way....half to the first and last bin....
in general if we talk about 8bins corresponding to 256 values..the it should be divide the rang of bins into equal . like 256/8=32...but here is somthing different in Matlab..
Walter Roberson
Walter Roberson on 3 Jan 2013
Yes, there is logic. The first bin center goes at the minimum value, the last bin center goes at the minimum value, and the other (N-2) are placed equally in the range of values between the min and max. Placing (N-2) interior points at equal distances in the range (max-min) has them spaced (max-min)/(N-1) apart.
In this case, min = 0, max = 255, and (255-0)/(16-1) = 255/15 = 17, so the bin centers are 0:17:255
Perhaps you are asking about why this is done. It is not at all clear from the documentation, but we can see a small hint: http://www.mathworks.com/help/matlab/ref/hist.html
n = hist(Y) bins the elements in vector Y into 10 equally spaced containers
notice "equally spaced" is used, not "equal length". It isn't much to go on.
I do not know why this was chosen. The fact is that it was. If you want something different, then use hist() or histc() instead of imhist(), or write your own histogramming function. Hint: histc(). Or if you insist on not using histc() then use accumarray()

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 31 Dec 2012
I think the bins returned in "b" are the bin centers, such as would be returned by hist().
Note: you do not need the (histo==NUMBER) part of your code
if histo <= 15
count(1)=count(1)+1;
elseif histo <= 31
count(2)=count(2)+1;
and so on.
Or, much more compact, get rid of the if/elseif tree and use
binnum = 1 + floor(histo / 16);
count(binnum) = count(binnum) + 1;
  1 Comment
preet
preet on 31 Dec 2012
thanx for compact code.. the value of b is for gray scale image
0
17
34
51
68
85
102
119
136
153
170
187
204
221
238
255
this s not the center..i think

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!