Preface:

Fit the image using genetic algorithms, so come and share a wave.

Let’s begin happily

The development tools

Python version: 3.6.4

Related modules:

PIL module;

And some of the modules that come with Python.

Environment set up

Install Python and add it to the environment variable, and the PIP will install the appropriate module.

The resources

50 translucent polygons recreate Mona Lisa:

http://alteredqualia.com/visualization/evolve/

The Science Squirrel will “Genetic Algorithm” :

http://songshuhui.net/archives/10462

Introduction of the principle

Adapt stories from references:

Once upon a time, there lived a group of scallops near the seashore. They had food and clothing, houses and cars. But the good times did not last long, the arrival of human beings broke their original carefree life.

Every once in a while, humans poach some of them. However, these people don’t like scallops with shell patterns that look like huskies, so they always choose scallops that look less like huskies.

This went on for tens of thousands of years. That’s when something magical happened: all the scallop shells had huskies on them!

Genetic algorithm — one of the meta-heuristic algorithms:

First of all, a wave of Darwin?

In short, the genetic algorithm simulates the evolution process of the population under selection pressure in nature, and thus obtains the approximate solution of the corresponding problem.

Specifically, what an organism looks like is largely determined by the genes on its chromosomes. If we use n polygons to form a husky, we can also assume that the appearance of the husky is determined by the specific position and color of the polygons, which means that these polygons can be regarded as the “genes” of the husky.

Of course, it should be noted that this algorithm only gets the approximate solution of the problem rather than the exact solution, and there are some problems such as premature gene homogenization.

Algorithm design:

Genetic algorithm is only a framework, for specific problems, of course, we need to design a specific algorithm.

Time relationship, this algorithm design is all simple.

Step1:

Random generation of 100 noise images with the same size as the original image.

Step2:

Population variation (mutation probability 50%), i.e., adding some random noise to 100 generated images.

Step3:

Calculate the degree of difference from the original image. The calculation formula is as follows:

Step4:

The generated images with low differences from the original images are retained, and they are cross-fused to generate new images so that the number of the population remains the same.

Step5:

Loop through Step2-4.

Detailed implementation process see the source code in the relevant files.

Using the demonstration

Run gene.py in the CMD window.

Explanation of some parameters:

Take the Chrome icon (30×30) as an example:

If the image is larger, it will cost more time. T_T

You can see it’s kind of interesting.

** Interested students can run a few more rounds, see tens of thousands of rounds after the result is what

More and more

The first link in Resources is to an online test system for genetic algorithms to fit the Mona Lisa.

The final result is:

And of course there are other patterns:

This article ends here, thank you for watching, follow me every day to share Python case series, the next article to share OpenCV implementation cat face detection.

To thank you readers, I’d like to share with you some of my recent collection of dry programming goodies and give something back to every reader in the hope of helping you out.

Dry goods mainly include:

① More than 2000 Python e-books (mainstream and classic books are available)

(2) The Python Standard Library (Chinese version)

③ project source code (40 or 50 interesting and classic hands-on project and source code)

④Python basic introduction, crawler, web development, big data analysis video (suitable for small white learning)

⑤ Python Learning Roadmap (Goodbye to Slow Learning)

All done~ complete source code + dry goods see personal profile or private message to obtain the relevant files.