This is about edge detection using java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moon123
    New Member
    • Feb 2008
    • 2

    #1

    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 :

    [code=java]
    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.prin tln("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(a rgs[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.prin tln("Error reading file " + args[0]);
    System.out.prin tln(e.getMessag e());
    System.exit(1);
    }

    /* Make a new image the same size as the one that was read in */
    JPEGImage imageTwo = new JPEGImage(image One.getWidth(),
    imageOne.getHei ght());

    /* Copy the pixel information from image that was read in to the
    new image */
    for (int x = 0; x < imageOne.getWid th(); x++) {
    for (int y = 0; y < imageOne.getHei ght(); y++) {
    /* Get the values from imageOne */
    int red = imageOne.getRed (x,y);
    int green = imageOne.getGre en(x,y);
    int blue = imageOne.getBlu e(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.prin tln("Error writing file " + args[1]);
    System.out.prin tln(e.getMessag e());
    System.exit(1);
    }

    }
    }[/code]
    Last edited by JosAH; Feb 28 '08, 06:30 AM. Reason: please use [code] ... [/code] tag
Working...