matlab有兩個生成直方圖的庫函數,分別是imhist和histogram,二者有何區別呢?
區別就是:
- imhist
- 官方help:imhist(I) calculates the histogram for the intensity image I and displays a plot of the histogram. The number of bins in the histogram is determined by the image type.
- 可見,imhist的NumBins值是由圖像類型決定的。若圖像為uint8類型,則bin的數量為256,即[0:1:255]。
-
histogram(早期版本為hist,現在matlab已不建議使用hist了)
- 官方help:
histogram(X) creates a histogram plot of X. The histogram function uses an automatic binning algorithm that returns bins with a uniform width, chosen to cover the range of elements in X and reveal the underlying shape of the distribution. histogram displays the bins as rectangles such that the height of each rectangle indicates the number of elements in the bin.
nbins — Number of bins
Number of bins, specified as a positive integer. If you do not specify nbins, then histogram automatically calculates how many bins to use based on the values in X.- 可見,histogram的NumBins值可以人為設定,在未指定該參數時,系統將基於圖像的灰度分布自動計算NumBins的值。
- 官方help:
例程:
1 imgColor = imread('lena.jpg'); 2 imgGray = rgb2gray(imgColor); 3 4 %% imhist 5 figure('name', 'imhist'), 6 imhist(imgGray); 7 8 %% histogram 9 figure('name', 'histogram auto'), 10 % histogram函數自動計算NumBins值 11 hist2 = histogram(imgGray); 12 % Find the bin counts 13 binCounts = hist2.Values; 14 % Get bin number 15 binNum = hist2.NumBins; 16 17 %% histogram 指定NumBins值 18 % Specify number of histogram bins 19 figure('name', 'histogram256'), 20 hist256 = histogram(imgGray, 256); % 等同於 imhist(imgGray)
由運行結果也可看出,histogram(imgGray, 256)與imhist(imgGray),二者在最大bin的灰度累計值是一樣的。

