License

Copyright (c) 2008, Frederic Moisy
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution
* Neither the name of the University Paris Sud nor the names
of its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

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.

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

Hi
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.

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.

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.

Sorry...you 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?

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=zeros(4096,4096);
a(600:800, 1200:1400)=1;
boxcount(a,'slope')
it works properly (dimension close to 2 for small r, and close to 0 for large r, as expected)
F.

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

2 Oct 2008

Frederic Moisy

15 May 2008

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.