空间滤波原理
使用大小为m*n的滤波器对大小为M*N的图像进行线性空间滤波,将滤波器模板乘以图像中对应灰度值,相加得模板中心灰度值
a = (m-1)/2, b = (n-1)/2
若f(x+s, y+t)不在原图内,补0
平滑线性滤波器滤波过程
分母为滤波器模板和
代码实现
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 /* linear filtering */ 21 short sum; 22 for (int i = 0; i < ARRAY_SIZE; i++) 23 for (int j = 0; j < ARRAY_SIZE; j++) 24 sum += average[i][j]; 25 26 for (int i = 0; i < height; i++){ 27 for (int j = 0; j < width; j++){ 28 value[0] = is_in_array(j-1, i-1, height, width) ? in_array[i-1][j-1] : 0; 29 value[1] = is_in_array(j, i-1, height, width) ? in_array[i-1][j] : 0; 30 value[2] = is_in_array(j+1, i-1, height, width) ? in_array[i-1][j+1] : 0; 31 value[3] = is_in_array(j-1, i, height, width) ? in_array[i][j-1] : 0; 32 value[4] = in_array[i][j]; 33 value[5] = is_in_array(j+1, i, height, width) ? in_array[i][j+1] : 0; 34 value[6] = is_in_array(j-1, i+1, height, width) ? in_array[i+1][j-1] : 0; 35 value[7] = is_in_array(j, i+1, height, width) ? in_array[i+1][j] : 0; 36 value[8] = is_in_array(j+1, i+1, height, width) ? in_array[i+1][j+1] : 0; 37 38 /* linear filtering */ 39 out_array[i][j] = (value[0] * average[0][0] + value[1] * average[0][1] + value[2] * average[0][2] + 40 value[3] * average[1][0] + value[4] * average[1][1] + value[5] * average[1][2] + 41 value[6] * average[2][0] + value[7] * average[2][1] + value[8] * average[2][2]) / sum; 42 43 } 44 } 45 }