aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/Main.java
blob: 3314e6062d9b9bda34e3d752461275bee78c9302 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.io.File;

/**
 * @author Pacien TRAN-GIRARD
 * @author Timothée FLOURE
 */
public final class Main {

    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(inputFile.getPath());
        Main.writeImage(image, fileNameWithoutExt, fileExt, 1, "original");

        // Convert to grayscale
        System.out.println("Convert to grayscale...");
        float[][] gray = Color.toGray(image);
        Main.writeImage(gray, fileNameWithoutExt, fileExt, 2, "grayscale");

        // Smooth it
        System.out.println("Smooth image...");
        float[][] smooth = Filter.smooth(gray);
        Main.writeImage(smooth, fileNameWithoutExt, fileExt, 3, "smooth");


        // Apply Sobel
        System.out.println("Compute Sobel filter...");
        float[][] sobel = Filter.sobel(smooth);
        Main.writeImage(sobel, fileNameWithoutExt, fileExt, 4, "sobel");

        // Find best seam
        System.out.println("Find best seam...");
        int[] seam = Seam.find(sobel);
        Main.writeImage(Seam.merge(image, seam), fileNameWithoutExt, fileExt, 5, "best_seam");

        // Shrink until it is a square
        int count = image[0].length - image.length;
        System.out.println("Shrink by removing " + count + " seams...");
        for (int i = 0; i < count; ++i) {
            sobel = Filter.sobel(Filter.smooth(Color.toGray(image)));
            seam = Seam.find(sobel);
            image = Seam.shrink(image, seam);
            System.out.print("Seam " + (i + 1) + "/" + count + "\r");
        }
        Main.writeImage(image, fileNameWithoutExt, fileExt, 6, "shrink");
        System.out.println("\nDone\n");
    }

    public static void main(String[] args) {
        File inputDir = new File(INPUT_DIR);
        File[] inputFiles = inputDir.listFiles();

        for (File file : inputFiles)
            Main.processImage(file);
    }

}