Here you can find the source of makeBinary(BufferedImage image, int threshold)
Parameter | Description |
---|---|
image | The image to create a binary image from. |
threshold | The threshold value to use for generating this binary image. |
public static BufferedImage makeBinary(BufferedImage image, int threshold)
//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); } }