From 30988e0419fdb84164523375d093709dd7d18796 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Thu, 15 Oct 2015 16:46:20 +0200 Subject: Automate image processing --- src/main/java/Main.java | 50 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 12 deletions(-) (limited to 'src/main/java') 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 @@ +import java.io.File; + /** * @author Pacien TRAN-GIRARD * @author Timothée FLOURE */ public final class Main { - public static void main(String[] args) { + public static final String INPUT_DIR = "resources/"; + public static final String OUTPUT_DIR = "output/"; + public static final String EXT_SEP = "."; + public static final String NAME_SEP = "_"; + + public static void writeImage(int[][] image, String fileNameWithoutExt, String ext, int step, String transform) { + Helper.write(OUTPUT_DIR + fileNameWithoutExt + NAME_SEP + step + NAME_SEP + transform + EXT_SEP + ext, image); + } + + public static void writeImage(float[][] image, String fileNameWithoutExt, String ext, int step, String transform) { + Main.writeImage(Color.toRGB(image), fileNameWithoutExt, ext, step, transform); + } + + public static void processImage(File inputFile) { + String fileName = inputFile.getName(); + int extSepPosition = fileName.lastIndexOf(EXT_SEP); + String fileNameWithoutExt = fileName.substring(0, extSepPosition); + String fileExt = fileName.substring(extSepPosition + 1, fileName.length()); + + System.out.println("Processing file " + fileName); // Load image System.out.println("Load image..."); - int[][] image = Helper.read("doves.jpg"); - Helper.show(image, "Original"); + int[][] image = Helper.read(inputFile.getPath()); + Main.writeImage(image, fileNameWithoutExt, fileExt, 1, "original"); // Convert to grayscale System.out.println("Convert to grayscale..."); float[][] gray = Color.toGray(image); - Helper.show(Color.toRGB(gray), "Grayscale"); + Main.writeImage(gray, fileNameWithoutExt, fileExt, 2, "grayscale"); // Smooth it System.out.println("Smooth image..."); float[][] smooth = Filter.smooth(gray); - Helper.show(Color.toRGB(smooth), "Smooth"); + Main.writeImage(smooth, fileNameWithoutExt, fileExt, 3, "smooth"); + // Apply Sobel System.out.println("Compute Sobel filter..."); float[][] sobel = Filter.sobel(smooth); - Helper.show(Color.toRGB(sobel), "Sobel"); + Main.writeImage(sobel, fileNameWithoutExt, fileExt, 4, "sobel"); // Find best seam System.out.println("Find best seam..."); int[] seam = Seam.find(sobel); - Helper.show(Seam.merge(image, seam), "Best seam"); + Main.writeImage(Seam.merge(image, seam), fileNameWithoutExt, fileExt, 5, "best_seam"); // Shrink until it is a square int count = image[0].length - image.length; @@ -38,14 +60,18 @@ public final class Main { sobel = Filter.sobel(Filter.smooth(Color.toGray(image))); seam = Seam.find(sobel); image = Seam.shrink(image, seam); - System.out.println("Seam " + (i + 1) + "/" + count); + System.out.print("Seam " + (i + 1) + "/" + count + "\r"); } - System.out.println("Done"); - Helper.show(image, "Shrink"); + Main.writeImage(image, fileNameWithoutExt, fileExt, 6, "shrink"); + System.out.println("\nDone\n"); + } - // Save result - Helper.write("doves.shrink.jpg", image); + public static void main(String[] args) { + File inputDir = new File(INPUT_DIR); + File[] inputFiles = inputDir.listFiles(); + for (File file : inputFiles) + Main.processImage(file); } } -- cgit v1.2.3