Histogram from imhist does not mach histogram from imtool

1 次查看(过去 30 天)
Hi,
I want to extract information out of an uint16 picture due to analyze the histogram.
But when I try to plot the Histogramm with " imhist "- funktion it does not mach the histogram from " imtool "- funktion at all.
Can anyone melp me?
As attached
img ---> 1024x1024 uint16
left picture --> imhist(img) right picture --> imtool(img,[])

回答(1 个)

Image Analyst
Image Analyst 2019-1-4
The problem is your values only go up to 2243 and it's 16 bits. The default number of bins for imhist() is 256 which means that all your counts fall into the leftmost 10 bins. If you want, you can try histogram() instead of imhist(), though you need to specify 256 bins or 65535 bins. If you specify 65535 bins, so that each gray level gets it's own bin, AND then scale your x axis so that it goes from 0 to 2243, then you can see the full shape of the histogram. Try to follow the illustrative code below.
Try this:
maxValue = max(A(:))
subplot(1, 3, 1);
imshow(A, [])
subplot(1, 3, 2);
% Call imhist with default 256 bins,
% so that all the image gray levels will be only in the 10 darkest bins.
[counts, grayLevels] = imhist(A);
bar(grayLevels, counts, 'BarWidth', 1);
title('Histogram with 256 bins', 'FontSize', 15);
% Values only go to 2243, not 65535 so histogram is
% all squished up on the left end. Only 10 bins are used.
% Use xlim() to set the x range so we can see it better.
xlim([1, grayLevels(10)]);
grid on;
% Try again with 65535 bins:
subplot(1, 3, 3);
[counts, grayLevels] = imhist(A, 65535);
bar(grayLevels, counts, 'BarWidth', 1);
% Scale to last value
lastIndex = find(counts > 0, 1, 'last');
xlim([1, grayLevels(lastIndex)]);
grid on;
title('Histogram with 65535 bins', 'FontSize', 15);
0000 Screenshot.png

Community Treasure Hunt

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

Start Hunting!

Translated by