Java 为什么这幅画是黑色的?

Java 为什么这幅画是黑色的?,java,Java,编写一个方法Picture emboss,通过应用以下内核向图片添加浮雕样式效果 | -2 -1 0 | | -1 1 1 | | 0 1 2 | (它是一个3*3矩阵) 当对边界附近的像素应用核滤波器时,其某些相邻像素可能不存在。在这种情况下,假设最左边的列环绕到最右边的列,反之亦然;而最上面的一行环绕到最下面的一行,反之亦然 下面是我代码的一部分,但我发现我得到的图片全是黑色的。我对java的知识有限。所以在检查了一段时间后,我仍然找不到错误 public static Pi

编写一个方法Picture emboss,通过应用以下内核向图片添加浮雕样式效果

| -2 -1  0 |
| -1  1  1 |
|  0  1  2 | 
(它是一个3*3矩阵)

当对边界附近的像素应用核滤波器时,其某些相邻像素可能不存在。在这种情况下,假设最左边的列环绕到最右边的列,反之亦然;而最上面的一行环绕到最下面的一行,反之亦然

下面是我代码的一部分,但我发现我得到的图片全是黑色的。我对java的知识有限。所以在检查了一段时间后,我仍然找不到错误

public static Picture emboss(Picture picture) {
        int width= picture.width();
        int height= picture.height();

        int[][] matrix1 = new int[height][width];
        int[][] matrix2 = new int[height][width];
        int[][] matrix3 = new int[height][width];

        for (int col = 0; col < width; col++) {
            for (int row = 0; row < height; row++){
                Color color = picture.getColor(col, row);
                matrix1[row][col] = color.getRed();
                matrix2[row][col] = color.getGreen();
                matrix3[row][col] = color.getBlue();
                
                int a = row-1;
                int b = col-1;
                int c = row+1;
                int d = col+1;

                if (a < 0) {
                    a = height-1;
                }
                if (b < 0) {
                    b = width-1;
                }
                if (c > height-1) {
                    c = 0;
                }
                if ( d > width-1 ) {
                    d = 0;
                }

                int GR = -2 * matrix1[a][b] - matrix1[row][b] - matrix1[a][col] + matrix1[row][col] + matrix1[c][col] + matrix1[row][d] + 2*matrix1[c][d];
                int GG = -2 * matrix2[a][b] - matrix2[row][b] - matrix2[a][col] + matrix2[row][col] + matrix2[c][col] + matrix2[row][d] + 2*matrix2[c][d];
                int GB = -2 * matrix3[a][b] - matrix3[row][b] - matrix3[a][col] + matrix3[row][col] + matrix3[c][col] + matrix3[row][d] + 2*matrix3[c][d];

                if (GR < 0) {
                    GR=0;
                }
                if( GR>255 ) {
                    GR=255;
                }
                if ( GG<0 ) {
                    GG=0;
                }
                if( GG>255 ) {
                    GG=255;
                }
                if ( GB<0 ) {
                    GB=0;
                }
                if ( GB>255 ) {
                    GB=255;
                }

                Color newColor= new Color(GR,GG,GB);
                picture.setColor(col,row,newColor);
            }
        }
        return picture;
    }
公共静态图片浮雕(图片){
int width=picture.width();
int height=picture.height();
int[][]矩阵X1=新的int[高度][宽度];
int[]matrix2=新int[高度][宽度];
int[]matrix3=新int[高度][宽度];
for(int col=0;col高度-1){
c=0;
}
如果(d>宽度-1){
d=0;
}
int GR=-2*matrix1[a][b]-matrix1[row][b]-matrix1[a][col]+matrix1[row][col]+matrix1[c][col]+matrix1[row][d]+2*matrix1[c][d];
int GG=-2*matrix2[a][b]-matrix2[row][b]-matrix2[a][col]+matrix2[row][col]+matrix2[c][col]+matrix2[row][d]+2*matrix2[c][d];
int GB=-2*matrix3[a][b]-matrix3[row][b]-matrix3[a][col]+matrix3[row][col]+matrix3[c][col col]+matrix3[row][d]+2*matrix3[c][d];
if(GR<0){
GR=0;
}
如果(GR>255){
GR=255;
}
如果(GG255){
GG=255;
}
如果(GB255){
GB=255;
}
颜色newColor=新颜色(GR、GG、GB);
picture.setColor(列、行、新颜色);
}
}
返回图片;
}

当您创建这样的矩阵时

int[]matrix1=新的int[height][width]

默认情况下,它用0填充

看看你什么时候给了矩阵一个值。在for循环的第一次迭代中,所有内容都是黑色的。首先设置当前像素:

matrix1[row][col]=color.getRed()

但是所有的邻居都是黑人。在迭代中移动时,所有尚未计算的像素都是黑色的,并且在使用相邻像素时将其包括在计算中

您应该先进行一次迭代以填充这三个矩阵,然后再进行另一次迭代以进行计算:

for (int col = 0; col < width; col++) {
    for (int row = 0; row < height; row++){
        Color color = picture.getColor(col, row);
        matrix1[row][col] = color.getRed();
        matrix2[row][col] = color.getGreen();
        matrix3[row][col] = color.getBlue();
    }
}

for (int col = 0; col < width; col++) {
    for (int row = 0; row < height; row++){
        int a = row-1;
        int b = col-1;
        [...] // The rest of the code goes here
    }
}

for(int col=0;col
没错。我应该从一个迭代开始,将像素值添加到所有这些矩阵中,然后使用另一个迭代进行计算。谢谢你的帮助!