File Exchange

image thumbnail


version (1.6 MB) by Frederic Moisy
Fractal dimension using the 'box-counting' method for 1D, 2D and 3D sets


Updated 10 Jul 2008

View License

BOXCOUNT Box-Counting of a D-dimensional array (with D=1,2,3).

The Box-counting method is useful to determine fractal properties of a
1D segment, a 2D image or a 3D array. If C is a fractal set, with fractal dimension DF < D, then the number N of boxes of size R needed to cover the set scales as R^(-DF). DF is known as the Minkowski-Bouligand dimension, or Kolmogorov capacity, or Kolmogorov dimension, or simply box-counting dimension.

[N, R] = BOXCOUNT(C), where C is a D-dimensional array (with D=1,2,3), counts the number N of D-dimensional boxes of size R needed to cover the nonzero elements of C. The box sizes are powers of two, i.e., R = 1, 2, 4 ... 2^P, where P is the smallest integer such that MAX(SIZE(C)) <= 2^P. If the sizes of C over each dimension are smaller than 2^P, C is padded with zeros to size 2^P over each dimension (e.g., a 320-by-200 image is padded to 512-by-512). The output vectors N and R are of size P+1. For a RGB color image (m-by-n-by-3 array), a summation over the 3 RGB planes is done first.

BOXCOUNT(C,'plot') also shows the log-log plot of N as a function of R
(if no output argument, this option is selected by default).

BOXCOUNT(C,'slope') also shows the semi-log plot of the local slope DF = - dlnN/dlnR as a function of R. If DF is contant in a certain range of R, then DF is the fractal dimension of the set C.

The execution time depends on the sizes of C. It is fastest for powers
of two over each dimension.


c = (rand(1,2048)<0.2);

c = randcantor(0.8, 512, 2);
figure, boxcount(c, 'slope');

Cite As

Frederic Moisy (2021). boxcount (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (36)

buenas tardes me presenta este error
Error using boxcount (line 51)
Not enough input arguments.

max segali

thanks for this..... how can I extract the slope of the curve? the scales are logaritmic so the basic fitting does not help. cheers

Ahmet Tuna

Han Wei

kavya negi

how do I download it?

Pablo Perez

which 3d image format is entered?

Shuqi Wang

Frederick White

I am trying to use this method currently but with many different images the boxcount plot always seems to be exactly the same.

How can you get the programme to return D value?


Athos Garcia

Thanks, great work on the demo!

eslam ali

Error using boxcount
Too many output arguments.

how can solve that


thanks of sharing this

hello thank you from your good code-I need codes about fractal simulations in Geology and mineral exploration Issues such as estimation and simulation for Thickness of Orebodies using fractal algorithms.thank you


Phumzile Mabuza

Hi, can I please ask how to get the boxcount or randcantor function, I am using 2014a student version and cannot find the functions. Where can Ii download from

please help!

orlando chancay

I am Working with this method
I need to calculate the fractal dimension of lesions in the Brain but this are very smalls, size of lesions is( 7x5x8 ) , and the result of FD in this lesions is around 1.4 to 2,2, and do not understand why this result, would know that should is 2-3.


Hi Frederic,

thanks for your submission, works really nicely.
to get if faster for 2D i propose instead of the inner loops:

c = c(1:2:end,1:2:end)|c(2:2:end,1:2:end)|c(1:2:end,2:2:end)|c(2:2:end,2:2:end);
n(g+1) = sum(c(:));


Nathan Orloff

I think the for loops in the function could be replaced with arrayfun to make the execution a little faster. And the output should be reshaped to be able to be used with cellfun. Other than that it is 1) correct and 2) awesome. I think that is the best you can hope for.

xiang fiona


What if one channel of an RGB image has some blocks equal to 0 ?
I modified your code:
[n,r] = boxcount(bcont);
if n==0,
DF(i,j,d) = 0;
DF(i,j,d) = mean(df);


Is it possible that your nice algorithm (i love it) is too much slow..?

Jeff Tuhtan

Thanks for the submission, it worked just fine analyzing the fractal dimension of river cross sections.

Marian Axente bounces me every time...

Frederic Moisy

Dear Marian
Please use my email address for questions or discussions about this submission. Algorithms to compute a fractal dimension (box-counting or other methods) are all approximations, which converge towards the "true" topological dimension in the limit of large scale separation. I hope this helps. - F.

Marian Axente are right. One more question in light of my fractal inexperience: why is the algorithm not give a full topological dimension whenever we put a square or a cube as input? Rather it gives 1.96 or 2.9. I thought that for smooth manifolds, the fractal dimension is equal to the topological dimension. Is there an approximation somewhere, or how could I modify your code to get a full 3 for a cube input?

Frederic Moisy

Dear Marian,
What does it mean "the program crashes"? Please provide an example. If I make the simplefollowing test to compute the "fractal" dimension of a square,
a(600:800, 1200:1400)=1;
it works properly (dimension close to 2 for small r, and close to 0 for large r, as expected)

Marian Axente

Hi Frederic

Could you point me in the right direction about your boxcounting algorithm? If I put a cube as input the program crashes; I thought that it was an easy way to check for consistency of the results.

Ashwin Sundar

Frederic Moisy

Dear Owen Horsfield,
This is not really a bug: boxcount does not give the *minimum* number of boxes needed to cover the set, but only the number of boxes starting from the 1st element. However, it is true that starting the box covering with the 1st element is arbitrary, and other choice may lead to slightly different results.

Owen Horsfield

[n,r]=boxcount([0 1 1 0])
I can put a single 1x2 box around the nonzero elements. So output should be:
n=[2 1 1]
r=[1 2 4]
However boxcount returns:
n=[2 2 1]
r=[1 2 4]
Algorithm problem? or am i missing something?

Carissa Pan

Thanks for sharing

Carissa Pan

Thanks for sharing.

ton malaka


clair et efficace ! merci pour le programme, il m'a fait gagné du temps

Guna Sekaran

Very good tool. It works. Thanks to Author. May be more docs will help for better understanding.

Martin Lopez

The program works.
It would be very useful more documentation.



MATLAB Release Compatibility
Created with R2006a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!