Difference between hist and imhist
显示 更早的评论
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
采纳的回答
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 个评论
I understand that I need to use the imhist to generate the gray scale image histogram. When I want to generate it via hist it is completely different, I do not know why and therefore how can I generate an histogram that looks the same like imhist output (even without using the imhist)?
You were doing it wrong, like I said. hist() knows nothing about what the x-values (gray levels) should be - it just hists what is there and is totally ignorant that a uint8 variable goes from 0 to 255, or at least can, but doesn't for the image you took. Here, try this and maybe you'll understand:
close all;
grayImage = imread('pout.tif');
subplot(2,1,1);
[pixelCounts, grayLevels] = imhist(grayImage);
bar(grayLevels, pixelCounts, 'BarWidth', 1);
imhist(grayImage)
subplot(2,1,2);
counts = hist(double(grayImage(:)), 256);
% IMPORTANT WARNING: counts(1) is not for gray level 0!!!
bar(counts, 'BarWidth', 1);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
Hi, I have not tried your script yet but I wonder if, Can it be used to create a Dicom Image Histogram?
Yes. It does not care what format was used to store the image on disk, it just looks at an array of numbers, which is what you get when you read a disk file into MATLAB.
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?
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(:))]
ans = 1x2
0.0275 0.9922
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% 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);

更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Image Filtering 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
