Datasets:

ArXiv:
License:
File size: 5,298 Bytes
c574d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBufferInt;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {

    public static void main(String[] args) {

        /*
        Change these
         */
        String path = "2";
        int numSegments = 50;
        int numPopulations = 20;
        int numIndividuals = 2;


        BufferedImage img = readImage(path);

        //img = scale(img, img.getType(), (int)(img.getWidth()*0.1), (int)(img.getHeight()*0.1), 0.1, 0.1);
        int[][] imgArray = Helpers.convertTo2DWithoutUsingGetRGB(img);

        ArrayList<ArrayList<Node>> nodes = Helpers.initNodes(imgArray);

        ArrayList<ArrayList<ArrayList<Edge>>> edges = new ArrayList<>();


        for (int i = 0; i < nodes.size(); i++) {
            edges.add(new ArrayList<>());
            for (int j = 0; j < nodes.get(i).size(); j++) {
                edges.get(i).add(new ArrayList<>());
                edges.get(i).get(j).addAll(Helpers.setNodeEdges(nodes.get(i).get(j),nodes));
            }
        }


        Population p = new Population(imgArray, edges, numSegments, numIndividuals);

        for (int i = 0; i < numPopulations; i++) {

            System.out.println("Population: " + i);
            System.out.println("Overall Deviation: " + p.getIndividuals().get(0).getOverallDeviation());
            System.out.println("Edge Value: " + p.getIndividuals().get(0).getEdgeValue());
            System.out.println();

            ArrayList<ArrayList<ArrayList<Integer>>> newRoots = GA.doGA(imgArray, p, numIndividuals);

            p = new Population(imgArray, edges, newRoots, numIndividuals);

            BufferedImage currImg = deepCopy(img);

            Individual outIndividual = p.getIndividuals().get(0);
            for(Segment segment : outIndividual.getSegments()){
                for(Node n : segment.getNodes()){
                    currImg = changeImageGroundTruth(currImg, n);
                }
            }

            writeImage(path, currImg, i);

        }


        BufferedImage img2 = deepCopy(img);

        Individual i = p.getIndividuals().get(0);
            for(Segment segment : i.getSegments()){
                for(Node n : segment.getNodes()){
                    img2 = changeImageWithColors(img2, n);
                }
            }

        writeImage(path+"COLORS", img2, 0);


    }

    static BufferedImage deepCopy(BufferedImage bi) {
        ColorModel cm = bi.getColorModel();
        boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
        WritableRaster raster = bi.copyData(null);
        return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
    }


    static BufferedImage readImage(String path) {
        BufferedImage img = null;

        try {
            img = ImageIO.read(new File("TestImages/" + path + "/Test image.jpg"));

        } catch (IOException e) {
            e.printStackTrace();
        }
        return img;
    }

    static void writeImage(String path, BufferedImage img, int imgCount) {
        File dir = new File("OutFiles/" + path);
        dir.mkdir();

        File outputfile = new File("OutFiles/" + path +"/" + path + imgCount + ".jpg");
        try {
            ImageIO.write(img, "jpg", outputfile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    static BufferedImage changeImageGroundTruth(BufferedImage img, Node node) {

        Color c;
        if(node.isEdge()){
            c = Color.BLACK;
        }
        else{
            c = Color.WHITE;
        }

        img.setRGB(node.getY(), node.getX(), c.getRGB());

        try{
            img.setRGB(node.getY()+1, node.getX(), c.getRGB());
        }
        catch (Exception e){

        }

        try{
            img.setRGB(node.getY()-1, node.getX(), c.getRGB());
        }
        catch (Exception e){

        }

        try{
            img.setRGB(node.getY(), node.getX()+1, c.getRGB());
        }
        catch (Exception e){

        }

        try{
            img.setRGB(node.getY(), node.getX()-1, c.getRGB());
        }
        catch (Exception e){

        }

        return img;
    }

    static BufferedImage changeImageWithColors(BufferedImage img, Node node) {

        Color c;
        if(node.isEdge()){
            c = Color.GREEN;
        }
        else{
            c = new Color(img.getRGB(node.getY(), node.getX()));
        }

        img.setRGB(node.getY(), node.getX(), c.getRGB());



        return img;
    }



    static BufferedImage scale(BufferedImage sbi, int imageType, int dWidth, int dHeight, double fWidth, double fHeight) {
        BufferedImage dbi = null;
        if (sbi != null) {
            dbi = new BufferedImage(dWidth, dHeight, imageType);
            Graphics2D g = dbi.createGraphics();
            AffineTransform at = AffineTransform.getScaleInstance(fWidth, fHeight);
            g.drawRenderedImage(sbi, at);
        }
        return dbi;
    }


}