Monday, December 15, 2008

Genetic Programming: Java

Edit: To add example image


The credit for the idea goes to Roger Alsin, you can take a look at his version over at http://rogeralsing.com/2008/12/07/genetic-programming-evolution-of-mona-lisa/

Anyway, after seeing that I thought I'd give it a try, so I threw together a version in Java that seems to do the trick quite well (albeit a little slowly). I'll throw in some images at some point, but in the meantime you can take a look at the code, play with it, modify it, and suggest any improvements (God knows it needs them)

As promised, here is the algorithm in action, I've tried it on a few images, but here is my version of the mona-lisa. I'll post a few of the other ones at a later stage.

I see that Roger Alsin has subsequently released the code for his, I haven't taken a look yet so not sure how it compares.

You'll need the Java Advanced Image API to use this.

imageCompare.java
imageGenerate.java

My apologies for the sparse comments and inconsistency you might encounter. Simply compile the files and run imageGenerate with a command line of argument of the image you wish to generate.

As a side note: I put this blog together very quickly a few minutes ago so forgive it's bareness and default template.

7 comments:

  1. Klaus MeffertDec 15, 2008 12:31 PM
    I didn't have time to look at your code, just one hint ahead: The two links point to the same java file.

    Great that you put Java code for this online! Thanx in advance.
    ReplyDelete
  2. PuffyDec 15, 2008 10:58 PM
    Woops, that's what I get for copy/pasting :) Thanks for pointing that out Klaus
    ReplyDelete
  3. ChangomanDec 27, 2008 12:49 PM
    Hi, just a bug I found in your code. In the "removePoint" method in "imageGenerate.java" you didn't copy tempx and tempy to x and y.

    Thanks for the code, very intresting :)
    ReplyDelete
  4. PuffyDec 27, 2008 11:26 PM
    Thanks Changoman, sorted that out quick. The machine that hosted the java files went down yesterday, only managed to get someone to reboot the machine now. Should all be back up again.
    ReplyDelete
  5. AnonymousJun 2, 2009 04:49 PM
    Why do you have a javascript running that forces my browser to refresh the page every 4-5 seconds?
    Not only it is annoying, but also very rude and intrusive...
    Your post might be interesting (I didn't have the patience to read it), but other than the time it takes to write this comment, I refuse to spend another minute on this page.

    My appologies if the error was on my side (which seems unlikely, since the annoying refreshing stoped when I turned JavaScript off..),

    Take care,

    Stefan.
    ReplyDelete
  6. Dave :BrosiusJun 5, 2009 10:10 PM
    Your pixel difference is just the square of the difference of the colors. But perhaps you would get better convergence if you weighted red much higher than blue as the human eye sees differences in red much more than blue.

    maybe something like
    red * 6
    green * 5
    blue * 4
    ReplyDelete
  7. PuffyJun 14, 2009 08:49 AM
    Stefan: yeah that would annoy the hell outta me aswell. Definitely not my side though, perhaps blogger was having issues?
    ReplyDelete