package Segmentation; import utils.StatisticalSummary; public class niblack { StatisticalSummary ss; int size; double beta; int half; final static byte fg = -1; final static byte bg = +0; final static int F = 0; final static int B = 255; static int defaultSize = 15; static double defaultBeta = -0.2; public niblack () { this( defaultSize , defaultBeta ); } public niblack(int size, double beta) { System.err.println("In constructor"); this.size = size; half = size / 2; this.beta = beta; System.err.println("Entering SS"); ss = new StatisticalSummary(); System.err.println("Constructed!"); } public static int b2int (byte b) { return (256 + (int) b) % 256; } public byte filter(byte[][] source, int x, int y) { System.err.println("In filter"); ss.reset(); for (int i=-half; i<=half; i++) { for (int j=-half; j<=half; j++) { try { ss.add( b2int( source[x+i][y+j]) ); } catch (ArrayIndexOutOfBoundsException e) { // do nothing - this is apparently // cheaper than checking for illegal accesses } } } int thresh = (int) ( ss.mean() + beta * ss.sd() ); if ( b2int( source[x][y] ) > thresh ) { return fg; } else { return bg; } } public int filter(double[][] source, int x, int y) { System.err.println("In filter"); ss.reset(); for (int i=-half; i<=half; i++) { for (int j=-half; j<=half; j++) { try { ss.add(source[x+i][y+j]); } catch (ArrayIndexOutOfBoundsException e) { // do nothing - this is apparently // cheaper than checking for illegal accesses } } } int thresh = (int) ( ss.mean() + beta * ss.sd() ); if (source[x][y] > thresh) { return fg; } else { return bg; } } /* Sauvola protected int computePixel(Image image, int y, int x) { double mean = super.computePixel(image, y, x); double meanSquaresSum = (squaresImage.get(x - halfWindow, y - halfWindow) + squaresImage.get(x + halfWindow, y + halfWindow) - squaresImage.get(x + halfWindow, y - halfWindow) - squaresImage.get(x - halfWindow, y + halfWindow)) / squareWindow; // this is our supercool local variance double variance = meanSquaresSum - mean * mean; double thr = mean * (1 + weight * (Math.sqrt(variance) / range - 1)); if (image.get(x, y) > thr) { return above; } else { return below; } }*/ public int filter (double[] source, int x, int y, int w, int h, boolean inverse) { //System.err.println("In filter"); ss.reset(); for (int i=-half; i<=half; i++) { for (int j=-half; j<=half; j++) { int p = (y + j)*w + (x + i); try { ss.add(source[p]); } catch (ArrayIndexOutOfBoundsException e) { // do nothing - this is apparently // cheaper than checking for illegal accesses } } } int thresh = (int) ( ss.mean() + beta * ss.sd() ); int p = y * w + x; if ((p > 0) && (p < w*h) && (source[p] > thresh)) { if (inverse) { return F; } else { return B; } } else { if (inverse) { return B; } else { return F; } } } }