// // Implementation of 'Binary Image' // // The following code creates a binary image by replacing every // pixel by it's compliment by either 0 or 255 depending on the // threshold of that region // public class binaryImage extends Object { private int inPixels[], width, height; binaryImage(int Pixels[], int w, int h) // the constructor { inPixels = new int[Pixels.length]; inPixels = Pixels; width = w; height = h; } // The following function binarizes a part of the image // startx,starty,endx and endy are the boundaries of the rectangular area public int[] binarizeSelection(int startx, int starty, int endx, int endy, int thold) { int outPixels[] = new int[inPixels.length]; outPixels = inPixels; for (int y = starty; y < endy; y++) for (int x = startx; x < endx; x++) { if (inPixels[y*width + x] > thold) outPixels[y*width + x] = 255; else outPixels[y*width + x] = 0; } return outPixels; } // The following function does local binarization by dividing the image in // 4 quadrants. Each quadrant can have a different threshold. For global // binarization, set all thresholds to the same value. public int[] binarize(int thold1,int thold2,int thold3,int thold4) { int outPixels[] = new int[inPixels.length]; for (int y = 0; y < height/2; y++) for (int x = width/2; x < width; x++) { if (inPixels[y*width + x] > thold1) outPixels[y*width + x] = 255; else outPixels[y*width + x] = 0; } for (int y = 0; y < height/2; y++) for (int x = 0; x < width/2; x++) { if (inPixels[y*width + x] > thold2) outPixels[y*width + x] = 255; else outPixels[y*width + x] = 0; } for (int y = height/2; y < height; y++) for (int x = 0; x < width/2; x++) { if (inPixels[y*width + x] > thold3) outPixels[y*width + x] = 255; else outPixels[y*width + x] = 0; } for (int y = height/2; y < height; y++) for (int x = width/2; x < width; x++) { if (inPixels[y*width + x] > thold4) outPixels[y*width + x] = 255; else outPixels[y*width + x] = 0; } return outPixels; } }