Here you can find the source of filterMedian(BufferedImage img)
public static BufferedImage filterMedian(BufferedImage img)
//package com.java2s; //License from project: Apache License import java.awt.Color; import java.awt.image.BufferedImage; public class Main { public static BufferedImage filterMedian(BufferedImage img) { final int MSIZE = 3; int x, y, i, j; int r;/* ww w . j av a2s. c o m*/ final int[] val = new int[MSIZE * MSIZE]; final int w = img.getWidth(); final int h = img.getHeight(); final BufferedImage dst = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); for (y = 0; y < h; y++) { for (x = 0; x < w; x++) { dst.setRGB(x, y, Color.WHITE.getRGB()); } } for (y = MSIZE / 2; y < h - MSIZE / 2; y++) { for (x = MSIZE / 2; x < w - MSIZE / 2; x++) { for (i = 0; i < MSIZE; i++) { for (j = 0; j < MSIZE; j++) { r = new Color(img.getRGB(x + j - MSIZE / 2, y + i - MSIZE / 2)).getRed(); val[i * MSIZE + j] = r; } } /* Bubble sort power! */ for (i = 0; i < MSIZE * MSIZE / 2 + 1; i++) { for (j = i + 1; j < MSIZE * MSIZE; j++) { if (val[i] > val[j]) { final int k = val[i]; val[i] = val[j]; val[j] = k; } } } i = val[MSIZE * MSIZE / 2]; dst.setRGB(x, y, new Color(i, i, i).getRGB()); } } return dst; } }