View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

3.9 | 18 ratings Rate this file 96 Downloads (last 30 days) File Size: 1.6 MB File ID: #13063 Version: 1.0



Frederic Moisy (view profile)


21 Nov 2006 (Updated )

Fractal dimension using the 'box-counting' method for 1D, 2D and 3D sets

| Watch this File

File Information

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');


This file inspired Dicom Operator Esme Process, Dem To Volumetiric, and Audio Watermarking By Selvakarna.

MATLAB release MATLAB 7.2 (R2006a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (28)
27 Jan 2017 Athos Garcia

Thanks, great work on the demo!

22 Apr 2016 eslam ali

Error using boxcount
Too many output arguments.

how can solve that

Comment only
26 Feb 2016 cobra

cobra (view profile)

thanks of sharing this

04 Oct 2015 seyed hosein alhoseiny

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

06 Feb 2015 10B

10B (view profile)

09 Jul 2014 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!

Comment only
20 Feb 2014 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.

Comment only
04 Feb 2013 kinor

kinor (view profile)

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(:));


20 Apr 2012 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.

18 Jul 2011 xiang fiona

03 Jun 2010 Luca

Luca (view profile)

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);

Comment only
25 May 2010 Luca

Luca (view profile)

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

28 Feb 2010 Jeff Tuhtan

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

02 Nov 2009 Marian Axente bounces me every time...

Comment only
30 Oct 2009 Frederic Moisy

Frederic Moisy (view profile)

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.

Comment only
30 Oct 2009 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?

Comment only
29 Oct 2009 Frederic Moisy

Frederic Moisy (view profile)

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)

Comment only
26 Oct 2009 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.

02 Oct 2008 Ashwin Sundar

15 May 2008 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.

Comment only
09 May 2008 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?

07 May 2008 Carissa Pan

Thanks for sharing

07 May 2008 Carissa Pan

Thanks for sharing.

07 May 2008 ton malaka

19 Feb 2008 Fabien MONTEL

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

28 Dec 2007 Guna Sekaran

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

23 Nov 2007 Martin Lopez

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

05 Feb 2007 SIMONA SSSS


Comment only

Contact us