Java BufferedImage Filter filterMedian(BufferedImage img)

Here you can find the source of filterMedian(BufferedImage img)

Description

filter Median

License

Apache License

Declaration

public static BufferedImage filterMedian(BufferedImage img) 

Method Source Code


//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;
    }
}

Related

  1. filter(BufferedImage image, BufferedImageOp op)
  2. filterContrast(BufferedImage img)
  3. filterDetectLines(BufferedImage img)
  4. filterFillHoles(BufferedImage img)
  5. filterImage(BufferedImageOp bufferedImageOp, BufferedImage srcImg, BufferedImage dstImg)
  6. filterScale(BufferedImage img, float ratio)
  7. filterSmooth(BufferedImage img)
  8. filterThreshold(BufferedImage img, int threshold)
  9. horizontalFilter(BufferedImage bufImg, int startX, int stopX, int start, int stop, int y, double[] pContrib)