Java BufferedImage Filter filterSmooth(BufferedImage img)

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

Description

filter Smooth

License

Apache License

Declaration

public static BufferedImage filterSmooth(BufferedImage img) throws Exception 

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 filterSmooth(BufferedImage img) throws Exception {
        final int SSIZE = 3;
        int x, y, i, j, val;
        int r;//  w  ww . java 2  s . c  o m
        final int w = img.getWidth();
        final int h = img.getHeight();
        final BufferedImage newImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

        for (y = 0; y < h; y++) {
            for (x = 0; x < w; x++) {
                newImg.setRGB(x, y, img.getRGB(x, y));
            }
        }

        for (y = SSIZE / 2; y < h - SSIZE / 2; y++) {
            for (x = SSIZE / 2; x < w - SSIZE / 2; x++) {
                val = 0;
                for (i = 0; i < SSIZE; i++) {
                    for (j = 0; j < SSIZE; j++) {
                        final Color color = new Color(img.getRGB(x + j - SSIZE / 2, y + i - SSIZE / 2));
                        r = color.getRed();
                        val += r;
                    }
                }
                i = val / (SSIZE * SSIZE);
                newImg.setRGB(x, y, new Color(i, i, i).getRGB());
            }
        }
        /* Remove border */
        for (y = 0; y < h; y++) {
            newImg.setRGB(0, y, newImg.getRGB(1, y));
            newImg.setRGB(w - 1, y, newImg.getRGB(w - 2, y));
        }

        for (x = 0; x < w; x++) {
            newImg.setRGB(x, 0, newImg.getRGB(x, 1));
            newImg.setRGB(x, h - 1, newImg.getRGB(x, h - 2));
        }

        return newImg;
    }
}

Related

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