Box-Counting Dimension, Explained
After reading this you will be able to cover any shape with a grid, count the boxes it touches at several scales, and read off a non-integer dimension from the slope of a straight line on log-log paper.
What box-counting measures, and one example
A line has dimension 1. A filled square has dimension 2. A point has dimension 0. These are the dimensions you learned as a child, and they are all whole numbers. Box-counting dimension extends that idea to shapes that fall between the integers.
Here is the hook. Take a Koch curve: start with a straight segment, replace its middle third with two sides of a small triangle, then repeat on every segment forever. The result is a wiggly curve of infinite length that still fits inside a finite box. It is too crinkly to be a plain line, but it does not fill any area. Its box-counting dimension turns out to be \log 4 / \log 3 \approx 1.262. That number sits strictly between 1 and 2, and it captures exactly how the curve fills space more thoroughly than a smooth line but less than a region.
The method behind that number is plain counting. Lay a grid of square boxes of side \varepsilon over the shape. Count how many boxes the shape touches. Call that N(\varepsilon). Shrink the boxes and count again. The way N(\varepsilon) grows as \varepsilon shrinks tells you the dimension.
The intuition before the formula
Think about the shapes you already understand. Cover a straight segment of length 1 with boxes of side \varepsilon = 1/4. You need about 4 boxes. Halve the box size to 1/8 and you need about 8. Each time you halve \varepsilon, the count doubles. Count grows like 1/\varepsilon to the first power.
Now cover a filled unit square with the same boxes. At \varepsilon = 1/4 you need 16 boxes. At 1/8 you need 64. Halving \varepsilon multiplies the count by 4, so the count grows like 1/\varepsilon to the second power.
The exponent is the dimension. A line gives exponent 1, a region gives exponent 2. A fractal gives something in between because it fills space in a way that is neither.
The formula and how to fit it
Write the counting relationship as a power law:
Here N(\varepsilon) is the number of occupied boxes at box size \varepsilon, C is a constant that depends on the shape's overall size, and D is the box-counting dimension you want. The minus sign appears because smaller boxes mean a larger count.
Take the logarithm of both sides to turn the power law into a straight line:
Now plot \log(1/\varepsilon) on the horizontal axis and \log N(\varepsilon) on the vertical axis. The points fall near a line whose slope is D. The intercept is \log C and you can ignore it. Any base of logarithm works as long as you use the same base on both axes, because the base cancels in the slope.
To get D from several points, fit a line by least squares. For points (x_i, y_i) with x_i = \log(1/\varepsilon_i) and y_i = \log N(\varepsilon_i), the slope is:
where \bar{x} and \bar{y} are the means of the two coordinates. That single number is the estimate you report.
Box-counting dimension is defined by a limit as \varepsilon \to 0. On a real image or a finitely iterated fractal you never reach that limit, so you fit a slope over the range of scales you actually have. The estimate is a slope, not the exact limit.
A worked example on the Koch curve
The demo runs on a Koch curve with the field defaults. Work the numbers by hand using the natural scaling of the construction. At each stage of the Koch curve there are 4 copies of the previous shape, each scaled down by a factor of 3. That is exactly what a clean box count sees.
Counting Koch boxes at four scales
Choose box sizes that match the construction: \varepsilon = 1/3, 1/9, 1/27, 1/81. At each step the number of boxes the curve touches multiplies by 4.
- At \varepsilon = 1/3, the curve touches about
4boxes. So \log(1/\varepsilon) = \log 3 \approx 1.099 and \log N = \log 4 \approx 1.386. - At \varepsilon = 1/9, count is about
16. Then \log(1/\varepsilon) = \log 9 \approx 2.197 and \log N = \log 16 \approx 2.773. - At \varepsilon = 1/27, count is about
64. Then \log(1/\varepsilon) \approx 3.296 and \log N \approx 4.159. - At \varepsilon = 1/81, count is about
256. Then \log(1/\varepsilon) \approx 4.394 and \log N \approx 5.545.
Every step raises \log(1/\varepsilon) by \log 3 \approx 1.099 and raises \log N by \log 4 \approx 1.386. The slope between any two neighbouring points is 1.386 / 1.099 = 1.262. That is \log 4 / \log 3, the true Koch dimension, recovered from plain counting.
Reading and interpreting the slope
The slope you fit is the whole answer. Compare it against the two anchors: 1 for a smooth curve, 2 for a filled region. The Sierpinski triangle sits at \log 3 / \log 2 \approx 1.585 because each halving of the box triples the occupied count. A measured coastline usually lands between about 1.02 and 1.30 depending on how rugged it is. Britain's west coast is often quoted near 1.25.
The table below shows the ideal counting behaviour for four shapes. Read each row as the factor by which the box count multiplies when you halve \varepsilon, and the dimension that factor implies.
| Shape | Count factor per halving | Dimension |
|---|---|---|
| Smooth line | 2 | 1.000 |
| Koch curve | 2.520 | 1.262 |
| Sierpinski triangle | 3 | 1.585 |
| Filled square | 4 | 2.000 |
The Koch factor 2.520 comes from 2^{1.262}. Any factor between 2 and 4 gives a dimension between 1 and 2, which is the fractal band.
Common mistakes
The estimate is only as honest as the range of scales you fit. Watch for four traps.
- Fitting the flat ends
- At very large boxes the whole shape sits in one box, so N = 1 and the slope goes to 0. At very small boxes, once every box holds at most one pixel, doubling the resolution just doubles the count and the slope drifts toward 2. Fit only the straight middle of the log-log plot.
- Too few scales
- Two points always define a line, so two scales give a slope with no way to check it. Use at least four or five scales and look at how tightly they line up.
- Grid placement
- Shifting the grid by half a box can change N(\varepsilon) by 10 to 20 percent at coarse scales. Serious estimates average over several grid offsets.
- Confusing dimension with roughness you can see
- A dimension of 1.26 does not mean the curve is 26 percent longer. It describes a scaling rate, not a length. Fractal length grows without bound as \varepsilon shrinks.
A single clean slope does not prove a shape is fractal. Plenty of ordinary curves look straight on a log-log plot over a narrow range. Report the range of scales you used and the scatter around the line, not just the slope.
Related tools
The same self-similar fractals appear all over this site, built by very different rules. The Chaos Game grows the Sierpinski triangle from random jumps, and Pascal's Triangle mod n reveals the same shape by colouring remainders. Zoom into the Mandelbrot Explorer or slide the parameter in the Julia Set Explorer to see boundaries whose dimension is exactly 2. For a curve that fills a square completely, so its box dimension is 2, watch the Hilbert Curve Explorer. Strange attractors with fractal cross-sections show up in the Lorenz Attractor and the Hénon Map Attractor, and the Magnetic Pendulum Fractal shades a fractal basin boundary you could box-count yourself.
Frequently asked questions
Why is the dimension not a whole number?
Because the box count grows at a rate between the line rate and the area rate. For the Koch curve, halving the box multiplies the count by 2.52, which is neither the 2 of a line nor the 4 of a region. The exponent that produces 2.52 is 1.262, and that exponent is the dimension.
Does the choice of logarithm base matter?
No. The slope is a ratio of two logs, and changing base multiplies both by the same factor, which cancels. Natural log, base 10 and base 2 all give the same slope of 1.262 for the Koch curve.
Is box-counting dimension the same as Hausdorff dimension?
They agree for the standard fractals here (Koch gives 1.262, Sierpinski gives 1.585 by both definitions), but they can differ for stranger sets. Box-counting is easier to compute because it needs only a grid and a counter, which is why it is the practical choice for images.
How many scales do I really need?
Four to six clean scales spanning a factor of at least 10 in box size give a trustworthy slope. Fewer than three leaves you unable to judge whether the points are truly on a line.
Can I box-count a real photograph?
Yes, and that is how coastline and cloud-edge dimensions are estimated. Convert the image to black-and-white edges first, then count boxes that contain any edge pixel. Fit the slope over scales where a box holds many pixels down to scales near a few pixels, and stop before the single-pixel regime pulls the slope toward 2.