-4

I am trying to calculate the energy of a pixel using the gradient formula how ever I am encountering a problem, for some reason everything but the edges of the image calculates. For example
Reading a 6x5 image from the command line the answer should look like this:

     57685   50893   91370   25418   33055   37246
     15421   56334   22808   54796   11641   25496
     12344   19236   52030   17708   44735   20663
     17074   23678   30279   80663   37831   45595
     32337   30796    4909   73334   40613   36556

but i'm getting

     195075  195075  195075  195075  195075  195075
     195075  56334   22808   54796   11641   195075
     195075  19236   52030   17708   44735   195075
     195075  23678   30279   80663   37831   195075
     195075  195075  195075  195075  195075  195075

my energy method

     public double energy(int x, int y) {

    if (x < 0 || x >= width()) {
        throw new IndexOutOfBoundsException("x must be between 0 and " + (width()-1));
    }
    if (y < 0 || y >= height()) {
        throw new IndexOutOfBoundsException("y must be between 0 and " + (height()-1));
    }
    // border case
    if (x == 0 || y == 0 || x == (width() - 1) || y == (height() - 1)) {
        return 3 * 255 * 255;
    }
    return Gradient(x, y);
}
public double Gradient(int x, int y) {
    Color c1 = picture.get(x-1, y);
    Color c2 = picture.get(x+1, y);
    Color c3 = picture.get(x, y - 1);
    Color c4 = picture.get(x, y + 1);
    double deltaX = Math.pow(c2.getRed() - c1.getRed(), 2) +
            Math.pow(c2.getBlue() - c1.getBlue(), 2) +
            Math.pow(c2.getGreen() - c1.getGreen(), 2);

    double deltaY = Math.pow(c4.getRed() - c3.getRed(), 2) +
            Math.pow(c4.getBlue() - c3.getBlue(), 2) +
            Math.pow(c4.getGreen() - c3.getGreen(), 2);
    return deltaX + deltaY;
}
Svetlin Zarev
  • 14,713
  • 4
  • 53
  • 82
WallyWest
  • 5
  • 1
  • 5

1 Answers1

0

The problem is this condition in your code:

// border case
if (x == 0 || y == 0 || x == (width() - 1) || y == (height() - 1)) {
    return 3 * 255 * 255;
}

This ends up returning 195075 for all of the edge points.

I don't know what formula you used to get the edge point energies in your desired answer, but you should use the same formula in your code instead of just returning 195075.

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
SamYonnou
  • 2,068
  • 1
  • 19
  • 23