package utils; import java.awt.image.BufferedImage; public class sobel { static double Gx[][]; static double Gy[][]; public static double [][] Sobel_Gx () { return Gx; } public static double [][] Sobel_Gy () { return Gy; } public static void appy_sobel (BufferedImage image) { int i, j; int width = image.getWidth(); int height = image.getHeight(); Gx = new double[height][width]; Gy = new double[height][width]; int[][] img = readLuminance (image); double[] I = new double[height*width]; double[] T = new double[height*width]; double[][] F = new double[height][width]; int k = 0; for (i = 0; i < height; i++) { for (j = 0; j < width; j++) { I[k] = (double)(img[i][j])/255.0; F[i][j] = (double)(img[i][j])/255.0; k++; } } image_functions.write_pgm (I, width, height, 0, 1, "Norm"); /*GaussianFilter filter = new GaussianFilter(5); filter.filter (I, T, width, height); image_functions.write_pgm (T, width, height, 0, 1, "FILTER"); k = 0; for (i = 0; i < height; i++) { for (j = 0; j < width; j++) { F[i][j] = T[k]; k++; } }*/ /*double[][]Lala = Gaussian.smooth(F, height, width, 9, 0.0); image_functions.write_pgm (Lala, width, height, 0, 255, "Filter");*/ for (i=0; i < height; i++) { for (j=0; j < width; j++) { if (i==0 || i==height-1 || j==0 || j==width-1) Gx[i][j] = Gy[i][j] = 0; // Image boundary cleared else{ Gy[i][j] = 3*F[i+1][j-1] + 10*F[i+1][j] + 3*F[i+1][j+1] - 3*F[i-1][j-1] - 10*F[i-1][j] - 3*F[i-1][j+1]; Gx[i][j] = 3*F[i-1][j+1] + 10*F[i][j+1] + 3*F[i+1][j+1] - 3*F[i-1][j-1] - 10*F[i][j-1] - 3*F[i+1][j-1]; } } } } private static int[][] readLuminance (BufferedImage image) { int width = image.getWidth(); int height = image.getHeight(); int picsize = width * height; int type = image.getType(); int[][] data = new int[height][width]; if (type == BufferedImage.TYPE_INT_RGB || type == BufferedImage.TYPE_INT_ARGB) { int[] pixels = (int[]) image.getData().getDataElements(0, 0, width, height, null); for (int i = 0; i < picsize; i++) { int p = pixels[i]; int r = (p & 0xff0000) >> 16; int g = (p & 0xff00) >> 8; int b = p & 0xff; int col = i % width; int row = i / width; data[row][col] = luminance(r, g, b); } } else if (type == BufferedImage.TYPE_BYTE_GRAY) { byte[] pixels = (byte[]) image.getData().getDataElements(0, 0, width, height, null); for (int i = 0; i < picsize; i++) { int col = i % width; int row = i / width; data[row][col] = (pixels[i] & 0xff); } } else if (type == BufferedImage.TYPE_USHORT_GRAY) { short[] pixels = (short[]) image.getData().getDataElements(0, 0, width, height, null); for (int i = 0; i < picsize; i++) { int col = i % width; int row = i / width; data[row][col] = (pixels[i] & 0xffff) / 256; } } else if (type == BufferedImage.TYPE_3BYTE_BGR) { byte[] pixels = (byte[]) image.getData().getDataElements(0, 0, width, height, null); int offset = 0; for (int i = 0; i < picsize; i++) { int b = pixels[offset++] & 0xff; int g = pixels[offset++] & 0xff; int r = pixels[offset++] & 0xff; int col = i % width; int row = i / width; data[row][col] = luminance(r, g, b); } } else { throw new IllegalArgumentException("Unsupported image type: " + type); } return data; } private static int luminance(float r, float g, float b) { return Math.round(0.299f * r + 0.587f * g + 0.114f * b); } }