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的灰度累计值是一样的。