diff options
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/Filter.java | 61 |
1 files changed, 29 insertions, 32 deletions
diff --git a/src/main/java/Filter.java b/src/main/java/Filter.java index 7e55f04..d88ea07 100644 --- a/src/main/java/Filter.java +++ b/src/main/java/Filter.java | |||
@@ -42,6 +42,32 @@ public final class Filter { | |||
42 | } | 42 | } |
43 | 43 | ||
44 | /** | 44 | /** |
45 | * Combine neighbour pixels using the given kernel. | ||
46 | * | ||
47 | * @param gray a HxW float array | ||
48 | * @param kernel a MxN float array, with M and N odd | ||
49 | * @param row Y coordinate | ||
50 | * @param col X coordinate | ||
51 | * @return the pixel value | ||
52 | */ | ||
53 | private static float combineNeighbours(float[][] gray, float[][] kernel, int row, int col) { | ||
54 | int kernelWidth = kernel[0].length; | ||
55 | int kernelHeight = kernel.length; | ||
56 | |||
57 | int rowOffset = row - kernelWidth / 2; // int division | ||
58 | int colOffset = col - kernelHeight / 2; // int division | ||
59 | |||
60 | float pixelValue = 0; | ||
61 | |||
62 | for (int kernelRow = 0; kernelRow < kernelHeight; ++kernelRow) | ||
63 | for (int kernelCol = 0; kernelCol < kernelWidth; ++kernelCol) | ||
64 | pixelValue += kernel[kernelRow][kernelCol] | ||
65 | * Filter.at(gray, rowOffset + kernelRow, colOffset + kernelCol); | ||
66 | |||
67 | return pixelValue; | ||
68 | } | ||
69 | |||
70 | /** | ||
45 | * Convolve a single-channel image with specified kernel. | 71 | * Convolve a single-channel image with specified kernel. |
46 | * | 72 | * |
47 | * @param gray a HxW float array | 73 | * @param gray a HxW float array |
@@ -52,40 +78,11 @@ public final class Filter { | |||
52 | int width = gray[0].length; | 78 | int width = gray[0].length; |
53 | int height = gray.length; | 79 | int height = gray.length; |
54 | 80 | ||
55 | int kernelWidth = kernel[0].length; | ||
56 | int kernelHeight = kernel.length; | ||
57 | |||
58 | float pixelNeighbors[][] = new float[kernelHeight][kernelWidth]; | ||
59 | float[][] filteredImage = new float[height][width]; | 81 | float[][] filteredImage = new float[height][width]; |
60 | 82 | ||
61 | for (int row = 0; row < height; ++row) { | 83 | for (int row = 0; row < height; ++row) |
62 | for (int col = 0; col < width; ++col) { | 84 | for (int col = 0; col < width; ++col) |
63 | 85 | filteredImage[row][col] = Filter.combineNeighbours(gray, kernel, row, col); | |
64 | int pixelRow = -kernelHeight / 2; | ||
65 | int pixelCol = -kernelWidth / 2; | ||
66 | |||
67 | for (int kernelRow = 0; kernelRow < kernelWidth; kernelRow++) { | ||
68 | for (int kernelCol = 0; kernelCol < kernelHeight; kernelCol++) { | ||
69 | pixelNeighbors[kernelRow][kernelCol] = Filter.at(gray, row + pixelRow, col + pixelCol); | ||
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++) { | ||
84 | filteredImage[row][col] += kernel[i][j] * pixelNeighbors[i][j]; | ||
85 | } | ||
86 | } | ||
87 | } | ||
88 | } | ||
89 | 86 | ||
90 | return filteredImage; | 87 | return filteredImage; |
91 | } | 88 | } |