i m having trouble in constructing a edge detector by using the sobel kernels and expected results are the vertical, horizontal and gradient edge image.
Sobel kernels :
[-1 0 1
hx = -2 0 2 and
-1 0 1]
[-1 -2 -1
hy = 0 0 0
1 2 1]
gradeint magnitude and direction:
g = (g2x + g2y) 1/2 and (theta) = tan-1 (gy/gx)
The following code can be used :
Code: ( java )
import java.io.*;
public class JPEG1{
public static void main (String args[]) {
if (args.length != 2) {
System.out.println("Usage: java JPEGCopy <source JPEG file> " +
"<target JPEG file>");
System.exit(1);
}
JPEGImage imageOne = new JPEGImage();
try {
imageOne.read(args[0]);
} catch (Exception e) {
/* An exception has been thrown. This is usually because the file
either does not exist, or is not a JPEG image */
System.out.println("Error reading file " + args[0]);
System.out.println(e.getMessage());
System.exit(1);
}
/* Make a new image the same size as the one that was read in */
JPEGImage imageTwo = new JPEGImage(imageOne.getWidth(),
imageOne.getHeight());
/* Copy the pixel information from image that was read in to the
new image */
for (int x = 0; x < imageOne.getWidth(); x++) {
for (int y = 0; y < imageOne.getHeight(); y++) {
/* Get the values from imageOne */
int red = imageOne.getRed(x,y);
int green = imageOne.getGreen(x,y);
int blue = imageOne.getBlue(x,y);
/* Put these values into imageTwo */
imageTwo.setRGB(x, y, red, green, blue);
}
}
/* Write the new image out to a file. Again exceptions might occur */
try {
imageTwo.write(args[1]);
} catch (Exception e) {
System.out.println("Error writing file " + args[1]);
System.out.println(e.getMessage());
System.exit(1);
}
}
}