Ensuring same bins in image histogram

4 views (last 30 days)
Hi, I have say 10 images that have been constructed from various amounts of summing 12 bit images. Nothing exceeds 16 bit i.e. no intensity is greater that 65535. Practically, the max intensity of the 10 images could range from anywhere between 12000 cts and 65535 counts. I want to create a histogram that combines all 10 images.
I cna do this if I use the number of bins nbins=65536. But for memory purposes, I want to reduce the number of bins to say 1000. The problem I think is that taking the two extreme images with max counts 12000 and 65535, will the binning into 1000 intensity levels be the same for both so I can just add their histograms. If not, how can i make sure that each bin is the same so for nbins=1000
1st bin =66 2nd bin =2*66
etc, rather than just taking the maxval of the image and then dividing this by 1000.
Thanks Jason

Accepted Answer

Image Analyst
Image Analyst on 4 Dec 2014
There will be no memory problem with one or two arrays of 65536 elements.
I agree with thorsten. If you want to be sure the bins are where you want then use histc()
overallHistogram = zeros(1, 65536)
for k = 1 : numberOfImages
grayImage = imread(................
% Get counts for this image.
thisCounts = histc(grayImage(:), 0:65535);
% Accumulate into our histogram that will sum all histograms.
overallHistogram = overallHistogram + thisCounts;
end
bar(overallHistogram);
  5 Comments
Image Analyst
Image Analyst on 5 Dec 2014
If you don't want one bin per possible gray level you can set your bin edges to whatever width you want, like
binEdges = 0 : 66 : 65535;
thisCounts = histc(grayImage(:), binEdges);
I wouldn't use linspace if you know you want integers, though it will work.
Jason
Jason on 5 Dec 2014
OK, I will follow your advice, just out of curiosity, whats wrong with using linspace?.

Sign in to comment.

More Answers (2)

Thorsten
Thorsten on 4 Dec 2014
Use hist/histc with a second vector argument to specify bin centers/edges
N = HIST(Y,X), where X is a vector, returns the distribution of Y
among bins with centers specified by X. The first bin includes
data between -inf and the first center and the last bin
includes data between the last bin and inf. Note: Use HISTC if
it is more natural to specify bin edges instead.
  2 Comments
Jason
Jason on 4 Dec 2014
I always though that this function wasn't the best to use with images? Thanks
Thorsten
Thorsten on 4 Dec 2014
These functions can be used with images; no problem.

Sign in to comment.


Adam
Adam on 4 Dec 2014
[counts,x] = imhist(___)
should do the job so far as I can see. Of course your data with a range of 12000 will be spread over fewer bins than the data with a range of 65000, but then you'd expect that from what you want.
Then you should just be able to sum the counts together for the 10 images.
  2 Comments
Jason
Jason on 4 Dec 2014
So can I dictate what the bin wodth should be or is it automatic? I guess I would find out by X(2)-x(1) ?? Thanks
Adam
Adam on 4 Dec 2014
Edited: Adam on 4 Dec 2014
[counts,x] = imhist( I, n );
n is the number of bins so you if you want to specify bin width you can calculate n from that. Default is 256 bins.
Also note though that those bin values are taken from the data type. You need to be using int16 to have bins up to 65536, not single or double which provide histograms from 0 to 1.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!