Java BufferedImage Operation otsuTreshold(BufferedImage original)

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

Description

otsu Treshold

License

Open Source License

Declaration

public static int otsuTreshold(BufferedImage original) 

Method Source Code


//package com.java2s;
/*/*  ww  w  . j a v a2  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 {
    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;
    }

    /**
     * @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. openAsBufferedImage(String path)
  2. operatedImage(BufferedImage source, BufferedImageOp op)
  3. optimizeForGraphicsHardware(BufferedImage image)
  4. optimizeForGraphicsHardwareIfRequired(BufferedImage image)
  5. optimizeImage(BufferedImage image)
  6. output(BufferedImage image, OutputStream out)
  7. outPutImage(BufferedImage bufferedimage, String targetPath)
  8. paletteSwapARGB8(Color[] colorSet, Color clearToColorRequested, BufferedImage argbMappedBufferedImage)
  9. palettize(BufferedImage img, int maxEntries)