Measuring fractal dimension by box-counting


The term ‘fractal dimension’ was introduced by by Benoit Mandelbrot in 1967 to explain self-similarity of a pattern. A fractal dimension is defined as a ratio of the change in detail to the change in scale. It is used as an index that quantifies the complexity of a fractal pattern (

Famously, fractal dimension has been described in the context of the British coastline. A coastline’s measured length was observed to change depending on the length of the measuring stick used. In 2-D and 3-D, this notion can be extended to the length of a measuring pixel or voxel, respectively.


Box Counting Method

One way to determine fractal dimension of an image is through the box counting method. Measuring elements are laid over the image in a fixed grid pattern. As it scans over the grid, information is collected as to whether or not the measuring element is at the edge of the pattern. The number of measuring elements that are at the edge is then used to calculate the fractal dimension (

Mathematical Definition

The relationship of a pattern’s fractal dimension and its measuring element can be expressed as:

\(N \propto \frac{1}{D^F}\)

\(F = \lim_{D \to 0} \frac{-log N(D)}{log(D)}\)

  • N: number of measuring elements

  • D: size of measuring element

  • F: fractal dimension


A Sierpinski carpet has a known fractal dimension of 1.8928. Performing the box counting method found its fractal dimension as approximately 1.8 ~ 1.9.

First, import the needed packages.

import matplotlib.pyplot as plt
import porespy as ps

Generate a sierpinski carpet and visualize.

im = ps.generators.sierpinski_foam(4, 5)

Finally, apply the box count function and visualize.

data = ps.metrics.boxcount(im)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4))
ax1.set_xlabel('log D')
ax1.set_ylabel('log N')
ax2.set_xlabel('log D')
ax1.plot(data.size, data.count,'-o')
ax2.plot(data.size, data.slope,'-o');