Sobel Operator MaskX

时间:2016-04-08 16:15:11

标签: java image mask sobel

为什么屏蔽x后的图像质量会变差?

public void doMaskX() {
    int[][] maskX = { { -1, -2, -1 }, { 0, 0, 0 }, { 1, 2, 1 } };

    int rgb, alpha = 0;
    int[][] square = new int[3][3];

    for (int y = 0; y < width - 3; y++) {
        for (int x = 0; x < height - 3; x++) {
            int sum = 0;
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    rgb = imgx.getRGB(y + i, x + j);

                    alpha = (rgb >> 24) & 0xff;
                    int red = (rgb >> 16) & 0xff;
                    int green = (rgb >> 8) & 0xff;
                    int blue = rgb & 0xff;

                    square[i][j] = (red + green + blue)/3;
                    sum += square[i][j] * maskX[i][j];
                }
            }
            rgb = (alpha << 24) | (sum << 16) | (sum << 8) | sum;
            imgx.setRGB(y, x, rgb);

        }
    }
    writeImg();
}

this is an image before masking

And this is the image after masking

质量应该优于第二张图像,为什么会出现黄色?

1 个答案:

答案 0 :(得分:1)

重要的是要意识到你在这里计算渐变的强度,这就是你正在显示的。因此,强度(或幅度)是正数。你必须采取绝对值:

sum=Math.abs(sum);

如果你也使用y导数,那么你可以结合使用:

sum=Math.sqrt(sumx*sumx+sumy*sumy);