diff options
-rw-r--r-- | src/main/java/Filter.java | 71 |
1 files changed, 61 insertions, 10 deletions
diff --git a/src/main/java/Filter.java b/src/main/java/Filter.java index 73b435b..2dd81ee 100644 --- a/src/main/java/Filter.java +++ b/src/main/java/Filter.java | |||
@@ -33,8 +33,26 @@ public final class Filter { | |||
33 | * @return a HxW float array | 33 | * @return a HxW float array |
34 | */ | 34 | */ |
35 | public static float[][] filter(float[][] gray, float[][] kernel) { | 35 | public static float[][] filter(float[][] gray, float[][] kernel) { |
36 | // TODO filter | 36 | int width = gray[0].length; |
37 | return null; | 37 | int height = gray.length; |
38 | float[][] filteredImage = new float[height][width]; | ||
39 | for (int row = 0; row < height; ++row) { | ||
40 | for (int col = 0; col < width; ++col) { | ||
41 | float pixelCore[][] = { | ||
42 | {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)}, | ||
44 | {Filter.at(gray, row-1, col-1),Filter.at(gray, row-1, col),Filter.at(gray, row-1, col+1)} | ||
45 | }; | ||
46 | |||
47 | for (int i = 0; i < kernel[0].length; i++) { | ||
48 | for (int j = 0; j < kernel.length; j++) { | ||
49 | filteredImage[row][col] += kernel[i][j] * pixelCore[i][j]; | ||
50 | } | ||
51 | } | ||
52 | } | ||
53 | } | ||
54 | |||
55 | return filteredImage; | ||
38 | } | 56 | } |
39 | 57 | ||
40 | /** | 58 | /** |
@@ -44,8 +62,15 @@ public final class Filter { | |||
44 | * @return a HxW float array | 62 | * @return a HxW float array |
45 | */ | 63 | */ |
46 | public static float[][] smooth(float[][] gray) { | 64 | public static float[][] smooth(float[][] gray) { |
47 | // TODO smooth | 65 | float smoothCore[][]={ |
48 | return null; | 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; | ||
49 | } | 74 | } |
50 | 75 | ||
51 | /** | 76 | /** |
@@ -55,8 +80,14 @@ public final class Filter { | |||
55 | * @return a HxW float array | 80 | * @return a HxW float array |
56 | */ | 81 | */ |
57 | public static float[][] sobelX(float[][] gray) { | 82 | public static float[][] sobelX(float[][] gray) { |
58 | // TODO sobelX | 83 | float sobelXCore[][]= { |
59 | return null; | 84 | {-1,0,1}, |
85 | {-2,0,2}, | ||
86 | {-1,0,1} | ||
87 | }; | ||
88 | |||
89 | float[][] sobelXImage = Filter.filter(gray, sobelXCore); | ||
90 | return sobelXImage; | ||
60 | } | 91 | } |
61 | 92 | ||
62 | /** | 93 | /** |
@@ -66,8 +97,15 @@ public final class Filter { | |||
66 | * @return a HxW float array | 97 | * @return a HxW float array |
67 | */ | 98 | */ |
68 | public static float[][] sobelY(float[][] gray) { | 99 | public static float[][] sobelY(float[][] gray) { |
69 | // TODO sobelY | 100 | float sobelYCore[][]={ |
70 | return null; | 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; | ||
71 | } | 109 | } |
72 | 110 | ||
73 | /** | 111 | /** |
@@ -77,8 +115,21 @@ public final class Filter { | |||
77 | * @return a HxW float array | 115 | * @return a HxW float array |
78 | */ | 116 | */ |
79 | public static float[][] sobel(float[][] gray) { | 117 | public static float[][] sobel(float[][] gray) { |
80 | // TODO sobel | 118 | float[][] x = Filter.sobelX(gray); |
81 | return null; | 119 | float[][] y = Filter.sobelY(gray); |
120 | |||
121 | int width = gray[0].length; | ||
122 | int height = gray.length; | ||
123 | |||
124 | float[][] sobelImage = new float[height][width]; | ||
125 | |||
126 | for (int row = 0; row < height; ++row) { | ||
127 | 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)); | ||
129 | } | ||
130 | } | ||
131 | |||
132 | return sobelImage; | ||
82 | } | 133 | } |
83 | 134 | ||
84 | } | 135 | } |