有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

为什么图片是黑色的?

编写一个方法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;
    }

共 (1) 个答案

  1. # 1 楼答案

    当你创建一个这样的矩阵

    int[][] matrix1 = new 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
        }
    }