灰度變換函數 s = T(r) 其中r爲輸入圖像在(x, y)點處的灰度值,s爲輸出圖像在(x, y)點處的灰度值算法
灰度變換的做用函數
上圖所示的兩幅T(s)函數的圖像曲線,第一幅圖能夠加強圖像對比度,第二幅圖能夠對圖像進行二值化處理測試
1 void reverse(short** in_array, short** out_array, long height, long width) 2 { 3 for (int i = 0; i < height; i++){ 4 for (int j = 0; j <width; j++) 5 out_array[i][j] = GRAY_LEVELS - in_array[i][j]; 6 } 7 }
最簡單的灰度變換函數,將圖像中的每一個像素點處的顏色值反轉,對於8位灰度圖片,用255減去原灰度值spa
s = clog(1 + r) c爲常數,本次測試中c取10code
1 void logarithm(short** in_array, short** out_array, long height, long width) 2 { 3 for (int i = 0; i < height; i++){ 4 for (int j = 0; j <width; j++) 5 out_array[i][j] = (short)(10 * log((1 + in_array[i][j]))); 6 } 7 }
能夠看出,對數變換下降了圖像的對比度blog
s = crγ 其中 c 和 γ 爲正常數圖片
其中γ<1時,下降對比度,γ>1時,提升對比度數學
γ = 1.2it
1 void gamma(short** in_array, short** out_array, long height, long width) 2 { 3 for (int i = 0; i < height; i++){ 4 for (int j = 0; j <width; j++) 5 out_array[i][j] = (short)pow(in_array[i][j], 1.2); 6 } 7 }
直方圖爲離散函數h(rk) = nk, 其中rk是第k級灰度值,nk是圖像中h灰度爲rk的像素個數io
如今給出上面幾幅圖像的直方圖
能夠明顯看出,對比度越高的圖像,直方圖的分佈越均衡,所以直方圖均衡化算法能夠顯著提升圖像對比度
1 void calculate_histogram(long height, long width, short **image, unsigned long histogram[]) 2 { 3 short k; 4 for(int i=0; i < height; i++){ 5 for(int j=0; j < width; j++){ 6 k = image[i][j]; 7 histogram[k] = histogram[k] + 1; 8 } 9 } 10 } 11 12 void histogram_equalization(short** in_array, short** out_array, long height, long width) 13 { 14 unsigned long sum, sum_of_h[GRAY_LEVELS]; 15 double constant; 16 unsigned long histogram[GRAY_LEVELS] = {}; 17 18 calculate_histogram(height, width, in_array, histogram); 19 sum = 0; 20 for(int i=0; i < GRAY_LEVELS; i++){ 21 sum = sum + histogram[i]; 22 sum_of_h[i] = sum; 23 } 24 25 constant = (double)(GRAY_LEVELS)/(double)(height*width); 26 for(int i = 0, k = 0; i < height; i++){ 27 for(int j = 0; j < width; j++){ 28 k = in_array[i][j]; 29 out_array[i][j] = sum_of_h[k] * constant; 30 } 31 } 32 }