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
採用された回答
Image Analyst
2014 年 8 月 9 日
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 件のコメント
Ely Raz
2014 年 8 月 9 日
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)?
Image Analyst
2014 年 8 月 9 日
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]);
Miguel Yanez
2016 年 7 月 8 日
Hi, I have not tried your script yet but I wonder if, Can it be used to create a Dicom Image Histogram?
Image Analyst
2016 年 7 月 8 日
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.
Nagabhushan SN
2018 年 8 月 27 日
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
2024 年 5 月 19 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
