Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Python plus genetic algorithm fits the image


Jun 01, 2021 Article blog



This article was reproduced to Know ID: Charles (Bai Lu) knows his personal column

Download the W3Cschool Mobile App, 0 Foundation Anytime, Anywhere Learning Programming >> Poke this to learn

Lead

It's a really...

Found unknowingly and haven't sent an article for days.

It happens to see an interesting application of genetic algorithms, using genetic algorithms to fit images, and then come and share a wave.

Let's start happily


Related documents

Baidu web download link: https://pan.baidu.com/s/1VMyY7u2C9Zq0vlMJZE1GSw

Password: ec77


Develop tools

Python version: 3.6.4

Related modules:

PIL module;

And some python comes with modules.

Environment construction

Install Python and add it to the environment variable, and pip installs the relevant modules that are required.

Resources

50 translucent polygons to recreate the Mona Lisa:

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

Scientific Squirrel Society Genetic Algorithms:

http://songshuhui.net/archives/10462

Introduction to the principle

Adapted references to the story:

Once upon a time, there was a group of scallops living on the shore, and they had no worries about food and clothing, and had houses and cars. But the good times did not last long, and the arrival of mankind broke their otherwise carefree lives.

Every once in a while, humans dig up some of them. However, these people don't like scallops that look like husky, so they always choose scallops that don't look like husky.

This has been going on for tens of thousands of years. By this time, the magic had happened: all the scallop shells were printed with husky!

Genetic algorithm - one of the meta-heuristic algorithms:

First salute a wave of Darwin?

 Python plus genetic algorithm fits the image1

Simply put, the genetic algorithm simulates the evolution of populations under selection pressure in nature, and thus obtains an approximate solution to the corresponding problems.

Specifically, what an organism looks like is largely determined by genes on the chromosome. If we use n polygons to form a husky, we can also assume that what husky looks like is determined by the specific location and color of these polygons, which means that these polygons can be regarded as husky's "genes".

Of course, it is important to note that this algorithm results in only an approximate solution to the problem rather than an accurate solution, and there are problems such as premature homization of genes.

Cold joke from http://xkcd.com/534/:

 Python plus genetic algorithm fits the image2

Algorithm design:

Genetic algorithms are just a framework, and they certainly need to be designed for specific problems.

Time relationship, this algorithm designs everything from Jane.

Step1:

Randomly generate 100 noise images of the same size as the original image.

Step2:

Population variation (50% probability of variation), i.e. some random noise is added to 100 resulting images.

Step3:

Calculate the difference from the original image. The formula is:

 Python plus genetic algorithm fits the image3

Step4:

Retain the resulting images that differ less from the original image and cross-merge them to generate new images so that the population remains the same.

Step5:

Loop through Step2-4.

The implementation process is detailed in the source code in the relevant file.


Use the demo

Run the Genetic.py file in the cmd window.

Some of the parameters explain:

 Python plus genetic algorithm fits the image4

Take the chrome icon (size 30×30):

If the image is large, the time spent is relatively large T_T

 Python plus genetic algorithm fits the image5

You can see that there's still something interesting about it.

Interested students can run a few more rounds, see what the results after tens of thousands of rounds, the time relationship I will not continue to run.


more

The first link in the resource is a genetic algorithm that fits the Mona Lisa's online test system.

The end result is:

 Python plus genetic algorithm fits the image6

Of course there are other patterns:

 Python plus genetic algorithm fits the image7

emmm, much better than my results.

When I'm free, optimize what I write myself