Java BufferedImage Filter filterThreshold(BufferedImage img, int threshold)

Here you can find the source of filterThreshold(BufferedImage img, int threshold)

Description

filter Threshold

License

Apache License

Declaration

public static BufferedImage filterThreshold(BufferedImage img, int threshold) 

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 filterThreshold(BufferedImage img, int threshold) {
        final int w = img.getWidth();
        final int h = img.getHeight();
        final BufferedImage newImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Color min = Color.BLACK;
        Color max = Color.WHITE;
        if (threshold < 0) {
            min = Color.WHITE;/*from w  w  w .  j a v  a  2 s. c  o m*/
            max = Color.BLACK;
            threshold = -threshold;
        }
        threshold *= 3;
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                final Color color = new Color(img.getRGB(x, y));
                if (color.getRed() + color.getGreen() + color.getBlue() < threshold) {
                    newImg.setRGB(x, y, min.getRGB());
                } else {
                    newImg.setRGB(x, y, max.getRGB());
                }
            }
        }
        return newImg;
    }
}

Related

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