Java BufferedImage Operation binarize(BufferedImage original)

Here you can find the source of binarize(BufferedImage original)

Description

binarize

License

Open Source License

Parameter

Parameter Description
original a parameter

Return

binarized image.

Declaration

public static BufferedImage binarize(BufferedImage original) 

Method Source Code


//package com.java2s;
/*//  w w w .  j av a  2 s .  c o m
  This file is part of Cuber.
    
Cuber is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
Cuber is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with Cuber.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Color;

import java.awt.image.BufferedImage;

public class Main {
    /**
     * @param original
     * @return binarized image.
     * @since 21/06/2013
     * @author wonka
     */
    public static BufferedImage binarize(BufferedImage original) {
        int red;
        int newPixel;

        int threshold = otsuTreshold(original);

        BufferedImage binarized = new BufferedImage(original.getWidth(), original.getHeight(), original.getType());

        for (int i = 0; i < original.getWidth(); i++) {
            for (int j = 0; j < original.getHeight(); j++) {
                // Get pixels
                red = new Color(original.getRGB(i, j)).getRed();
                int alpha = new Color(original.getRGB(i, j)).getAlpha();
                if (red > threshold) {
                    newPixel = 255;
                } else {
                    newPixel = 0;
                }

                newPixel = colorFromRGBA(newPixel, newPixel, newPixel, alpha);
                binarized.setRGB(i, j, newPixel);
            }
        }
        return binarized;
    }

    public static int otsuTreshold(BufferedImage original) {
        int[] histogram = imageHistogram(original);
        int total = original.getHeight() * original.getWidth();

        float sum = 0;
        for (int i = 0; i < 256; i++) {
            sum += i * histogram[i];
        }

        float sumB = 0;
        int wB = 0;
        int wF = 0;

        float varMax = 0;
        int threshold = 0;

        for (int i = 0; i < 256; i++) {
            wB += histogram[i];
            if (wB == 0) {
                continue;
            }
            wF = total - wB;

            if (wF == 0) {
                break;
            }

            sumB += i * histogram[i];
            float mB = sumB / wB;
            float mF = (sum - sumB) / wF;

            float varBetween = (float) wB * (float) wF * (mB - mF) * (mB - mF);

            if (varBetween > varMax) {
                varMax = varBetween;
                threshold = i;
            }
        }
        return threshold;
    }

    /**
     * Convert R, G, B, Alpha to standard 8 bit
     * 
     * @param alpha
     * @param red
     * @param green
     * @param blue
     * @return standard 8 bit
     * @since 21/06/2013
     * @author wonka
     */
    public static int colorFromRGBA(int red, int green, int blue, int alpha) {
        int newPixel = 0;
        newPixel += alpha;
        newPixel = newPixel << 8;
        newPixel += red;
        newPixel = newPixel << 8;
        newPixel += green;
        newPixel = newPixel << 8;
        newPixel += blue;

        return newPixel;
    }

    /**
     * @param input
     * @return Return histogram of grayscale image
     * @since 21/06/2013
     * @author wonka
     */
    public static int[] imageHistogram(BufferedImage input) {
        int[] histogram = new int[256];

        for (int i = 0; i < histogram.length; i++) {
            histogram[i] = 0;
        }

        for (int i = 0; i < input.getWidth(); i++) {
            for (int j = 0; j < input.getHeight(); j++) {
                int red = new Color(input.getRGB(i, j)).getRed();
                histogram[red]++;
            }
        }

        return histogram;
    }
}

Related

  1. applyMaskImage(BufferedImage src, int x, int y, int w, int h)
  2. applyShadow(BufferedImage image)
  3. areBufferedImagesEqual(BufferedImage img1, BufferedImage img2)
  4. autoPanImage(BufferedImage img, Color bgcolor)
  5. binarize(BufferedImage image)
  6. binary(BufferedImage src)
  7. blackAndWhiteCleaning(BufferedImage image)
  8. boostBufferedImagePerformance(BufferedImage image, boolean translucent)
  9. buildColorStatisticsOfImage(BufferedImage image)