-
Notifications
You must be signed in to change notification settings - Fork 0
/
mona_lisa_GA.pde
55 lines (41 loc) · 1.19 KB
/
mona_lisa_GA.pde
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
import processing.pdf.*;
import java.lang.reflect.Method;
int popmax;
float mutationRate;
Population population;
int newgendraw;
int IMAGE_WIDTH = 100; // must be set to image width
int IMAGE_HEIGHT = 149; // must be set to image height
PImage img;
int SCALE_FACTOR = 1; //not yet working
float[] targetBrightness = new float[IMAGE_WIDTH*IMAGE_HEIGHT];
void settings() {
size(IMAGE_WIDTH*SCALE_FACTOR, IMAGE_HEIGHT*SCALE_FACTOR);
}
void setup() {
img = loadImage("small_lisa.jpg"); // Load the image into the program
popmax = 100;
mutationRate = 0.004;
// set up an array with brightness level for each pixel of target image already calculated
for(int i = 0; i < this.img.pixels.length; i++) {
targetBrightness[i] = brightness(this.img.pixels[i]);
}
population = new Population(targetBrightness, mutationRate, popmax);
}
void draw() {
background(0);
// Generate mating pool
population.naturalSelection();
// Create next generation
population.generate();
// Calculate fitness
population.calcFitness();
// Draw the fittest image
population.drawBest();
}
void keyPressed() {
if (key == 'r') {
saveFrame("v3/gen_######.png");
println(frameCount);
}
}