c語言數字影象處理(八):噪聲模型及均值濾波器
阿新 • • 發佈:2018-12-18
影象退化/復原過程模型
高斯噪聲
PDF(概率密度函式)
生成高斯隨機數序列
演算法可參考<http://www.doc.ic.ac.uk/~wl/papers/07/csur07dt.pdf>
程式碼實現
1 double gaussrand() 2 { 3 static double V1, V2, S; 4 static int phase = 0; 5 double X; 6 7 if(phase == 0) { 8 do { 9 double U1 = (double)rand() / RAND_MAX;10 double U2 = (double)rand() / RAND_MAX; 11 12 V1 = 2 * U1 - 1; 13 V2 = 2 * U2 - 1; 14 S = V1 * V1 + V2 * V2; 15 } while(S >= 1 || S == 0); 16 17 X = V1 * sqrt(-2 * log(S) / S); 18 } else 19 X = V2 * sqrt(-2 * log(S) / S);20 21 phase = 1 - phase; 22 23 return X * 50; 24 }
生成高斯噪聲圖及直方圖
下面給一幅圖新增高斯噪聲
程式碼實現
1 void add_gaussian_noise(short** in_array, short** out_array, long height, long width) 2 { 3 srand(time(NULL)); 4 for (int i = 0; i < height; i++){ 5 for (int j = 0; j < width; j++){6 out_array[i][j] = in_array[i][j] + (short)gaussrand(); 7 if (out_array[i][j] < 0x00) 8 out_array[i][j] = 0x00; 9 else if (out_array[i][j] > 0xff) 10 out_array[i][j] = 0xff; 11 } 12 } 13 }
原圖
新增高斯噪聲
椒鹽噪聲
新增椒鹽噪聲(胡椒噪聲和鹽粒噪聲概率分別為5%)
1 void add_salt_pepper_noise(short** in_array, short** out_array, long height, long width) 2 { 3 srand(time(NULL)); 4 int noise_p; 5 6 for (int i = 0; i < height; i++){ 7 for (int j = 0; j < width; j++){ 8 noise_p = rand() % 10; 9 if (noise_p == 0){ 10 int temp = rand() % 2; 11 if (temp) 12 out_array[i][j] = 0x00; 13 else 14 out_array[i][j] = 0xff; 15 } 16 else 17 out_array[i][j] = in_array[i][j]; 18 } 19 } 20 }
均值濾波器
算術均值濾波器
程式碼實現
1 int is_in_array(short x, short y, short height, short width) 2 { 3 if (x >= 0 && x < width && y >= 0 && y < height) 4 return 1; 5 else 6 return 0; 7 } 8 9 /* 10 * element 11 * v0 v1 v2 12 * v3 v4 v5 13 * v6 v7 v8 14 * 15 */ 16 void filtering(short** in_array, short** out_array, long height, long width) 17 { 18 short value[9]; 19 20 for (int i = 0; i < height; i++){ 21 for (int j = 0; j < width; j++){ 22 value[0] = is_in_array(j-1, i-1, height, width) ? in_array[i-1][j-1] : 0; 23 value[1] = is_in_array(j, i-1, height, width) ? in_array[i-1][j] : 0; 24 value[2] = is_in_array(j+1, i-1, height, width) ? in_array[i-1][j+1] : 0; 25 value[3] = is_in_array(j-1, i, height, width) ? in_array[i][j-1] : 0; 26 value[4] = in_array[i][j]; 27 value[5] = is_in_array(j+1, i, height, width) ? in_array[i][j+1] : 0; 28 value[6] = is_in_array(j-1, i+1, height, width) ? in_array[i+1][j-1] : 0; 29 value[7] = is_in_array(j, i+1, height, width) ? in_array[i+1][j] : 0; 30 value[8] = is_in_array(j+1, i+1, height, width) ? in_array[i+1][j+1] : 0; 31 32 /* Arithmetic Mean Filter */ 33 for (int k = 0; k < ARRAY_SIZE*ARRAY_SIZE; k++) 34 out_array[i][j] += value[k]; 35 out_array[i][j] /= ARRAY_SIZE*ARRAY_SIZE; 36 37 } 38 } 39 }
處理高斯噪聲
處理椒鹽噪聲
結論:算術平均濾波對於高斯噪聲和椒鹽噪聲都有一定的效果,但是同時會平滑影象
幾何均值濾波器
實現
1 void filtering(short** in_array, short** out_array, long height, long width) 2 { 3 short value[9]; 4 5 for (int i = 0; i < height; i++){ 6 for (int j = 0; j < width; j++){ 7 value[0] = is_in_array(j-1, i-1, height, width) ? in_array[i-1][j-1] : 0; 8 value[1] = is_in_array(j, i-1, height, width) ? in_array[i-1][j] : 0; 9 value[2] = is_in_array(j+1, i-1, height, width) ? in_array[i-1][j+1] : 0; 10 value[3] = is_in_array(j-1, i, height, width) ? in_array[i][j-1] : 0; 11 value[4] = in_array[i][j]; 12 value[5] = is_in_array(j+1, i, height, width) ? in_array[i][j+1] : 0; 13 value[6] = is_in_array(j-1, i+1, height, width) ? in_array[i+1][j-1] : 0; 14 value[7] = is_in_array(j, i+1, height, width) ? in_array[i+1][j] : 0; 15 value[8] = is_in_array(j+1, i+1, height, width) ? in_array[i+1][j+1] : 0; 16 17 /* Geometric Mean Filter */ 18 double product = 1.0; 19 for (int k = 0; k < ARRAY_SIZE*ARRAY_SIZE; k++) 20 product *= value[k]; 21 product = pow(product, 1.0 / 9.0); 22 out_array[i][j] = (short)product; 23 24 if (out_array[i][j] < 0x00) 25 out_array[i][j] = 0x00; 26 else if (out_array[i][j] > 0xff) 27 out_array[i][j] = 0xff; 28 } 29 } 30 }
幾何均值濾波器與算術均值濾波器相比,丟失的影象細節更少
諧波均值濾波器
實現
1 void filtering(short** in_array, short** out_array, long height, long width) 2 { 3 short value[9]; 4 5 for (int i = 0; i < height; i++){ 6 for (int j = 0; j < width; j++){ 7 value[0] = is_in_array(j-1, i-1, height, width) ? in_array[i-1][j-1] : 0; 8 value[1] = is_in_array(j, i-1, height, width) ? in_array[i-1][j] : 0; 9 value[2] = is_in_array(j+1, i-1, height, width) ? in_array[i-1][j+1] : 0; 10 value[3] = is_in_array(j-1, i, height, width) ? in_array[i][j-1] : 0; 11 value[4] = in_array[i][j]; 12 value[5] = is_in_array(j+1, i, height, width) ? in_array[i][j+1] : 0; 13 value[6] = is_in_array(j-1, i+1, height, width) ? in_array[i+1][j-1] : 0; 14 value[7] = is_in_array(j, i+1, height, width) ? in_array[i+1][j] : 0; 15 value[8] = is_in_array(j+1, i+1, height, width) ? in_array[i+1][j+1] : 0; 16 17 /* Harmonic Mean Filter */ 18 double sum = 0; 19 for (int k = 0; k < ARRAY_SIZE*ARRAY_SIZE; k++) 20 sum += 1.0 / value[k]; 21 out_array[i][j] = (short)(9.0 / sum); 22 23 if (out_array[i][j] < 0x00) 24 out_array[i][j] = 0x00; 25 else if (out_array[i][j] > 0xff) 26 out_array[i][j] = 0xff; 27 } 28 } 29 }
處理高斯噪聲
處理椒鹽噪聲
對鹽粒噪聲效果較好,不適用於胡椒噪聲,善於處理高斯噪聲
逆諧波均值濾波器
Q為濾波器的階數,Q為正時,消除胡椒噪聲,Q為負時消除鹽粒噪聲
Q=0為算術均值濾波器,Q=-1諧波均值濾波器
實現
1 void filtering(short** in_array, short** out_array, long height, long width) 2 { 3 short value[9]; 4 5 for (int i = 0; i < height; i++){ 6 for (int j = 0; j < width; j++){ 7 value[0] = is_in_array(j-1, i-1, height, width) ? in_array[i-1][j-1] : 0; 8 value[1] = is_in_array(j, i-1, height, width) ? in_array[i-1][j] : 0; 9 value[2] = is_in_array(j+1, i-1, height, width) ? in_array[i-1][j+1] : 0; 10 value[3] = is_in_array(j-1, i, height, width) ? in_array[i][j-1] : 0; 11 value[4] = in_array[i][j]; 12 value[5] = is_in_array(j+1, i, height, width) ? in_array[i][j+1] : 0; 13 value[6] = is_in_array(j-1, i+1, height, width) ? in_array[i+1][j-1] : 0; 14 value[7] = is_in_array(j, i+1, height, width) ? in_array[i+1][j] : 0; 15 value[8] = is_in_array(j+1, i+1, height, width) ? in_array[i+1][j+1] : 0; 16 17 /* Contra-Harmonic Mean Filter */ 18 int Q = 2; 19 double num = 0.0, den = 0.0; 20 for (int k = 0; k < ARRAY_SIZE*ARRAY_SIZE; k++){ 21 num += pow(value[k], Q+1); 22 den += pow(value[k], Q); 23 } 24 out_array[i][j] = (short)(num / den); 25 26 if (out_array[i][j] < 0x00) 27 out_array[i][j] = 0x00; 28 else if (out_array[i][j] > 0xff) 29 out_array[i][j] = 0xff; 30 } 31 } 32 }
Q = 2 消除胡椒噪聲
Q = -2消除鹽粒噪聲
Q = -2消除鹽粒噪聲後的影象使用Q = 2消除胡椒噪聲
再來一次
再來
此時椒鹽噪聲已經基本消除