aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/Filter.java
blob: d88ea0735f72bf73ce226dc0238b9ca1d982f47e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
 * @author Pacien TRAN-GIRARD
 * @author Timothée FLOURE
 */
public final class Filter {

    public static final float[][] SMOOTH_CORE = new float[][]{
            {0.1f, 0.1f, 0.1f},
            {0.1f, 0.2f, 0.1f},
            {0.1f, 0.1f, 0.1f}
    };
    public static final float[][] SOBEL_X_CORE = new float[][]{
            {-1, 0, 1},
            {-2, 0, 2},
            {-1, 0, 1}
    };
    public static final float[][] SOBEL_Y_CORE = new float[][]{
            {-1, -2, -1},
            {0, 0, 0},
            {1, 2, 1}
    };

    /**
     * Get a pixel without accessing out of bounds
     *
     * @param gray a HxW float array, H > 0, W > 0
     * @param row  Y coordinate
     * @param col  X coordinate
     * @return nearest valid pixel color
     */
    public static float at(float[][] gray, int row, int col) {
        int maxRow = gray.length - 1;
        int maxCol = gray[0].length - 1;

        if (row < 0) row = 0;
        if (col < 0) col = 0;

        if (row > maxRow) row = maxRow;
        if (col > maxCol) col = maxCol;

        return gray[row][col];
    }

    /**
     * Combine neighbour pixels using the given kernel.
     *
     * @param gray   a HxW float array
     * @param kernel a MxN float array, with M and N odd
     * @param row    Y coordinate
     * @param col    X coordinate
     * @return the pixel value
     */
    private static float combineNeighbours(float[][] gray, float[][] kernel, int row, int col) {
        int kernelWidth = kernel[0].length;
        int kernelHeight = kernel.length;

        int rowOffset = row - kernelWidth / 2; // int division
        int colOffset = col - kernelHeight / 2; // int division

        float pixelValue = 0;

        for (int kernelRow = 0; kernelRow < kernelHeight; ++kernelRow)
            for (int kernelCol = 0; kernelCol < kernelWidth; ++kernelCol)
                pixelValue += kernel[kernelRow][kernelCol]
                        * Filter.at(gray, rowOffset + kernelRow, colOffset + kernelCol);

        return pixelValue;
    }

    /**
     * Convolve a single-channel image with specified kernel.
     *
     * @param gray   a HxW float array
     * @param kernel a MxN float array, with M and N odd
     * @return a HxW float array
     */
    public static float[][] filter(float[][] gray, float[][] kernel) {
        int width = gray[0].length;
        int height = gray.length;

        float[][] filteredImage = new float[height][width];

        for (int row = 0; row < height; ++row)
            for (int col = 0; col < width; ++col)
                filteredImage[row][col] = Filter.combineNeighbours(gray, kernel, row, col);

        return filteredImage;
    }

    /**
     * Smooth a single-channel image
     *
     * @param gray a HxW float array
     * @return a HxW float array
     */
    public static float[][] smooth(float[][] gray) {
        return Filter.filter(gray, SMOOTH_CORE);
    }

    /**
     * Compute horizontal Sobel filter
     *
     * @param gray a HxW float array
     * @return a HxW float array
     */
    public static float[][] sobelX(float[][] gray) {
        return Filter.filter(gray, SOBEL_X_CORE);
    }

    /**
     * Compute vertical Sobel filter
     *
     * @param gray a HxW float array
     * @return a HxW float array
     */
    public static float[][] sobelY(float[][] gray) {
        return Filter.filter(gray, SOBEL_Y_CORE);
    }

    /**
     * Compute the magnitude of combined Sobel filters
     *
     * @param gray a HxW float array
     * @return a HxW float array
     */
    public static float[][] sobel(float[][] gray) {
        float[][] x = Filter.sobelX(gray);
        float[][] y = Filter.sobelY(gray);

        int width = gray[0].length;
        int height = gray.length;

        float[][] sobelImage = new float[height][width];

        for (int row = 0; row < height; ++row) {
            for (int col = 0; col < width; ++col) {
                sobelImage[row][col] = (float) Math.sqrt(Math.pow(x[row][col], 2) + Math.pow(y[row][col], 2));
            }
        }

        return sobelImage;
    }

}