# how to set x-axes in a histogram of gray scale image and how to see if the image is saturated (12 bit)

6 views (last 30 days)
Happy PhD on 17 Feb 2021
Answered: Steven Lord on 17 Feb 2021
Hi, I am trying to plot a histogram of a unit16 picture (actually its a 12 bit image) and plot the gray scale. I am interested to see if the image gets saturated or not (again 12 bit image). Which btw i find weird its only the first 12 that is populated with a very huge number. Why is that? i only have a image of an laser lobe in the picture.
So my question is, because the histgram looks weird in the appdesigner plot, i think it doesn't seem to adjust to the pixel count in the picture in the x-scale. Y-scale has a huge number. What does this represent? How can i see if the picture is staurated? 2^12 = 4096 should be the max intensity of the image. but I get maxcount = 2123959
Any ideas how set the x-axes as well?
Bits of the code (from the appdesigner)
% Create histograms based on number of color channels
switch size(im,3)
case 1
% Display the grayscale image
imagesc(app.Image,im);
% Plot all histograms with the same data for grayscale
hist = histogram(app.HistAxes,im, 'FaceColor',[1 0 0],'EdgeColor', 'none');
%[counts,binLocations] = imhist(im);
otherwise
% Error when image is not grayscale or truecolor
uialert(app.UIFigure, 'Image must be grayscale.', 'Image Error');
return;
end
% Get largest bin count
maxim = max(hist.BinCounts);
maxcount = max([maxim])
% Set y axes limits based on largest bin count
app.HistAxes.YLim = [0 maxcount];
app.HistAxes.YTick = round([0 maxcount/2 maxcount], 2, 'significant');

Image Analyst on 17 Feb 2021
If the maxcount is 2123959, then that just means that the highest bin has a y value of 2123959. That bin could be anywhere along the x (gray scale) axis. If you look at the bin for 4095, and there are some values in there, and you have 4096 bins, then yes, some pixels are saturated. It is also possible to saturate at a gray level value that is not the maximum for the number of bits you're using for a variety of reasons. So you should look to see if the last bin or two is much higher than the bins just a little darker than those.
Happy PhD on 17 Feb 2021
Edited: Happy PhD on 17 Feb 2021
@Image Analyst There seem to be 770 binCounts, each with its own value. The image is about the size of 1200 x 900 pixels, What does the number 770 represent and what is 2123959? If I plot the intensity profile the maximum pixel value is below the intensity value 2^12 (see image below),.. that is what i don't get what the histrogram shows.
K>> maxcount
maxcount =
2123959
K>> hist.BinCounts(1:20)
ans =
Columns 1 through 6
9199 2123959 124940 29258 10904 5065
Columns 7 through 12
2933 1943 1385 1049 778 730
Columns 13 through 18
513 454 505 377 328 264
Columns 19 through 20
300 280 //
Columns 745 through 750
0 1 0 0 1 1
Columns 751 through 756
1 0 0 0 0 0
Columns 757 through 762
0 0 0 0 0 0
Columns 763 through 768
0 0 0 0 0 0
Columns 769 through 770
0 1
Around 679 it becomes just zero and ones.
The image is 12 bit due to the camera only gives 12 bit snapshots via gigecam and snapshot function but MATLAB converts the image matrix to 16 bit.

Cris LaPierre on 17 Feb 2021
Have you tried imhist?
Happy PhD on 17 Feb 2021
It doesn't seem to be working plotting it in appdesigner and it doesn't show much. I can barely see any lines.

Steven Lord on 17 Feb 2021
You haven't specified the bin edges, the bin method, or the bin width in your histogram call. Because of this I'd be concerned that your bins may be wide enough that they count multiple values in your data.
x = randi(1e4, 1, 1e6);
subplot(2, 1, 1)
h1 = histogram(x);
subplot(2, 1, 2)
h2 = histogram(x, 'BinMethod', 'integers'); % Each integer gets its own bin
fprintf("The bins in histogram h1 are %d units wide, " + ...
"while the bins in histogram h2 are %d unit wide.\n", h1.BinWidth, h2.BinWidth)
The bins in histogram h1 are 100 units wide, while the bins in histogram h2 are 1 unit wide.
fprintf("The first bin in h1 is the range [%d, %d) " + ...
"while the first bin in h2 is the range [%g, %g).\n", ...
h1.BinEdges(1:2), h2.BinEdges(1:2))
The first bin in h1 is the range [0, 100) while the first bin in h2 is the range [0.5, 1.5).

### Categories

Find more on Convert Image Type in Help Center and File Exchange

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by