HorizontalSobel.java
package ac.essex.ooechs.imaging.commons.apps.anpr;
import ac.essex.ooechs.imaging.commons.PixelLoader;
/**
* <p/>
* This program 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 2
* of the License, or (at your option) any later version,
* provided that any use properly credits the author.
* This program 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 at http://www.gnu.org
* </p>
*
* @author Olly Oechsle, University of Essex, Date: 15-Feb-2007
* @version 1.0
*/
public class HorizontalSobel {
public static double[][] Sobel(PixelLoader image) {
// STEP1 - Gaussian Blur
final int gaussianSize = 3;
int[][] matrix = new int[gaussianSize][gaussianSize];
matrix[0][0] = 2;
matrix[0][1] = 5;
matrix[0][2] = 2;
matrix[1][0] = 5;
matrix[1][1] = 10;
matrix[1][2] = 5;
matrix[2][0] = 2;
matrix[2][1] = 5;
matrix[2][2] = 2;
int maskTotal = 0;
for (int x = 0; x < gaussianSize; x++) {
for (int y = 0; y < gaussianSize; y++) {
maskTotal += matrix[x][y];
}
}
// STEP 2 - Perform Sobel Edge Detection using a pair of masks
int sobelSize = 3;
int[][] sobelGx = new int[sobelSize][sobelSize];
int[][] sobelGy = new int[sobelSize][sobelSize];
// Finds edges in the X direction
sobelGx[0][0] = -1;
sobelGx[0][1] = 0;
sobelGx[0][2] = 1;
sobelGx[1][0] = -2;
sobelGx[1][1] = 0;
sobelGx[1][2] = 2;
sobelGx[2][0] = -1;
sobelGx[2][1] = 0;
sobelGx[2][2] = 1;
// Finds edges in the Y direction
sobelGy[0][0] = 1;
sobelGy[0][1] = 2;
sobelGy[0][2] = 1;
sobelGy[1][0] = 0;
sobelGy[1][1] = 0;
sobelGy[1][2] = 0;
sobelGy[2][0] = -1;
sobelGy[2][1] = -2;
sobelGy[2][2] = -1;
// STEP 2: Sobel Edge Detection
double[][] sobelled = new double[image.getWidth()][image.getHeight()];
for (int y = (gaussianSize + sobelSize); y < image.getHeight() - (gaussianSize + sobelSize); y++) {
for (int x = (gaussianSize + sobelSize); x < image.getWidth() - (gaussianSize + sobelSize); x++) {
double gX = 0;
double gY = 0;
for (int mx = 0; mx < 3; mx++) {
for (int my = 0; my < 3; my++) {
gY += (image.getGreyValue(x - mx, y - my) * sobelGy[mx][my]);
}
}
sobelled[x][y] = gY;
int edgeStrength = (int) (Math.abs(gX) + Math.abs(gY));
if (edgeStrength > 0) {
// ensure that gX isn't exactly 0 (or we'd get a divide by 0 error).
if (gX == 0) gX = 0.00001;
// find the angle of the edge
double theta = Math.atan(gY / gX) / 0.0174532925;
}
}
}
return sobelled;
}
}