## Median Filter in C

The median filter is one of the familiar noise reduction filters in image processing. This filter exhibits its high performance for salt and pepper noise.

In this problem, we need to program an ordinary 3×3 median filter, whose input and output image format is ASCII portable gray map (PGM).

**Task**

Designing a median filter where each pixel value of an input image f is given by f (x, y), where x and y are the x and y coordinates of the pixel, respectively. It should be noted that x, y, and f (x, y) are integer values. The output of the median filter, g(x, y), is given by the following,

**g(x,y)=Med{f(x+i,y+j);i****=−1,0,1, j=−1,0,1}, **

(1) where the operator Med returns the median value of given nine pixels. For example, in the case of Med {100, 110, 120, 95, 105, 115, 92, 102, 112} ,

(2) at first the values are sorted as {92, 95, 100, 102, 105, 110, 112, 115, 120}, and then the fifth value (median value) of the sorted data, which is 105 in this case, is returned as the result of the operator Med. The output image can be obtained as a result of calculation for all pixels of g(x, y) using Eq. (1).

Assuming the image width and height are w and h, respectively, only the pixel values for 0 ≤ x < w and 0 ≤ y < h are valid. To obtain g(0, 0), invalid pixels such as f (−1, −1) are required. In this case, we use:

f (0, y) instead of f(−1,y)

f(w−1,y) instead of f(w,y)

f(x,0) instead of f(x,−1)

f(x,h−1) instead of f(x,h).

**Input**

The program gets its input image from the standard input stream. Input images are given as ASCII PGM format.

A sample of this format is shown as follows.

P2

640 480

255

23 39 46 44 28 17 21 33 40 40 39 30 17 11 13 13 13

14 14 12 20 39 41 39 27 19 31 40 41 38 29 19 23 21

The magic code (P2) in the first line indicates that this file is ASCII PGM. The values in the second line indicate the width (640) and height (480) of this image. The value in the third line indicates the maximum value (255) of each pixel. You can assume the maximum value is 255, which means 0 ≤ f (x, y) ≤ 255. After these three lines, the pixel values are stored in the raster scan order, which is the order of f(0,0), f(0,1),..., f(0,h−1), f(1,0),f(1,1),..., f(1,h− 1),..., f(w − 1,0), f(w − 1,1),..., f(w − 1,h − 1). The pixel values are separated by white spaces or line breaks. The pixel values can be parsed easily by using scanf.

**Output**

The program writes its output image to the standard output stream as the above-mentioned ASCII PGM format.

**Sample input**

P2

12 9

255

37 64 67 33 14 255 60 58 31 22 73 98

46 70 77 40 23 41 66 65 27 36 86 120

47 68 84 47 0 40 51 48 0 35 47 57

100 91 94 44 39 52 58 61 69 81 67 41

90 78 66 69 104 99 109 189 177 103 118 84

90 23 43 68 63 69 82 112 105 75 85 94

23 43 57 68 79 93 106 108 116 0 255 138

1 22 49 91 110 0 110 117 128 137 144 143

31 31 54 83 97 102 106 106 115 123 133 134

**Sample output**

P2

12 9

255

46 64 64 33 33 60 60 58 31 31 73 98

47 67 67 40 40 41 58 51 35 35 57 86

68 77 70 44 40 41 52 58 48 47 57 57

90 84 69 66 47 52 58 61 69 69 67 57

90 90 68 66 68 69 82 105 103 85 84 84

78 57 66 68 69 93 106 109 108 105 94 94

23 43 49 68 69 82 106 110 112 116 137 138

23 31 54 79 91 102 106 110 116 128 137 138

31 31 54 83 97 102 106 110 117 128 134 134

Tags :