I know, having a sister, that a day before Eid is a tough day for girls around the world. There are a lot of decisions and preparations that go into this 3 day period of festivities. One little dilemma, that my sister has always had is choosing between the aesthetically most appealing mehndi design. As it is ‘chaand raat’ or the night the moon of eid is sighted, I thought I might do a fast blog post on how to automate this decision and make life of sisters around the world, a little easier. But to do this we, first, have to go back in time.
A little Step Back in Time
In early 20th century, there began a discovery of mathematical graphs that were continuous everywhere but nowhere differentiable (fancy way of saying that the are not smooth). Lack of smoothness of such graphs rendered them useless to the mathematics of that time. Some specialists such as Poincare (the guy who discovered Einstein’s theory before him but got a tad bit late in reporting it) considered them useless; He argued that such graphs will have no use whatsoever, however, he argued, that they could be used to torture old school mathematicians.
Long story short, there have been number of mathematicians scratching their heads about these peculiar graphs until a dude named, Benoit Mandelbrot came in the scene. Mandelbrot was a maverick and would definitely be a rock star, had he not found his way spiraling in to Mathematics. He showed that such graphs/functions can be useful too; he called them fractals, which were derived from the word ‘Fractus’ in Latin, which means broken.
Fractals are geometrical structures that have fine scale, roughness and self-similar motifs which repeat as the fractal is further zoomed in or out. It is really crazy. You guys must check these out on internet. They are very intriguing. Fractals are of infinite length but contain a finite area (Trust me, I am not tripping). Following are some examples of these graphs.
The Koch Snowflake
Koch curve was introduced by Helge von Koch in 1904. To draw a Koch curve, start with drawing an equilateral triangle. Secondly, divide each line segment into three segments of equal length. Thirdly, draw an equilateral triangle that has the middle segment from step 2 as its base and which points outward. And finally, remove the line segment that is the base of the triangle from step 3. Keep repeating the process. The more the process is repeated, the complex the figure gets.
The Sierpinski’s Triangle
Sierpenski’s Triangle often called Sierpenski’s gasket, is constructed by following five simple steps. First, draw an equilateral triangle. Secondly, construct another triangle exactly in the centre of the first triangle, such that all vertices of this central triangle touch the mid-point of each side of the first triangle. Step two generates 3 triangles. And finally, repeat the step 2 using each smaller triangle as a template. Following figure is the evolution of Sierpenski’s triangle up to 5 iterates.
So now you have seen how these ‘cute’ mathematical motifs can be so devastating to mathematicians for centuries. Lets see how mathematicians finally tamed them. The changed the definition of a dimension. I promise I won’t go in the details here. I will just demonstrate how this new dimension is calculated. I am saving mathematics for a later, more detailed(and sadistic) post.
Calculating Fractal Dimension
Let’s assume you were given a task of calculating the true perimeter of British Isles. And with all enthusiasm you set out to complete your task.
You bought a new set of rulers. However, you soon realized that the perimeter of isles depends on the size of your rulers. The larger the size of your rulers, the smaller, is the perimeter of the Isles you can measure!
In fact, if you gradually decrease the size of the rulers or boxes in a grid, you notice that the measure of perimeter increase exponentially.
If we take the log of the box size and box count you can have a nice linear increase in the box size vs number of boxes in the grid.
If you plot it, it looks nice and crisp line.
Our new dimension, is a slope of this line. Slope is exactly calculated like you did in the school. That is by taking rise over run. In British isles’ case, the fractal dimension is 1.54. Intuitively, we can say that British isles are more complicated than a line which has a dimension and less complicated than a plane which has a dimension 2. Or more pretty than a line and less pretty than a 2D plane. From now on, we are going to call it, ‘fractal dimension’.
Following is how you can calculate the dimension in python.
import numpy as np import scipy.misc import scipy.stats as stats import os, os.path def rgb2gray(rgb): #converting images into gray scale for now as it is the standard. But we can play around with other filters too. r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2] gray = 0.2989 * r + 0.5870 * g + 0.1140 * b return gray def fractal_dimension(Z, threshold=50): # Threshold defines the final pattern of image. We cna play around with it. We can choose different values to see how pattern changes wrt Fractal dimensions. assert(len(Z.shape) == 2) # This caluclates Minkowski–Bouligand dimension, which is commonly known as Box-counting dimension. def boxcount(Z, k): S = np.add.reduceat( np.add.reduceat(Z, np.arange(0, Z.shape[0], k), axis=0), np.arange(0, Z.shape[1], k), axis=1) # We count non-empty (0) and non-full boxes (k*k) return len(np.where((S > 0) & (S < k*k))[0]) # Transform Z into a binary array Z = (Z <span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>< threshold) # Minimal dimension of image p = min(Z.shape) # Greatest power of 2 less than or equal to p n = 2**np.floor(np.log(p)/np.log(2)) # Extract the exponent n = int(np.log(n)/np.log(2)) # Build successive box sizes (from 2**n down to 2**1) sizes = 2**np.arange(n, 1, -1) # Actual box counting with decreasing size counts = [] for size in sizes: counts.append(boxcount(Z, size)) # Fit the successive log(sizes) with log (counts) coeffs = np.polyfit(np.log(sizes), np.log(counts), 1) return -coeffs[0]
But wait! Didn't I promise you that the post is about automatically finding more aesthetic mehndi designs?
Actually, to find the most beautiful mehndi design, all you have to do is to take the image of the design and calculate the fractal dimension of it using this code.
Number of research articles have reported that the design with highest fractal dimension is the one that is most beautiful! In fact, our history of art is is rich with fractals.
Aesthetics of Fractals
Geometry has always played a key role in arts. It was Euclidean(ordinary school geometry) geometry that brought about revolution in western arts by providing artists the way of drawing 3 dimensional objects on two dimensional plane of paper or canvas. Although it appears to be natural to us now, it was not the case before the renaissance period in arts. As the formalism of Euclidean geometry preceded its application in arts, this is not quite the case with fractal geometry. Fractal images exist in arts since prehistoric times and the formalism of fractal geometry was not complete until recently. Even the first deterministic fractal that Mandelbrot plotted was in late 1970s using computers. Generating fractals, other than simple curves mentioned above, was impossible before the advent of computers.
It can be postulated that fractals came in to arts through observing such phenomena in nature as nature is full of fractal like objects, which behave like deterministic fractals. The mystery that was associated with the art in fractal is that the fractal nature is clearly depicted in the ancient art. Secondly, the art from ancient times has evolved from fractals having lower dimensions to that of fractals having higher dimensions.
An American physicist Clint Sprott has demonstrated in 1993 that fractal dimension is clearly linked with aesthetics. That is the greater the dimension of a fractal the more aesthetically pleasing it is to us. And B. Sephar and Collin Clifford et al. further showed that this relationship doesn’t only hold with naturally occurring fractal objects, but exists in images irrespective of the method used to make them. That is either natural, human hand or mathematical iteration produces them. Thus a fractal appears to be more and more aesthetically pleasing to us the more complex it is. Why this relationship holds is still a mystery. Following are interesting examples of fractal occurrences in arts, even before they were defined properly by B. Mandelbrot.