MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by preet
on 31 Dec 2012

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().

*No products are associated with this question.*

Answer by Image Analyst
on 31 Dec 2012

Accepted answer

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.

Show 3 older comments

Walter Roberson
on 3 Jan 2013

He said "Note how the first and last bins have only half the number of counts as the other bins?" So the "half of counts" is relative to the other counts, pixelCounts(1) compared to pixelCounts(2)

The same *pattern* would occur for 8 bins: the first and last bins would have counts that were each only have of the counts for bins 2 to 6. The actual counts will be higher because you are dividing the 256 different values (0:255) into fewer pieces.

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
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()

Answer by 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;

## 0 Comments