469,283 Members | 2,158 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,283 developers. It's quick & easy.

This is about edge detection using java

constructing a edge detector using sobel kernels with a discrete convolution algorithm. The result should give a vertical edge image, a horizontal edge image and a gradient 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 :

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class JPEG1{
  4.  
  5.  
  6.     public static void main (String args[]) {
  7.     /* Check that the user has provided the right number of arguments */
  8.     if (args.length != 2) {
  9.         System.out.println("Usage: java JPEGCopy <source JPEG file> " + 
  10.                    "<target JPEG file>");
  11.         System.exit(1);
  12.     }
  13.  
  14.     /* Create an empty image. We will read an image from a file into
  15.        this object */
  16.     JPEGImage imageOne = new JPEGImage();
  17.  
  18.     /* Try to open the file. This may cause an exception if the name 
  19.        given is not a valid JPEG file so we need to catch the exceptions */
  20.     try {
  21.         imageOne.read(args[0]);
  22.     } catch (Exception e) {
  23.         /* An exception has been thrown. This is usually because the file
  24.            either does not exist, or is not a JPEG image */
  25.         System.out.println("Error reading file " + args[0]);
  26.         System.out.println(e.getMessage());
  27.         System.exit(1);
  28.     }
  29.  
  30.     /* Make a new image the same size as the one that was read in */
  31.     JPEGImage imageTwo = new JPEGImage(imageOne.getWidth(),
  32.                        imageOne.getHeight());
  33.  
  34.     /* Copy the pixel information from image that was read in to the 
  35.        new image */
  36.     for (int x = 0; x < imageOne.getWidth(); x++) {
  37.         for (int y = 0; y < imageOne.getHeight(); y++) {
  38.         /* Get the values from imageOne */
  39.         int red = imageOne.getRed(x,y);
  40.         int green = imageOne.getGreen(x,y);
  41.         int blue = imageOne.getBlue(x,y);
  42.         /* Put these values into imageTwo */
  43.         imageTwo.setRGB(x, y, red, green, blue);
  44.         }
  45.     }
  46.  
  47.     /* Write the new image out to a file. Again exceptions might occur */
  48.     try {
  49.         imageTwo.write(args[1]);
  50.     } catch (Exception e) {
  51.         System.out.println("Error writing file " + args[1]);
  52.         System.out.println(e.getMessage());
  53.         System.exit(1);
  54.     }
  55.  
  56.     }
  57. }
Feb 28 '08 #1
0 4753

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

23 posts views Thread by Andrew Thompson | last post: by
18 posts views Thread by Mickey Segal | last post: by
3 posts views Thread by jesper | last post: by
2 posts views Thread by a | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.