diff options
-rw-r--r-- | src/main/java/Filter.java | 55 |
1 files changed, 26 insertions, 29 deletions
diff --git a/src/main/java/Filter.java b/src/main/java/Filter.java index bb87287..03c8b0b 100644 --- a/src/main/java/Filter.java +++ b/src/main/java/Filter.java | |||
@@ -4,6 +4,23 @@ | |||
4 | */ | 4 | */ |
5 | public final class Filter { | 5 | public final class Filter { |
6 | 6 | ||
7 | public static final float[][] SMOOTH_CORE = new float[][]{ | ||
8 | {0.1f, 0.1f, 0.1f}, | ||
9 | {0.1f, 0.2f, 0.1f}, | ||
10 | {0.1f, 0.1f, 0.1f} | ||
11 | }; | ||
12 | public static final float[][] SOBEL_X_CORE = new float[][]{ | ||
13 | {-1, 0, 1}, | ||
14 | {-2, 0, 2}, | ||
15 | {-1, 0, 1} | ||
16 | }; | ||
17 | public static final float[][] SOBEL_Y_CORE = new float[][]{ | ||
18 | {-1, -2, -1}, | ||
19 | {0, 0, 0}, | ||
20 | {1, 2, 1} | ||
21 | }; | ||
22 | |||
23 | |||
7 | /** | 24 | /** |
8 | * Get a pixel without accessing out of bounds | 25 | * Get a pixel without accessing out of bounds |
9 | * | 26 | * |
@@ -38,15 +55,15 @@ public final class Filter { | |||
38 | float[][] filteredImage = new float[height][width]; | 55 | float[][] filteredImage = new float[height][width]; |
39 | for (int row = 0; row < height; ++row) { | 56 | for (int row = 0; row < height; ++row) { |
40 | for (int col = 0; col < width; ++col) { | 57 | for (int col = 0; col < width; ++col) { |
41 | float pixelCore[][] = { | 58 | float pixelNeighbors[][] = { |
42 | {Filter.at(gray, row-1, col-1),Filter.at(gray, row-1, col),Filter.at(gray, row-1, col+1)}, | 59 | {Filter.at(gray, row - 1, col - 1), Filter.at(gray, row - 1, col), Filter.at(gray, row - 1, col + 1)}, |
43 | {Filter.at(gray, row, col-1),Filter.at(gray, row, col),Filter.at(gray, row, col+1)}, | 60 | {Filter.at(gray, row, col - 1), Filter.at(gray, row, col), Filter.at(gray, row, col + 1)}, |
44 | {Filter.at(gray, row+1, col-1),Filter.at(gray, row+1, col),Filter.at(gray, row+1, col+1)} | 61 | {Filter.at(gray, row + 1, col - 1), Filter.at(gray, row + 1, col), Filter.at(gray, row + 1, col + 1)} |
45 | }; | 62 | }; |
46 | 63 | ||
47 | for (int i = 0; i < kernel[0].length; i++) { | 64 | for (int i = 0; i < kernel[0].length; i++) { |
48 | for (int j = 0; j < kernel.length; j++) { | 65 | for (int j = 0; j < kernel.length; j++) { |
49 | filteredImage[row][col] += kernel[i][j] * pixelCore[i][j]; | 66 | filteredImage[row][col] += kernel[i][j] * pixelNeighbors[i][j]; |
50 | } | 67 | } |
51 | } | 68 | } |
52 | } | 69 | } |
@@ -62,14 +79,7 @@ public final class Filter { | |||
62 | * @return a HxW float array | 79 | * @return a HxW float array |
63 | */ | 80 | */ |
64 | public static float[][] smooth(float[][] gray) { | 81 | public static float[][] smooth(float[][] gray) { |
65 | float smoothCore[][]={ | 82 | float[][] smoothtImage = Filter.filter(gray, SMOOTH_CORE); |
66 | {0.1f,0.1f,0.1f}, | ||
67 | {0.1f,0.2f,0.1f}, | ||
68 | {0.1f,0.1f,0.1f} | ||
69 | }; | ||
70 | |||
71 | float[][] smoothtImage = Filter.filter(gray, smoothCore); | ||
72 | |||
73 | return smoothtImage; | 83 | return smoothtImage; |
74 | } | 84 | } |
75 | 85 | ||
@@ -80,13 +90,7 @@ public final class Filter { | |||
80 | * @return a HxW float array | 90 | * @return a HxW float array |
81 | */ | 91 | */ |
82 | public static float[][] sobelX(float[][] gray) { | 92 | public static float[][] sobelX(float[][] gray) { |
83 | float sobelXCore[][]= { | 93 | float[][] sobelXImage = Filter.filter(gray, SOBEL_X_CORE); |
84 | {-1,0,1}, | ||
85 | {-2,0,2}, | ||
86 | {-1,0,1} | ||
87 | }; | ||
88 | |||
89 | float[][] sobelXImage = Filter.filter(gray, sobelXCore); | ||
90 | return sobelXImage; | 94 | return sobelXImage; |
91 | } | 95 | } |
92 | 96 | ||
@@ -97,14 +101,7 @@ public final class Filter { | |||
97 | * @return a HxW float array | 101 | * @return a HxW float array |
98 | */ | 102 | */ |
99 | public static float[][] sobelY(float[][] gray) { | 103 | public static float[][] sobelY(float[][] gray) { |
100 | float sobelYCore[][]={ | 104 | float[][] sobelYImage = Filter.filter(gray, SOBEL_Y_CORE); |
101 | {-1,-2,-1}, | ||
102 | {0,0,0}, | ||
103 | {1,2,1} | ||
104 | }; | ||
105 | |||
106 | float[][] sobelYImage = Filter.filter(gray, sobelYCore); | ||
107 | System.out.println(sobelYImage[0][0]); | ||
108 | return sobelYImage; | 105 | return sobelYImage; |
109 | } | 106 | } |
110 | 107 | ||
@@ -125,7 +122,7 @@ public final class Filter { | |||
125 | 122 | ||
126 | for (int row = 0; row < height; ++row) { | 123 | for (int row = 0; row < height; ++row) { |
127 | for (int col = 0; col < width; ++col) { | 124 | for (int col = 0; col < width; ++col) { |
128 | sobelImage[row][col] = (float) Math.sqrt(Math.pow(x[row][col],2)+Math.pow(y[row][col],2)); | 125 | sobelImage[row][col] = (float) Math.sqrt(Math.pow(x[row][col], 2) + Math.pow(y[row][col], 2)); |
129 | } | 126 | } |
130 | } | 127 | } |
131 | 128 | ||