diff options
-rw-r--r-- | src/main/java/Filter.java | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/src/main/java/Filter.java b/src/main/java/Filter.java index 1e50d7e..7e55f04 100644 --- a/src/main/java/Filter.java +++ b/src/main/java/Filter.java | |||
@@ -23,7 +23,7 @@ public final class Filter { | |||
23 | /** | 23 | /** |
24 | * Get a pixel without accessing out of bounds | 24 | * Get a pixel without accessing out of bounds |
25 | * | 25 | * |
26 | * @param gray a HxW float array | 26 | * @param gray a HxW float array, H > 0, W > 0 |
27 | * @param row Y coordinate | 27 | * @param row Y coordinate |
28 | * @param col X coordinate | 28 | * @param col X coordinate |
29 | * @return nearest valid pixel color | 29 | * @return nearest valid pixel color |
@@ -51,17 +51,36 @@ public final class Filter { | |||
51 | public static float[][] filter(float[][] gray, float[][] kernel) { | 51 | public static float[][] filter(float[][] gray, float[][] kernel) { |
52 | int width = gray[0].length; | 52 | int width = gray[0].length; |
53 | int height = gray.length; | 53 | int height = gray.length; |
54 | |||
55 | int kernelWidth = kernel[0].length; | ||
56 | int kernelHeight = kernel.length; | ||
57 | |||
58 | float pixelNeighbors[][] = new float[kernelHeight][kernelWidth]; | ||
54 | float[][] filteredImage = new float[height][width]; | 59 | float[][] filteredImage = new float[height][width]; |
60 | |||
55 | for (int row = 0; row < height; ++row) { | 61 | for (int row = 0; row < height; ++row) { |
56 | for (int col = 0; col < width; ++col) { | 62 | for (int col = 0; col < width; ++col) { |
57 | float pixelNeighbors[][] = { | 63 | |
58 | {Filter.at(gray, row - 1, col - 1), Filter.at(gray, row - 1, col), Filter.at(gray, row - 1, col + 1)}, | 64 | int pixelRow = -kernelHeight / 2; |
59 | {Filter.at(gray, row, col - 1), Filter.at(gray, row, col), Filter.at(gray, row, col + 1)}, | 65 | int pixelCol = -kernelWidth / 2; |
60 | {Filter.at(gray, row + 1, col - 1), Filter.at(gray, row + 1, col), Filter.at(gray, row + 1, col + 1)} | 66 | |
61 | }; | 67 | for (int kernelRow = 0; kernelRow < kernelWidth; kernelRow++) { |
62 | 68 | for (int kernelCol = 0; kernelCol < kernelHeight; kernelCol++) { | |
63 | for (int i = 0; i < kernel[0].length; i++) { | 69 | pixelNeighbors[kernelRow][kernelCol] = Filter.at(gray, row + pixelRow, col + pixelCol); |
64 | for (int j = 0; j < kernel.length; j++) { | 70 | |
71 | pixelCol += 1; | ||
72 | if (pixelCol > kernelWidth / 2) { | ||
73 | pixelCol = -kernelWidth / 2; | ||
74 | } | ||
75 | } | ||
76 | pixelRow += 1; | ||
77 | if (pixelRow > kernelHeight / 2) { | ||
78 | pixelRow = -kernelHeight / 2; | ||
79 | } | ||
80 | } | ||
81 | |||
82 | for (int i = 0; i < kernelWidth; i++) { | ||
83 | for (int j = 0; j < kernelHeight; j++) { | ||
65 | filteredImage[row][col] += kernel[i][j] * pixelNeighbors[i][j]; | 84 | filteredImage[row][col] += kernel[i][j] * pixelNeighbors[i][j]; |
66 | } | 85 | } |
67 | } | 86 | } |