Sobel边缘检测Java

时间:2018-11-24 19:47:24

标签: java edge-detection sobel

再次遇到麻烦。我正在使用sobel edgedetector来获取图像的边缘。我使用布雷森汉姆算法来画另一端。但是要做到这一点,我需要坚实的坐标。

我的sobel为每个边返回2 0,而我只是想不出原因。如果图像的大小是原来的两倍,那是有道理的,但事实并非如此。

在PAINT中,我制作了一个单像素正方形的50x50像素图像,它返回了一个48x48 int数组,如下所示。我了解48x48,但我不了解2 0。

255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 255 255 255 0 0 0 0 0 0 0 0 255 255 255 0 0 255 255 255 255 255 255 255 255 255 0 0 255 255 255 255 255 255 255 255 255 0 0 255 255 255 255 255 255 255 255 255 0 0 255 255 255 255 255 255 255 255 255 0 0 255 255 255 255 255 255

我使用的sobel来自一种称为幅度数组的方法

public int[][] getMagnitudeArray() {

   int[][] filter1 = {
        {-1, 0, 1},
        {-2, 0, 2},
        {-1, 0, 1}
    };

    int[][] filter2 = {
        {1, 2, 1},
        {0, 0, 0},
        {-1, -2, -1}
    };

    Picture picture0 = new Picture(imagePath);
    int width = picture0.width()-2;
    int height = picture0.height()-2;
    int[][] arrayRepresentation = new int[width][height];

    for (int y = 0; y < height ; y++) {
        for (int x = 0; x < width ; x++) {

            // get 3-by-3 array of colors in neighborhood
            int[][] gray = new int[3][3];
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    gray[i][j] = (int) Luminance.intensity(picture0.get(x + i, y + j));
                }
            }

            // apply filter
            int gray1 = 0, gray2 = 0;
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    gray1 += gray[i][j] * filter1[i][j];
                    gray2 += gray[i][j] * filter2[i][j];
                }
            }
            // int magnitude = 255 - truncate(Math.abs(gray1) + Math.abs(gray2));
            int magnitude = 255 - binaryTruncate((int) Math.sqrt(gray1 * gray1 + gray2 * gray2));
            arrayRepresentation[x][y] = magnitude;
        }
    }

    return arrayRepresentation;
} `

有没有人可以告诉我此方法为何返回2 0,以及如何将其更改为仅返回1、0?

在它返回带有地狱和许多if语句的数组后,我试图对其进行“修复”,但是它看起来并不十分优雅。

谢谢。

0 个答案:

没有答案