Image christians Method - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Effect

Description

Image christians Method

Demo Code


//package com.java2s;

import java.util.HashMap;

import java.util.Map;

public class Main {
    public static int[][] christiansMethod(int[][] image) {
        int h = image.length;
        int w = image[0].length;

        int d = 20;

        int dH = h / d;
        int dW = w / d;

        int[][] ret = new int[h][w];

        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                ret[i][j] = 255;/*from   w  ww.j  av a 2 s  . co  m*/
            }
        }

        double k = 0.5;

        int minVal = Integer.MAX_VALUE;
        double maxStDev = -1;
        // hash for maps is y + x * 100
        Map<Integer, Double> means = new HashMap<Integer, Double>();
        Map<Integer, Double> stDevs = new HashMap<Integer, Double>();
        for (int y = 0; y < d; y++) {
            for (int x = 0; x < d; x++) {
                // calculate min val
                int val = image[y][x];
                if (val < minVal) {
                    minVal = val;
                }

                // calculate local mean
                double mean = 0;
                for (int i = 0; i < dH; i++) {
                    for (int j = 0; j < dW; j++) {
                        mean += image[y * dH + i][x * dW + j];
                    }
                }

                mean /= dH * dW;
                means.put(y + x * 100, mean);

                // calculate local standard deviation
                double stDev = 0;
                for (int i = 0; i < dH; i++) {
                    for (int j = 0; j < dW; j++) {
                        stDev += Math.abs(mean
                                - image[y * dH + i][x * dW + j]);
                    }
                }

                stDev /= dH * dW;
                stDevs.put(y + x * 100, stDev);

                // calculate maximum local standard deviation
                if (stDev > maxStDev) {
                    maxStDev = stDev;
                }
            }
        }

        // apply method to locals
        for (int y = 0; y < d; y++) {
            for (int x = 0; x < d; x++) {
                double mean = means.get(y + x * 100);
                double stDev = stDevs.get(y + x * 100);
                int threshold = (int) ((1 - k) * mean + k * minVal + k
                        * (stDev / maxStDev) * (mean - minVal));

                int[][] tile = new int[dH][dW];
                int[][] cTile = null;

                for (int i = 0; i < dH; i++) {
                    for (int j = 0; j < dW; j++) {
                        tile[i][j] = image[y * dH + i][x * dW + j];
                    }
                }

                cTile = matrixToBinary(tile, threshold);

                for (int i = 0; i < dH; i++) {
                    for (int j = 0; j < dW; j++) {
                        ret[y * dH + i][x * dW + j] = cTile[i][j];
                    }
                }
            }
        }

        return ret;
    }

    public static int[][] matrixToBinary(int[][] image, int mean) {
        int w = image[0].length;
        int h = image.length;
        int[][] retVal = new int[h][w];

        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                if (image[y][x] < mean) {
                    retVal[y][x] = 0;
                } else {
                    retVal[y][x] = 255;
                }
            }
        }

        return retVal;
    }
}

Related Tutorials