//Kevin Liu //module for the median filter import java.io.*; import java.util.*; class Median { private int[][] data; private int numRow; private int numCol; private int[][] result; //takes as input the two dimensional array representing the input image public Median(int[][] image, int row, int column) { data = image; numRow = row; numCol = column; result = new int[numRow][numCol]; // this represents the resulting filtered image } public int[][] process() { int column; int row; int r; int c; int iterator; int[] surround = new int[9]; // slides the window across the image and copies the nine pixels inside into an array called "surround" for(row = 0; row < numRow; row++) { for(column = 0; column < numCol; column++) { if(row == 0 || row == numRow-1 || column == 0 || column == numCol-1) { result[row][column] = data[row][column]; continue; } iterator = 0; for(r=row-1; r < row+2; r++) for(c=column-1; c < column+2; c++) { surround[iterator] = data[r][c]; iterator++; } result[row][column] = insertionSortMedian(surround, 9); //calls the sorting method and enters the median value } } return result; } private int insertionSortMedian(int numbers[], int array_size) //sorts array and returns median value { int i, j, index; for(i = 0; i < array_size; i++) System.out.print(numbers[i] + " "); System.out.println(); for (i=1; i < array_size; i++) { index = numbers[i]; j = i; while ((j > 0) && (numbers[j-1] > index)) { numbers[j] = numbers[j-1]; j = j - 1; } numbers[j] = index; } for(i = 0; i < array_size; i++) System.out.print(numbers[i] + " "); System.out.println(); System.out.println(numbers[array_size/2]); return (numbers[array_size/2]); } }