Java BufferedImage Operation makeBinary(BufferedImage image, int threshold)

Here you can find the source of makeBinary(BufferedImage image, int threshold)

Description

Returns a binary image of a given image and a given threshold.

License

Open Source License

Parameter

Parameter Description
image The image to create a binary image from.
threshold The threshold value to use for generating this binary image.

Return

A binary image from the given image.

Declaration

public static BufferedImage makeBinary(BufferedImage image, int threshold) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;

public class Main {
    /**/*ww w  . ja  v  a  2  s . co m*/
     * Returns a binary image of a given image and a given threshold.
     * All pixels less than the threshold become black and all pixels greater than the threshold become white.
     * @param image The image to create a binary image from.
     * @param threshold The threshold value to use for generating this binary image.
     * @return A binary image from the given image.
     */
    public static BufferedImage makeBinary(BufferedImage image, int threshold) {
        BufferedImage copiedImage = copyBufferedImage(image);

        for (int x = 0; x < image.getWidth(); x++) {
            for (int y = 0; y < image.getHeight(); y++) {
                if (grayscaleFromRgb(image.getRGB(y, x)) <= threshold) {
                    copiedImage.setRGB(y, x, rgbFromGrayscale(0));
                } else {
                    copiedImage.setRGB(y, x, rgbFromGrayscale(255));
                }
            }
        }
        return copiedImage;
    }

    /**
     * Returns a copy of a BufferedImage.
     * @param bufferedImage The BufferedImage to make a copy of.
     * @return A copy of the BufferedImage.
     */
    public static BufferedImage copyBufferedImage(BufferedImage bufferedImage) {
        ColorModel colorModel = bufferedImage.getColorModel();
        boolean isAlphaPremultiplied = colorModel.isAlphaPremultiplied();
        WritableRaster writeableRaster = bufferedImage.copyData(null);
        return new BufferedImage(colorModel, writeableRaster, isAlphaPremultiplied, null);
    }

    /**
     * Converts an integer rgb value into a grayscale value.
     * The RGB values of grayscale images are the same, so we only need to grab red value to determine the grayscale
     * value.
     * @param rgb An integer representing a rgba color value using the RGB color space.
     * @return A grayscale value such that (0 >= value <= 255).
     */
    public static int grayscaleFromRgb(int rgb) {
        return (rgb >> 16) & 0xFF;
    }

    /**
     * Returns an rgb integer in RGB color space from the specified red, green, and blue values.
     * @param red The red value (0 >= red <= 255).
     * @param green The green value (0 >= green <= 255).
     * @param blue The blue value (0 >= blue <= 255).
     * @return The rgb integer value in RGB color space.
     */
    public static int getRgb(int red, int green, int blue) {
        return (red << 16) | (green << 8) | blue | (255 << 24);
    }

    /**
     * Converts a grayscale value into an RGB color space rgb integer.
     * @param grayscale The grayscale value such that (0 >= value <= 255)
     * @return An rgb integer in RGB color space.
     */
    public static int rgbFromGrayscale(int grayscale) {
        return getRgb(grayscale, grayscale, grayscale);
    }
}

Related

  1. intBuffer2BufferedImage(IntBuffer ib, BufferedImage img)
  2. intensityArrayToBufferedImage(byte[] array, int w, int h)
  3. interp(BufferedImage img1, BufferedImage img2, int weight1, int weight2)
  4. knit(BufferedImage[] buffImages)
  5. layer(BufferedImage source, BufferedImage destination, int x, int y)
  6. makeCursor(BufferedImage img, int hotx, int hoty, String name)
  7. makeGhost(BufferedImage image)
  8. makeImageTranslucent(BufferedImage source, float alpha)
  9. makeItBlack(BufferedImage image)