Java 计算能量

Java 计算能量,java,image,pixel,energy,Java,Image,Pixel,Energy,我试图用梯度公式计算一个像素的能量,不管我遇到什么问题,出于某种原因,除了图像的边缘,所有的东西都可以计算。例如 从命令行读取6x5图像时,答案应如下所示: 57685 50893 91370 25418 33055 37246 15421 56334 22808 54796 11641 25496 12344 19236 52030 17708 44735 20663 17074 23678

我试图用梯度公式计算一个像素的能量,不管我遇到什么问题,出于某种原因,除了图像的边缘,所有的东西都可以计算。例如
从命令行读取6x5图像时,答案应如下所示:

     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
但是我越来越

     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
我的能量法

     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;
}
公共双能源(整数x,整数y){ 如果(x<0 | | x>=width()){ 抛出新的IndexOutOfBoundsException(“x必须介于0和“+(width()-1)”之间); } 如果(y<0 | | y>=高度()){ 抛出新的IndexOutOfBoundsException(“y必须介于0和“+(height()-1)”之间); } //边境案件 如果(x==0 | | y==0 | | x==(宽度()-1)| | y==(高度()-1)){ 返回3*255*255; } 返回梯度(x,y); } 公共双梯度(整数x,整数y){ 颜色c1=图片获取(x-1,y); 颜色c2=图片获取(x+1,y); 颜色c3=图片。获取(x,y-1); 颜色c4=图片。获取(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); 返回deltaX+deltaY; }
问题在于代码中的这种情况:

// border case
if (x == 0 || y == 0 || x == (width() - 1) || y == (height() - 1)) {
    return 3 * 255 * 255;
}
这将返回所有边点的
195075


我不知道你用什么公式来得到你想要的答案中的边缘点能量,但是你应该在你的代码中使用相同的公式,而不是仅仅返回
195075

我想这是你的代码的问题。你能提供你的代码吗?很抱歉含糊不清,希望能有所帮助