From f34f95c31268608b08792df15d1fdd87fccc74eb Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Thu, 15 Oct 2015 15:14:54 +0200 Subject: Clean up --- src/main/java/Filter.java | 55 ++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) (limited to 'src') 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 @@ */ 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 * @@ -38,15 +55,15 @@ public final class Filter { float[][] filteredImage = new float[height][width]; for (int row = 0; row < height; ++row) { for (int col = 0; col < width; ++col) { - float pixelCore[][] = { - {Filter.at(gray, row-1, col-1),Filter.at(gray, row-1, col),Filter.at(gray, row-1, col+1)}, - {Filter.at(gray, row, col-1),Filter.at(gray, row, col),Filter.at(gray, row, col+1)}, - {Filter.at(gray, row+1, col-1),Filter.at(gray, row+1, col),Filter.at(gray, row+1, col+1)} + float pixelNeighbors[][] = { + {Filter.at(gray, row - 1, col - 1), Filter.at(gray, row - 1, col), Filter.at(gray, row - 1, col + 1)}, + {Filter.at(gray, row, col - 1), Filter.at(gray, row, col), Filter.at(gray, row, col + 1)}, + {Filter.at(gray, row + 1, col - 1), Filter.at(gray, row + 1, col), Filter.at(gray, row + 1, col + 1)} }; for (int i = 0; i < kernel[0].length; i++) { for (int j = 0; j < kernel.length; j++) { - filteredImage[row][col] += kernel[i][j] * pixelCore[i][j]; + filteredImage[row][col] += kernel[i][j] * pixelNeighbors[i][j]; } } } @@ -62,14 +79,7 @@ public final class Filter { * @return a HxW float array */ public static float[][] smooth(float[][] gray) { - float smoothCore[][]={ - {0.1f,0.1f,0.1f}, - {0.1f,0.2f,0.1f}, - {0.1f,0.1f,0.1f} - }; - - float[][] smoothtImage = Filter.filter(gray, smoothCore); - + float[][] smoothtImage = Filter.filter(gray, SMOOTH_CORE); return smoothtImage; } @@ -80,13 +90,7 @@ public final class Filter { * @return a HxW float array */ public static float[][] sobelX(float[][] gray) { - float sobelXCore[][]= { - {-1,0,1}, - {-2,0,2}, - {-1,0,1} - }; - - float[][] sobelXImage = Filter.filter(gray, sobelXCore); + float[][] sobelXImage = Filter.filter(gray, SOBEL_X_CORE); return sobelXImage; } @@ -97,14 +101,7 @@ public final class Filter { * @return a HxW float array */ public static float[][] sobelY(float[][] gray) { - float sobelYCore[][]={ - {-1,-2,-1}, - {0,0,0}, - {1,2,1} - }; - - float[][] sobelYImage = Filter.filter(gray, sobelYCore); - System.out.println(sobelYImage[0][0]); + float[][] sobelYImage = Filter.filter(gray, SOBEL_Y_CORE); return sobelYImage; } @@ -125,7 +122,7 @@ public final class Filter { 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)); + sobelImage[row][col] = (float) Math.sqrt(Math.pow(x[row][col], 2) + Math.pow(y[row][col], 2)); } } -- cgit v1.2.3