From 358cceba114f6992884bde1ebd840317d9cd2b15 Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Thu, 15 Oct 2015 14:37:30 +0200 Subject: Implement filters --- src/main/java/Filter.java | 71 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 10 deletions(-) (limited to 'src/main/java') 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 { * @return a HxW float array */ public static float[][] filter(float[][] gray, float[][] kernel) { - // TODO filter - return null; + int width = gray[0].length; + int height = gray.length; + 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)} + }; + + 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]; + } + } + } + } + + return filteredImage; } /** @@ -44,8 +62,15 @@ public final class Filter { * @return a HxW float array */ public static float[][] smooth(float[][] gray) { - // TODO smooth - return null; + 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); + + return smoothtImage; } /** @@ -55,8 +80,14 @@ public final class Filter { * @return a HxW float array */ public static float[][] sobelX(float[][] gray) { - // TODO sobelX - return null; + float sobelXCore[][]= { + {-1,0,1}, + {-2,0,2}, + {-1,0,1} + }; + + float[][] sobelXImage = Filter.filter(gray, sobelXCore); + return sobelXImage; } /** @@ -66,8 +97,15 @@ public final class Filter { * @return a HxW float array */ public static float[][] sobelY(float[][] gray) { - // TODO sobelY - return null; + float sobelYCore[][]={ + {-1,-2,-1}, + {0,0,0}, + {1,2,1} + }; + + float[][] sobelYImage = Filter.filter(gray, sobelYCore); + System.out.println(sobelYImage[0][0]); + return sobelYImage; } /** @@ -77,8 +115,21 @@ public final class Filter { * @return a HxW float array */ public static float[][] sobel(float[][] gray) { - // TODO sobel - return null; + float[][] x = Filter.sobelX(gray); + float[][] y = Filter.sobelY(gray); + + int width = gray[0].length; + int height = gray.length; + + float[][] sobelImage = new float[height][width]; + + 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)); + } + } + + return sobelImage; } } -- cgit v1.2.3