## 空間濾波原理

a = (m-1)/2, b = (n-1)/2

## 平滑線性濾波器

``` 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 }```

## 統計排序（非線性）濾波器

### 中值濾波器

``` 1 short mid_val(short* a, short num)
2 {
3     short temp;
4
5     for (int i = 0; i < num; i++)
6     {
7         temp = a[i];
8         int j = i;
9         for (; j > 0 && a[j - 1] > temp; j--)
10             a[j] = a[j - 1];
11         a[j] = temp;
12     }
13
14     return a[num/2];
15 }
16 int is_in_array(short x, short y, short height, short width)
17 {
18     if (x >= 0 && x < width && y >= 0 && y < height)
19         return 1;
20     else
21         return 0;
22 }
23
24 /*
25  * element
26  * v0  v1  v2
27  * v3  v4  v5
28  * v6  v7  v8
29  *
30  */
31 void filtering(short** in_array, short** out_array, long height, long width)
32 {
33     short value[9];
34
35     for (int i = 0; i < height; i++){
36         for (int j = 0; j < width; j++){
37             value[0] = is_in_array(j-1, i-1, height, width) ? in_array[i-1][j-1] : 0;
38             value[1] = is_in_array(j, i-1, height, width) ? in_array[i-1][j] : 0;
39             value[2] = is_in_array(j+1, i-1, height, width) ? in_array[i-1][j+1] : 0;
40             value[3] = is_in_array(j-1, i, height, width) ? in_array[i][j-1] : 0;
41             value[4] = in_array[i][j];
42             value[5] = is_in_array(j+1, i, height, width) ? in_array[i][j+1] : 0;
43             value[6] = is_in_array(j-1, i+1, height, width) ? in_array[i+1][j-1] : 0;
44             value[7] = is_in_array(j, i+1, height, width) ? in_array[i+1][j] : 0;
45             value[8] = is_in_array(j+1, i+1, height, width) ? in_array[i+1][j+1] : 0;
46
47             /* median filtering */
48             out_array[i][j] = mid_val(value, 9);
49
50         }
51     }
52 }```

## 銳化空間濾波器

### 使用二階微分進行圖像銳化-拉普拉斯算子

``` 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             /* sharpening filtering */
18             out_array[i][j] = value[0] * sharpen[0][0] + value[1] * sharpen[0][1] + value[2] * sharpen[0][2] +
19                               value[3] * sharpen[1][0] + value[4] * sharpen[1][1] + value[5] * sharpen[1][2] +
20                               value[6] * sharpen[2][0] + value[7] * sharpen[2][1] + value[8] * sharpen[2][2];
21             out_array[i][j] += in_array[i][j];
22             if (out_array[i][j] < 0)
23                 out_array[i][j] = 0;
24             else if (out_array[i][j] > 0xff)
25                 out_array[i][j] = 0xff;
26
27         }
28     }
29 }```

### 使用一階微分對（非線性）圖像銳化-梯度

``` 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             /* sharpening using grad */
33             out_array[i][j] = (short)abs(value[0] * soble1[0][0] + value[1] * soble1[0][1] + value[2] * soble1[0][2] +
34                                          value[3] * soble1[1][0] + value[4] * soble1[1][1] + value[5] * soble1[1][2] +
35                                          value[6] * soble1[2][0] + value[7] * soble1[2][1] + value[8] * soble1[2][2]) +
36                               (short)abs(value[0] * soble2[0][0] + value[1] * soble2[0][1] + value[2] * soble2[0][2] +
37                                          value[3] * soble2[1][0] + value[4] * soble2[1][1] + value[5] * soble2[1][2] +
38                                          value[6] * soble2[2][0] + value[7] * soble2[2][1] + value[8] * soble2[2][2]);
39         }
40     }
41 }```

### 邊緣增強

```1 /* edge enhancement */
2             if (out_array[i][j] < 0)
3                 out_array[i][j] = 0;
4             else if (out_array[i][j] > 0xff)
5                 out_array[i][j] = 0xff;
6             else if (out_array[i][j] > 200)
7                 ;
8             else
9                 out_array[i][j] = in_array[i][j];```