diff options
author | Pacien | 2015-10-28 11:19:23 +0100 |
---|---|---|
committer | Pacien | 2015-10-28 11:19:23 +0100 |
commit | a98c2a7fe70640f3c855e6e13f664165b0a4e466 (patch) | |
tree | 04eb443a8f9382fc3ff92be5a46ea8aaa3d160f0 /src/main/java | |
parent | 2db512ac95a782f6bb35b7c45475a33f73b6f252 (diff) | |
parent | 30988e0419fdb84164523375d093709dd7d18796 (diff) | |
download | seam-stitcher-a98c2a7fe70640f3c855e6e13f664165b0a4e466.tar.gz |
Merge pull request #3 from Pacien/beyond
Automate image processing
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/Main.java | 50 |
1 files changed, 38 insertions, 12 deletions
diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 43571bb..3314e60 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java | |||
@@ -1,35 +1,57 @@ | |||
1 | import java.io.File; | ||
2 | |||
1 | /** | 3 | /** |
2 | * @author Pacien TRAN-GIRARD | 4 | * @author Pacien TRAN-GIRARD |
3 | * @author Timothée FLOURE | 5 | * @author Timothée FLOURE |
4 | */ | 6 | */ |
5 | public final class Main { | 7 | public final class Main { |
6 | 8 | ||
7 | public static void main(String[] args) { | 9 | public static final String INPUT_DIR = "resources/"; |
10 | public static final String OUTPUT_DIR = "output/"; | ||
11 | public static final String EXT_SEP = "."; | ||
12 | public static final String NAME_SEP = "_"; | ||
13 | |||
14 | public static void writeImage(int[][] image, String fileNameWithoutExt, String ext, int step, String transform) { | ||
15 | Helper.write(OUTPUT_DIR + fileNameWithoutExt + NAME_SEP + step + NAME_SEP + transform + EXT_SEP + ext, image); | ||
16 | } | ||
17 | |||
18 | public static void writeImage(float[][] image, String fileNameWithoutExt, String ext, int step, String transform) { | ||
19 | Main.writeImage(Color.toRGB(image), fileNameWithoutExt, ext, step, transform); | ||
20 | } | ||
21 | |||
22 | public static void processImage(File inputFile) { | ||
23 | String fileName = inputFile.getName(); | ||
24 | int extSepPosition = fileName.lastIndexOf(EXT_SEP); | ||
25 | String fileNameWithoutExt = fileName.substring(0, extSepPosition); | ||
26 | String fileExt = fileName.substring(extSepPosition + 1, fileName.length()); | ||
27 | |||
28 | System.out.println("Processing file " + fileName); | ||
8 | 29 | ||
9 | // Load image | 30 | // Load image |
10 | System.out.println("Load image..."); | 31 | System.out.println("Load image..."); |
11 | int[][] image = Helper.read("doves.jpg"); | 32 | int[][] image = Helper.read(inputFile.getPath()); |
12 | Helper.show(image, "Original"); | 33 | Main.writeImage(image, fileNameWithoutExt, fileExt, 1, "original"); |
13 | 34 | ||
14 | // Convert to grayscale | 35 | // Convert to grayscale |
15 | System.out.println("Convert to grayscale..."); | 36 | System.out.println("Convert to grayscale..."); |
16 | float[][] gray = Color.toGray(image); | 37 | float[][] gray = Color.toGray(image); |
17 | Helper.show(Color.toRGB(gray), "Grayscale"); | 38 | Main.writeImage(gray, fileNameWithoutExt, fileExt, 2, "grayscale"); |
18 | 39 | ||
19 | // Smooth it | 40 | // Smooth it |
20 | System.out.println("Smooth image..."); | 41 | System.out.println("Smooth image..."); |
21 | float[][] smooth = Filter.smooth(gray); | 42 | float[][] smooth = Filter.smooth(gray); |
22 | Helper.show(Color.toRGB(smooth), "Smooth"); | 43 | Main.writeImage(smooth, fileNameWithoutExt, fileExt, 3, "smooth"); |
44 | |||
23 | 45 | ||
24 | // Apply Sobel | 46 | // Apply Sobel |
25 | System.out.println("Compute Sobel filter..."); | 47 | System.out.println("Compute Sobel filter..."); |
26 | float[][] sobel = Filter.sobel(smooth); | 48 | float[][] sobel = Filter.sobel(smooth); |
27 | Helper.show(Color.toRGB(sobel), "Sobel"); | 49 | Main.writeImage(sobel, fileNameWithoutExt, fileExt, 4, "sobel"); |
28 | 50 | ||
29 | // Find best seam | 51 | // Find best seam |
30 | System.out.println("Find best seam..."); | 52 | System.out.println("Find best seam..."); |
31 | int[] seam = Seam.find(sobel); | 53 | int[] seam = Seam.find(sobel); |
32 | Helper.show(Seam.merge(image, seam), "Best seam"); | 54 | Main.writeImage(Seam.merge(image, seam), fileNameWithoutExt, fileExt, 5, "best_seam"); |
33 | 55 | ||
34 | // Shrink until it is a square | 56 | // Shrink until it is a square |
35 | int count = image[0].length - image.length; | 57 | int count = image[0].length - image.length; |
@@ -38,14 +60,18 @@ public final class Main { | |||
38 | sobel = Filter.sobel(Filter.smooth(Color.toGray(image))); | 60 | sobel = Filter.sobel(Filter.smooth(Color.toGray(image))); |
39 | seam = Seam.find(sobel); | 61 | seam = Seam.find(sobel); |
40 | image = Seam.shrink(image, seam); | 62 | image = Seam.shrink(image, seam); |
41 | System.out.println("Seam " + (i + 1) + "/" + count); | 63 | System.out.print("Seam " + (i + 1) + "/" + count + "\r"); |
42 | } | 64 | } |
43 | System.out.println("Done"); | 65 | Main.writeImage(image, fileNameWithoutExt, fileExt, 6, "shrink"); |
44 | Helper.show(image, "Shrink"); | 66 | System.out.println("\nDone\n"); |
67 | } | ||
45 | 68 | ||
46 | // Save result | 69 | public static void main(String[] args) { |
47 | Helper.write("doves.shrink.jpg", image); | 70 | File inputDir = new File(INPUT_DIR); |
71 | File[] inputFiles = inputDir.listFiles(); | ||
48 | 72 | ||
73 | for (File file : inputFiles) | ||
74 | Main.processImage(file); | ||
49 | } | 75 | } |
50 | 76 | ||
51 | } | 77 | } |