Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples/image_matcher - pull elite consumer into another file
- Loading branch information
Showing
2 changed files
with
45 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
ga "github.com/tomcraven/goga" | ||
"os" | ||
_ "image/jpeg" | ||
"image/png" | ||
"image/draw" | ||
"image" | ||
"image/color" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type myEliteConsumer struct { | ||
currentIter int | ||
previousFitness int | ||
} | ||
|
||
func (ec *myEliteConsumer) OnElite(g *ga.IGenome) { | ||
bits := (*g).GetBits() | ||
newImage := createImageFromBitset(bits) | ||
|
||
// Output elite | ||
outputImageFile, _ := os.Create("elite.png") | ||
png.Encode(outputImageFile, newImage) | ||
outputImageFile.Close() | ||
|
||
// Output elite with input image blended over the top | ||
outputImageFileAlphaBlended, _ := os.Create("elite_with_original.png") | ||
draw.DrawMask(newImage, newImage.Bounds(), | ||
inputImage, image.ZP, | ||
&image.Uniform{color.RGBA{0, 0, 0, 255 / 4}}, image.ZP, | ||
draw.Over) | ||
png.Encode(outputImageFileAlphaBlended, newImage) | ||
outputImageFileAlphaBlended.Close() | ||
|
||
ec.currentIter++ | ||
fitness := (*g).GetFitness() | ||
fmt.Println(ec.currentIter, "\t", fitness, "\t", fitness-ec.previousFitness) | ||
|
||
ec.previousFitness = fitness | ||
|
||
time.Sleep(10 * time.Millisecond) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters