Rectify

Since a my research involves a lot of Monte Carlo (MC) simulation, I thought that it might be fun to make use of some of the algorithms in a somewhat different context. The result is two simple command-line utilities, rectify and flux, that use Monte Carlo processes to generate digital artwork.

Rectify starts with a blank canvas and converges towards a target image by applying a sequence of random rectangular black or white brush strokes. The strokes are blended with the existing pixels on the canvas and the trial is accepted or rejected according to a Metropois acceptance rule: moves that result in the canvas looking more like the target image are always accepted, while a thermal bath controls the probabilility of accepting moves that make the canvas less like the target.

The code works with Portable Network Graphics (PNG) images and uses LodePNG to avoid unneccessary dependency on libpng and zlib. All that's needed in order to compile the code is a recent version of gcc. As a result it should be fairly self-contained and portable.

There are several options that the user can tweak, such as the maximum size of the trial brush stroke, the number of MC iterations to execute, and the number of images to save as the algorithm progresses. Full details can be found on the rectify GitHub page.

Installation

The code can be downloaded and installed as follows:

git clone https://github.com/lohedges/rectify.git
cd rectify
make
sudo make install

Examples

To get started there are a few test images included in the examples folder. Running rectify as shown below converges towards a target image of Manhattan, NYC. The program executes 10 million trial brush strokes and saves a total of 1000 images at logarithmically spaced intervals. All of the output is saved to a directory called nyc, which will be created if it doesn't already exist.

cd examples
rectify nyc.png -d nyc -i 10000000 -f 1000 -l

The following short movie shows the output of the program. Watch how ghostly buildings appear from the blank canvas as the algorithm progresses.

Also included in the examples folder is a photograph of a zebra. Try running the following command, the movie shows an example of the resulting output.

rectify zebra.png -d zebra -i 1000000 -f 1000 -l