1.使用imhist()函數求圖片直方圖的時候,如果是RGB彩色圖,則要首先調用rgb2gray()函數將其轉化為灰度圖。
eg:

1 ImageData=imread('baby.jpg'); 2 I=rgb2gray(ImageData ); 3 figure(1); 4 subplot(2,1,1); 5 imshow(ImageData); 6 subplot(2,1,2); 7 imshow(I); 8 figure(2); 9 imhist(uint8(I) );
執行結果如下:
2.bar可以用作繪制條狀圖、stem()函數繪制桿狀圖、plot()函數用來繪制圖像最常見。
{
linspace()函數:
令A=linspace(1,100) 回車:然后會看到結果生成的是1到100之間的整數,一共100個數字,我們可以看到默認情況下linspace(a1,a2) 是生成包括a1 a2在內的等差數組。
linspace(a1,a2,N) 此函數是用來生成a1與a2之間等距的數組,間距d=(a2-a1)/(N-1)。例如B=linspace(0,9,9)我們可以看到結果如下:
B =
0 1.1250 2.2500 3.3750 4.5000 5.6250 6.7500 7.8750 9.0000
因為間距d = 1.125=(9-0)/(9-1)。
}
{
stem()函數:
stem(horz, z, 'fill')中,'fill'參數用來指示桿狀圖的標記點的顏色和線條的顏色,fill指示默認的‘藍色,實線,圓’。如果換成‘r--p’就變成了'紅色,虛線,五角星形'。詳細的參數表格見《數字圖像處理(MATLAB版)》P35。
}

1 ImageData=imread('baby.jpg'); 2 f=rgb2gray(ImageData ); 3 figure(1); 4 imhist(f); %最原始的結果 5 6 figure(2); 7 h = imhist(f,25); 8 horz = linspace(0,255,25); 9 bar(horz,h) %條形統計圖 10 axis([0 255 0 60000] ) 11 set(gca,'xtick',0:50:255) 12 set(gca,'ytick',0:20000:60000) 13 14 figure(3); 15 stem(horz,h,'fill'); %桿狀圖 16 axis([0 255 0 60000]) 17 set(gca,'xtick',0:50:255) 18 set(gca,'ytick',0:20000:60000) 19 20 figure(4); 21 plot(imhist(f)); 22 axis([0 255 0 15000]) 23 set(gca,'xtick',0:50:255) 24 set(gca,'ytick',0:20000:60000)
結果如下圖:
3.直方圖均衡。g= histeq(f,nlev),f為輸入圖像,nlev為輸出圖像設定的灰度級數,默認為64。

1 %{ 2 ImageData=imread('test.tif'); 3 f=rgb2gray(ImageData ); 4 %} 5 clear,clc; 6 close all; 7 f = imread('test.tif'); 8 figure,imhist(f); %最原始的結果 9 title('原圖像直方圖'); 10 ylim('auto'); 11 g = histeq(f,256); 12 figure,imshow(g) 13 title('均衡化處理后的圖像'); 14 figure,imhist(g) 15 title('均衡化后的直方圖'); 16 ylim('auto'); 17 18 hnorm = imhist(f)./numel(f); 19 cdf = cumsum(hnorm); 20 x = linspace(0,1,256); 21 figure,plot(x,cdf); 22 title('變換函數'); 23 axis([0 1 0 1]); 24 set(gca,'xtick',0:.2:1) 25 set(gca,'ytick',0:.2:1) 26 xlabel('Input intensity values','fontsize',9); 27 ylabel('Output intensity values','fontsize',9);
執行后結果為:
4.直方圖規定化(自定義規定函數)
1 f = imread('test.tif'); %讀入原圖像 2 p = manualhist; %輸入規定直方圖 3 g= histeq(f,p); %調用histeq函數得到得到規定化直方圖 4 figure,imhist(g); %顯示規定化后的直方圖
如代碼中所示,manualhist函數用來得到規定化直方圖,並畫出圖像。 twomodegauss用來計算一個歸一化到單位區域的雙模態高斯函數。
manualhist函數為:

1 function p = manualhist 2 %MANUALHIST Generates a two-mode histogram interactively. 3 % P = MANUALHIST generates a two-mode histogram using 4 % TWOMODEGAUSS(m1, sig1, m2, sig2, A1, A2, k). m1 and m2 are the 5 % means of the two modes and must be in the range [0,1]. sig1 and 6 % sig2 are the standard deviations of the two modes. A1 and A2 are 7 % amplitude values, and k is an offset value that raised the 8 % "floor" of histogram. The number of elements in the histogram 9 % vector P is 256 and sum(P) is normalized to 1. MANUALHIST 10 % repeatedly prompts for the parameters and plots the resulting 11 % histogram until the user types an 'x' to quit, and then it returns 12 % the last histogram computed. 13 % 14 % A good set of starting values is: (0.15, 0.05, 0.75, 0.05, 1, 15 % 0.07, 0.002). 16 17 % Initialize. 18 repeats = true; 19 quitnow = 'x'; 20 21 % Compute a default histogram in case the user quits before 22 % estimating at least one histogram. 23 p = twomodegauss(0.15, 0.05, 0.75, 0.05, 1, 0.07, 0.002); 24 25 % Cycle until an x is input. 26 while repeats 27 s = input('Enter m1, sig1, m2, sig2, A1, A2, k OR x to quit:','s'); 28 if s == quitnow 29 break 30 end 31 32 % Convert the input string to a vector of numerical values and 33 % verify the number of inputs. 34 v = str2num(s); 35 if numel(v) ~= 7 36 disp('Incorrect number of inputs') 37 continue 38 end 39 40 p = twomodegauss(v(1), v(2), v(3), v(4), v(5), v(6), v(7)); 41 % Start a new figure and scale the axes. Specifying only xlim 42 % leaves ylim on auto. 43 figure, plot(p) 44 xlim([0 255]) 45 end

1 function p = twomodegauss(m1, sig1, m2, sig2, A1, A2, k) 2 %TWOMODEGAUSS Generates a two-mode Gaussian function. 3 % P = TWOMODEGAUSS(M1, SIG1, M2, SIG2, A1, A2, K) generates a 4 % two-mode, Gaussian-like function in the interval [0,1]. P is a 5 % 256-element vector normalized so that SUM(P) equals 1. The mean 6 % and standard deviation of the modes are (M1, SIG1) and (M2, 7 % SIG2), respectively. A1 and A2 are the amplitude values of the 8 % two modes. Since the output is normalized, only the relative 9 % values of A1 and A2 are important. K is an offset value that 10 % raises the "floor" of the function. A good set of values to try 11 % is M1=0.15, S1=0.05, M2=0.75, S2=0.05, A1=1, A2=0.07, and 12 % K=0.002. 13 14 % Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins 15 % Digital Image Processing Using MATLAB, Prentice-Hall, 2004 16 % $Revision: 1.6 $ $Date: 2003/10/13 00:54:47 $ 17 18 c1 = A1 * (1 / ((2 * pi) ^ 0.5) * sig1); 19 k1 = 2 * (sig1 ^ 2); 20 c2 = A2 * (1 / ((2 * pi) ^ 0.5) * sig2); 21 k2 = 2 * (sig2 ^ 2); 22 z = linspace(0, 1, 256); 23 24 p = k + c1 * exp(-((z - m1) .^ 2) ./ k1) + ... 25 c2 * exp(-((z - m2) .^ 2) ./ k2); 26 p = p ./ sum(p(:));
執行結果為:
5.函數adapthisteq():自適應直方圖均衡,這種方法用直方圖匹配的方法來逐個處理圖像中的較小區域。然后用雙線性內插的方法來逐個將相鄰的小片組合起來,從而消除人工引入的邊界。特別是在均勻灰度區域,可以限制對比度來避免放大噪聲。adapthisteq的語法是g = adapthisteq(f,param1,val1,param2,val2,...);
eg:

1 clear,clc; 2 close all; 3 f = imread('test.tif'); %讀入原圖像 4 imhist(f); 5 6 g1 = adapthisteq(f); 7 figure,imhist(g1); 8 9 g2 = adapthisteq(f,'NumTiles',[25,25], 'ClipLimit',0.05 ); 10 figure,imhist(g2);
得到的結果如下:很明顯,均衡化處理的結果越來越好: