Difference between hist and imhist
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ely Raz
am 9 Aug. 2014
Kommentiert: DGM
am 19 Mai 2024
Hi,
I have a fundamental question about the hist and imhist and an explanation will be great as I am a bit lost.
I am running this script:
I = imread('pout.tif');
figure;
x=imhist(I)
imhist(I)
figure(2);
hist(x)
and when I compare the two histograms they do not look alike, why?
How can I save the imhist data so that the hist will show the same plot?
Thanks
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 9 Aug. 2014
For a gray scale image imhist() will give 256 bins. hist() only gives 10 by default. But you did not use hist() to take the histogram of the image - the badly-named "I". You used it to take a histogram of the histogram counts, x - a pretty weird/strange thing to do.
6 Kommentare
Nagabhushan SN
am 27 Aug. 2018
Hi @ImageAnalyst, you've mentioned that "IMPORTANT WARNING: counts(1) is not for gray level 0!!!" So do you mean that hist doesn't give histogram in order of intensity values? So, histogram plots by imhist(grayImage) and hist(double(grayImage(:)), 256) will be different?
DGM
am 19 Mai 2024
What that comment means is that when only the number of bins are specified, their location is entirely up to hist() to decide. Where does hist() put the bins? It arranges the bins such that they span the range of data in the input array. To be more specific, the outer edges of the end bins lie on the extrema of the input data, which will vary from image to image.
On the other hand, imhist() centers the end bins on the values which represent black and white for the numeric class of the input data. The bin locations are strictly a function of the numeric class, not the range of the data. For images of the same class, two count vectors of equal length represent the same ranges of gray levels.
In order to make hist() (or histc() or the newer histcounts()) have similar behavior, you need to specify the bin locations. Pretending we're still in <R2014b:
% we have some image which is properly-scaled for its class
inpict = imread('cameraman.tif');
inpict = im2double(inpict); % unit-scale
% the image data doesn't extend completely to [0 1]
[min(inpict(:)) max(inpict(:))]
% number of bins
N = 7;
% do it with imhist()
subplot(3,1,1);
[counts1 centers1] = imhist(inpict,N); % get the counts/centers
bar(centers1,counts1,1);
% hist() will put the bins whereever it wants
% unless you actually tell it where they go
subplot(3,1,2);
[counts2 centers2] = hist(double(inpict(:)),N); %#ok<HIST>
bar(centers2,counts2,1);
% replicate imhist()'s centered bins
xrange = getrangefromclass(inpict);
os = 0.5*diff(xrange)/(N-1);
breakpoints = linspace(xrange(1)-os,xrange(2)+os,N+1).';
centers3 = linspace(xrange(1),xrange(2),N).';
counts3 = histc(inpict(:),breakpoints); %#ok<HISTC>
counts3(end-1) = sum(counts3((end-1):end));
counts3 = counts3(1:end-1);
subplot(3,1,3);
bar(centers3,counts3,1);
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Histograms finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!