{"id":21,"date":"2013-01-28T15:54:25","date_gmt":"2013-01-28T15:54:25","guid":{"rendered":"http:\/\/drainthemainbrain.wordpress.com\/?p=21"},"modified":"2013-01-28T15:54:25","modified_gmt":"2013-01-28T15:54:25","slug":"21","status":"publish","type":"post","link":"http:\/\/blog.rajarshidas.com\/?p=21","title":{"rendered":"Mutating Random Polygons to Images"},"content":{"rendered":"<p>After being inspired by this <a href=\"http:\/\/rogeralsing.com\/2008\/12\/07\/genetic-programming-evolution-of-mona-lisa\/\" target=\"_blank\">link<\/a>, I planned to implement a similar Genetic Algorithm. The algorithm uses a sequence of mutating polygons to recreate an image. Since I was taking a Coursera refresher in R, I decided to give it a shot in that language.<!--more--><\/p>\n<p>Although, this has been described as a Genetic Algorithm or Genetic Programming, I thought the description was kind of stretched. A more accurate description is a Hill Climbing Algorithm:<\/p>\n<ol>\n<li>Initialize: Start with a random polygon<\/li>\n<li>Incremental change: Add, mutate, or remove polygons<\/li>\n<li>Comparison: Use a fitness function to compare the change with original &#8211; the &#8220;winner&#8221; goes through to the next round<\/li>\n<li>Repeat steps 2 &amp; 3<\/li>\n<\/ol>\n<p>The mutations were done using some fixed parameters I picked arbitrarily while coding, but I haven&#8217;t had a chance to fiddle with them yet:<\/p>\n<ol>\n<li>Mutation probability &#8211; 25% chance of each polygon to mutate slightly<\/li>\n<li>Color mutation &#8211; 15% range for each RGB color value to change in polygon<\/li>\n<li>Height\/Width mutation &#8211; 5% range for each point of the polygon to change<\/li>\n<li>Add polygon &#8211; 25% chance of a random new polygon to be added in a generation<\/li>\n<li>Remove polygon &#8211; 25% chance of a random polygon in current sequence to be removed in a generation<\/li>\n<li>Polygon vertices &#8211; 3, 4, or 5 points per polygon, chosen randomly<\/li>\n<li>Polygon transparency &#8211; 0.5 alpha in RGBA color space<\/li>\n<\/ol>\n<p>The fitness function is a mean square error in color space.<\/p>\n<p>There were a few issues I ran into using R for this, which I didn&#8217;t anticipate. R has a graphics device but I couldn&#8217;t figure out how to run my fitness function (a pixel by pixel comparison in color space) on it. Instead, I had to read &amp; write images to disk, which was a big hurdle in computation. The alternative would be to do the comparison while the images are stored as matrices. However, I couldn&#8217;t figure out the conversion from multiple stacked RGBA polygons (polygons with transparency) to an RGB color.<\/p>\n<p>I also coded the polygon vertices to be within the image dimensions, unless they mutate out. This boundary has a tendency to create a white space around the image. Maybe in the future I will fix that.<\/p>\n<p>Regardless, I let it run a few times with different images. During the inauguration, I ran it with the US flag. Earlier, I did a test run with the Mona Lisa. I was impatient and used small images to speed up the results. I also ended the trials after the incremental mutations became too infrequent. As you can see below, the general shape is achievable, but getting the details would take a lot more iterations.<\/p>\n<p><strong>Results<\/strong><\/p>\n<p>US flag video of evolution through all successful iterations:<\/p>\n<p>[youtube http:\/\/www.youtube.com\/watch?v=4Sfl7GN5NMU&amp;rel=0&amp;h=126&amp;w=224;]<\/p>\n<p>Mona Lisa select images of evolution:<br \/>\n<img decoding=\"async\" alt=\"image\" src=\"https:\/\/lh5.googleusercontent.com\/-t6Z3x6uIniA\/UQViK7FAJ2I\/AAAAAAAABA4\/1UIAW5D6NOA\/s800\/monalisa.jpg\" \/><br \/>\nIn 18 samples&#8230;<br \/>\n<img decoding=\"async\" alt=\"image\" src=\"https:\/\/lh3.googleusercontent.com\/-6yYvHt90tME\/UQViFrX38EI\/AAAAAAAAA-8\/8rpdEu2S3SE\/s800\/parent0001.jpg\" \/><img decoding=\"async\" alt=\"image\" src=\"https:\/\/lh4.googleusercontent.com\/-R9LeKYL6FjM\/UQViFla8nnI\/AAAAAAAAA-0\/HiwgbxKpWEQ\/s800\/parent0025.jpg\" \/><img decoding=\"async\" alt=\"image\" src=\"https:\/\/lh5.googleusercontent.com\/-y0Psueo0H38\/UQViFrLB2aI\/AAAAAAAAA_E\/t5FRjjPsC64\/s800\/parent0050.jpg\" \/><img decoding=\"async\" alt=\"image\" src=\"https:\/\/lh3.googleusercontent.com\/-EHAge2bULbg\/UQViGOs5CLI\/AAAAAAAAA_A\/A62XzR-d6wU\/s800\/parent0075.jpg\" \/><img decoding=\"async\" alt=\"image\" src=\"https:\/\/lh6.googleusercontent.com\/-eDAceyMAOvQ\/UQViGkO2BcI\/AAAAAAAAA_Y\/tksucI6-m0A\/s800\/parent0100.jpg\" \/><img decoding=\"async\" alt=\"image\" src=\"https:\/\/lh5.googleusercontent.com\/-ZdUN2TZaNK4\/UQViGuLRR0I\/AAAAAAAAA_I\/GSQzkdQyyvY\/s800\/parent0125.jpg\" \/><\/p>\n<p><img decoding=\"async\" alt=\"image\" src=\"https:\/\/lh3.googleusercontent.com\/-eInwPKNiRxQ\/UQViGuLfjeI\/AAAAAAAAA_M\/qii28VKG6fQ\/s800\/parent0150.jpg\" \/><img decoding=\"async\" alt=\"image\" src=\"https:\/\/lh6.googleusercontent.com\/-N1rrEYIIe30\/UQViHG-8CcI\/AAAAAAAAA_o\/tIQrZNFfYGI\/s800\/parent0175.jpg\" \/><img decoding=\"async\" alt=\"image\" src=\"https:\/\/lh5.googleusercontent.com\/-CfuDMFi3g54\/UQViHffq7jI\/AAAAAAAAA_s\/DZXRAroUpK4\/s800\/parent0200.jpg\" \/><img decoding=\"async\" alt=\"image\" src=\"https:\/\/lh6.googleusercontent.com\/-5Y2jthDnm2g\/UQViHmDyz2I\/AAAAAAAAA_4\/ibiOGV5NWko\/s800\/parent0225.jpg\" \/><img decoding=\"async\" alt=\"image\" src=\"https:\/\/lh6.googleusercontent.com\/-oeDlZrQRokg\/UQViIdpLWzI\/AAAAAAAABAA\/IrAYMcvDvAY\/s800\/parent0250.jpg\" \/><img decoding=\"async\" alt=\"image\" src=\"https:\/\/lh6.googleusercontent.com\/-mxnG-49FJUQ\/UQViIVXf5SI\/AAAAAAAABAE\/5WeymxX3Kzw\/s800\/parent0275.jpg\" \/><\/p>\n<p><img decoding=\"async\" alt=\"image\" src=\"https:\/\/lh4.googleusercontent.com\/-31QOta6QtfE\/UQViJFEaz6I\/AAAAAAAABAM\/71fAQYjGiQ8\/s800\/parent0300.jpg\" \/><img decoding=\"async\" alt=\"image\" src=\"https:\/\/lh6.googleusercontent.com\/-ID7XL_H4JsE\/UQViJRYDQPI\/AAAAAAAABAQ\/VtBT2AqobKs\/s800\/parent0325.jpg\" \/><img decoding=\"async\" alt=\"image\" src=\"https:\/\/lh5.googleusercontent.com\/-WkxL37yQ4Kc\/UQViJduSyKI\/AAAAAAAABAU\/dJaB4bYWz48\/s800\/parent0350.jpg\" \/><img decoding=\"async\" alt=\"image\" src=\"https:\/\/lh6.googleusercontent.com\/-ZJjiSBI9wH0\/UQViKOOIISI\/AAAAAAAABAo\/PjiV2eQROwI\/s800\/parent0400.jpg\" \/><img decoding=\"async\" alt=\"image\" src=\"https:\/\/lh4.googleusercontent.com\/-Ahh_oU1MTDI\/UQViKy8fiuI\/AAAAAAAABA0\/Db-1o4CooLM\/s800\/parent0450.jpg\" \/><img decoding=\"async\" alt=\"image\" src=\"https:\/\/lh6.googleusercontent.com\/-9QqRjPGHcis\/UQViLIGA9tI\/AAAAAAAABBA\/29-Fkwmu-s4\/s800\/parent0507.jpg\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>After being inspired by this link, I planned to implement a similar Genetic Algorithm. The algorithm uses a sequence of mutating polygons to recreate an image. Since I was taking a Coursera refresher in R, I decided to give it a shot in that language.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[16,17,26],"class_list":["post-21","post","type-post","status-publish","format-standard","hentry","category-programming","tag-genetic-algorithm","tag-image-generation","tag-r"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/blog.rajarshidas.com\/index.php?rest_route=\/wp\/v2\/posts\/21","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.rajarshidas.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.rajarshidas.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.rajarshidas.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.rajarshidas.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=21"}],"version-history":[{"count":0,"href":"http:\/\/blog.rajarshidas.com\/index.php?rest_route=\/wp\/v2\/posts\/21\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.rajarshidas.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.rajarshidas.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.rajarshidas.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}