Search code examples
javaedge-detection

Edge detection in Java on a PPM using the Sobel Operator


I am trying to do edge detection, using the Sobel Operator, and I am getting a weird triplicate image.

The image starts life as a PPM, which I am storing as a multi-dimension array of Color's: Original image

I convert it to grayscale, which seems to work just fine: Grayscale converted image

but when I attempt to find edges, things go.. weird: Edges found?

Given that the code is in Java, it's rather verbose, so I've only included the conversion function. If it's suitable to post the whole thing, I will.

private Color[][] sobelConvert(Color[][] image) {
    Color[][] newPPM = new Color[maxX][maxY];

    for (int y = 0; y < maxY; y++) {
        for (int x = 0; x < maxX; x++) {
            int a = testForColor(x-1, y-1, image);
            int b = testForColor(x-1, y,   image);
            int c = testForColor(x-1, y+1, image);

            int d = testForColor(x, y-1, image);
            int e = testForColor(x, y+1,   image);

            int f = testForColor(x+1, y-1, image);
            int g = testForColor(x+1, y,   image);
            int h = testForColor(x+1, y+1, image);

            int eH = (c + 2*e + h) - (a + 2*d + f);
            int eV = (f + 2*g + h) - (a + 2*b + c);

            int edgyness = (int) Math.sqrt((eH * eH) + (eV * eV));
            if (edgyness > 255) {
                edgyness = 255;
            }
            if (edgyness < 0) {
                edgyness = 0;
            }
            newPPM[x][y] = new Color(edgyness, edgyness, edgyness);
        }
    }
    return newPPM;
}

testForColor() does some range checking, and returns one of the RGB values in the Color object - so that I think I can figure out brightness.

private int testForColor(int x, int y, Color[][] ppm) {
    if (x < 0 || x >= maxX) {
        return 0;
    }

    if (y < 0 || y >= maxY) {
        return 0;
    }

    return ppm[x][y].getGreen();
}

Edit: Adding code to read & write the PPM:

public Edgeness(String fileName) throws Exception {
    this.fileName = fileName;
    boolean foundDims = false;
    Color c = null;
    int r, g, b;

    String line;

    try {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        while ((line = br.readLine()) != null) {
            //System.out.println(line);

            // Test if the line has any lenght (ignore empty lines)
            // First line.  We will ignore it.
            // First character is a #.  Ignore it.  (this is bad - should check the whole line.  TODO.)
            if (line.length() > 0 && !line.equals("P3") && !(line.charAt(0) == '#')) {
                // Check for image dimensions.  Only do this once.  I assume regex's take longer
                // than testing a boolean.
                if (!foundDims) {
                    Pattern pImDim = Pattern.compile("(\\d+) (\\d+)");
                    Matcher mImDim = pImDim.matcher(line);
                    if (mImDim.find()) {
                        maxX = Integer.parseInt(mImDim.group(1));
                        maxY = Integer.parseInt(mImDim.group(2));
                        ppm = new Color[maxX][maxY];
                    }
                    foundDims = true;
                }

                // Hmm.  Last capture is kept, the rest are overwritten.  so.. we split instead.
                // https://stackoverflow.com/questions/3537878/how-to-capture-an-arbitrary-number-of-groups-in-javascript-regexp
                String[] rgbVals = line.split(" ");
                if (rgbVals.length % 3 == 0) {
                    for (int i = 0; i < rgbVals.length; i += 3) {
                        r = Integer.parseInt(rgbVals[i]);
                        g = Integer.parseInt(rgbVals[i+1]);
                        b = Integer.parseInt(rgbVals[i+2]);
                        c = new Color(r, g, b);
                        addNextColor(c);
                    }
                }
            }
        }
        br.close();
    } finally {
    }
}
private void addNextColor(Color c) {
    ppm[currentX][currentY] = c;
    currentY++;
    if (currentY >= maxY) {
        currentX++;
        currentY = 0;
    }
}

And the function to save the PPM out to a file. If I load a PPM, then immediately call render(), the image that is generated is an exact copy. Likewise if I save the grayscale image, I get the image included above.

private void render(Color[][] image, String fileName) throws IOException {
    ArrayList<String> output = new ArrayList<String>();
    output.add("P3");
    output.add(maxX + " " + maxY);
    output.add("255");

    for (int x = 0; x < maxX; x++) {
        StringBuilder sb = new StringBuilder();
        for (int y = 0; y < maxY; y++) {
            if (image[x][y] != null) {
                sb.append(image[x][y].getRed() + " " + image[x][y].getGreen() + " " + image[x][y].getBlue() + " ");
            } else {
                sb.append("0 0 0 ");
            }
        }
        output.add(sb.toString());
    }

    BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
    for (String s : output) {
        bw.write(s);
        bw.newLine();
    }
    bw.close();
}

I convert the PPM output to PNGs using ImageMagik's convert program.

I found an implementation written in Ruby at http://blog.saush.com/2011/04/20/edge-detection-with-the-sobel-operator-in-ruby/ which, when adapted to Java, yielded the same result.

FWIW, this is from Reddit's Daily Programming Challenge.


Solution

  • The problem is located somewhere else - the method works ok - in the display maybe

    Try to check each stage by saving the image produced with a

    BufferedImage bim=new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    int[] pix=new int[w*h];
    for(i=0; i<pix.length; i++) pix[i]=(p[i%w][i/w].getBlue()<<16)|(p[i%w][i/w].getBlue()<<8)|p[i%w][i/w].getBlue()|0xff000000;
    bim.setRGB(0, 0, w, h, pix, o, w);
    
    try {
    ImageIO.write(bim, "png", new File(path+".png"));
    } catch (IOException ex) { ex.printStackTrace(); }
    

    where p is your ppm array