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 :
Expand|Select|Wrap|Line Numbers
- import java.io.*;
- public class JPEG1{
- public static void main (String args[]) {
- /* Check that the user has provided the right number of arguments */
- if (args.length != 2) {
- System.out.println("Usage: java JPEGCopy <source JPEG file> " +
- "<target JPEG file>");
- System.exit(1);
- }
- /* Create an empty image. We will read an image from a file into
- this object */
- JPEGImage imageOne = new JPEGImage();
- /* Try to open the file. This may cause an exception if the name
- given is not a valid JPEG file so we need to catch the exceptions */
- 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);
- }
- }
- }