StereoFractAnalyzer: measuring how rough a surface really is
A small Python package that turns 'jagged' into a number: the fractal dimension of a 3D surface from an STL file, or of a shape in a 2D image, by box counting. Here it is tested on shapes with known answers and run on a rough 3D mesh, slice by slice.
Some shapes are hard to describe with ordinary geometry. A coastline, a fracture surface, the outline of a soot particle: zoom in and new wiggles keep appearing. Length and area depend on how closely you look, but one number does not, at least over a range of scales: the fractal dimension. A smooth line has a dimension of 1 and a filled plane 2; a jagged curve sits somewhere in between, and the more it fills space, the higher its dimension.
StereoFractAnalyzer is a small Python package, written with Dr. Somesh Roy at Marquette University, that computes the fractal dimension of 3D surfaces (from STL mesh files) and of shapes in 2D images. It is on PyPI, with documentation at comp-comb.github.io/StereoFractAnalyzer.
Box counting, in one picture
The package uses box counting, the standard way to estimate a fractal dimension. Cover the shape with a grid of boxes and count how many boxes it touches. Shrink the boxes and count again, then again. For a smooth curve, boxes a third the size means about three times as many boxes; for a jagged one the count grows faster. On a log-log plot of box count against inverse box size, the points fall on a straight line, and the slope of that line is the fractal dimension.
The example is a Koch curve, whose dimension is known exactly (1.26). Four box sizes already give 1.23. That gap is a reminder that box counting is an estimate: it depends on the range of box sizes you choose, as the tests below show.
What it does
- 2D images. Load a picture of a shape, darker than its background, and get its fractal dimension. Pixels darker than mid-gray count as the shape, and ten box sizes, from half the image size down to one pixel and spaced evenly on a log scale, are counted.
- 3D surfaces. Load an STL mesh, the format most CAD and 3D-scanning tools export. The package slices the surface with horizontal planes, measures the fractal dimension of each slice’s outline, and turns the average into a surface fractal dimension. The slice spacing is yours to choose.
- Mesh tools. Translate, rotate and scale a model, convert it to a voxel grid, and view the model, the voxels or individual contour slices.
- Plots. Every calculation can show its log-log plot with the fitted line, so you can check that the points really fall on a straight line before trusting the number.
It is built on Open3D, NumPy, Matplotlib and Pillow, and comes with a test suite.
Shapes with known answers
Before trusting a tool like this, it is worth feeding it shapes whose answers are known. I drew six as black-and-white images, 1,024 pixels square (729 for the carpet, so that its holes line up with the pixels), and measured each with the package’s default settings: one call to get_image_fractal_dimension().
Every estimate lands within 0.09 of the exact value. The filled square comes out at 2.00 and the circle at 1.03; the Koch curve and the Sierpinski carpet come out low, the Sierpinski triangle a little high. The errors are not random noise. They come from the box sizes the fit uses.
Choosing the box sizes
By default the fit uses ten box sizes, from half the image down to a single pixel. The two ends of that range are the least reliable. The largest boxes number only a handful, so one box more or less moves the count a lot. The smallest are a pixel wide, and at that scale a line one pixel thick is no longer self-similar.
Narrowing the range is not a cure, though. The same change moves the Sierpinski triangle from 1.62 to 1.55, past its exact 1.58 to the other side. A box-counting dimension is a fit to data, and the honest way to use one is to look at the plot and fit the straight part. The lower-level method takes the range directly:
import numpy as np
from PIL import Image
from StereoFractAnalyzer import StereoFractAnalyzer
a = StereoFractAnalyzer()
img = Image.open("koch.png")
sizes, counts = a.box_count_image(img, min_size=2, max_size=128,
n_sizes=10)
# the slope of log(count) against log(1/size) is the dimension
D = np.polyfit(np.log(1 / np.array(sizes)), np.log(counts), 1)[0]
A 3D surface, slice by slice

The package ships with a test mesh, tests/test.stl: a rough, blob-shaped surface of 28,058 triangles, about 41 × 50 × 55 units across.
get_surface_fractal_dimension() cuts it with a horizontal plane at every whole unit of height, 55 planes in all. Where a plane crosses a triangle it leaves a short line segment, and together the segments trace the outline of that slice. Each outline is box-counted like a 2D shape. The surface dimension is then one more than the average slice dimension: a surface sits one dimension above its cross-sections, just as a smooth surface (2) has smooth curves (1) for slices.
One detail matters when you bring your own mesh. The 3D fit uses boxes from 1 to about 5.6 units of the mesh’s own length, whatever those units are, so the same object exported in meters and in millimeters gives different answers. Scale the model with transform(scale=...) so that its features span many of those boxes and its triangles are smaller than the smallest one. (The range is set by the attributes slope_min_point and slope_max_point, in powers of ten of one over the box size.)
The slices do not agree exactly, and the spread is worth looking at. The lowest value, 0.94, comes from a slice near the top, where the plane clips only a few small outlines. The highest, 1.49, comes from the middle of the object, where the outlines are longest and most convoluted.
Using it
Install it with pip (Python 3.6 or newer):
pip install StereoFractAnalyzer
From Python:
from StereoFractAnalyzer import StereoFractAnalyzer
# Fractal dimension of a shape in a 2D image (with a log-log plot)
img = StereoFractAnalyzer(img_path="particle.png")
print(img.get_image_fractal_dimension(plot=1))
# Fractal dimension of a 3D surface from an STL file
mesh = StereoFractAnalyzer(stl_path="surface.stl")
print(mesh.get_surface_fractal_dimension())
Or straight from the command line. Here it is on the two test files that come with the package, a jagged curve image and the mesh above:
$ StereoFractAnalyzer --img tests/test.png
The fractal dimension is: 1.5116328029275579
$ StereoFractAnalyzer --stl tests/test.stl
Calculating Fractal Dimension of the contour at z= 1
Calculating Fractal Dimension of the contour at z= 2
...
Fractal Dimension of the surface = 2.307222818312059
Both options can be given at once (--stl surface.stl --img particle.png).
Where it is useful
Anywhere a shape’s complexity matters more than its size, for example the morphology of particles and aggregates in electron-microscope images, surface roughness in materials science, rock and fracture surfaces in geology, and procedural shapes in computer graphics.
Limits
- An estimate, not a law. Real objects are fractal only over a limited range of scales, and the result depends on the box sizes used. Look at the log-log plot and fit the straight part.
- Images need contrast. The 2D method treats pixels darker than mid-gray as the shape, so images should be cleanly separated into shape and background first.
- Surfaces by slices. The 3D method averages horizontal slices, all weighted equally, including thin slices near the top and bottom that cut only a few small outlines. Its accuracy depends on the mesh resolution, the slice spacing and the scale of the mesh against the fixed box range.
Get it
StereoFractAnalyzer is open source under the MIT license. The code is on GitHub, the package on PyPI, and the documentation at comp-comb.github.io/StereoFractAnalyzer. Contributions and bug reports are welcome.
This material is based upon work supported by the National Science Foundation under Grant No. 2144290.
Update, 2026: inside soot particles
Since this post was written, StereoFractAnalyzer has done its first large job. In a study of the pores and surfaces of incipient soot particles, we took 2,654 particles from reactive molecular dynamics simulations at four temperatures, mapped their outer surfaces and inner cavities with MoloVol, converted the surfaces to STL meshes with PyMOL, and measured their fractal dimensions with this package.


The particle surfaces came out at a fractal dimension of about 2.22, the same at all four temperatures and close to the 2.25 ± 0.09 measured for carbon black: rough, irregular surfaces with plenty of area for chemistry to happen on. The paper is K. M. Mukut, E. Goudeli, G. A. Kelesidis and S. P. Roy, “Elucidating pore and surface features of soot nanoparticles using molecular dynamics simulations,” Fuel 406, 136692 (2026), doi:10.1016/j.fuel.2025.136692.