📄 demo.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"><html xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <!--This HTML is auto-generated from an M-file.To make changes, update the M-file and republish this document. --> <title>Computing a fractal dimension with Matlab: 1D, 2D and 3D Box-counting</title> <meta name="generator" content="MATLAB 7.6"> <meta name="date" content="2008-07-09"> <meta name="m-file" content="demo"><style>body { background-color: white; margin:10px;}h1 { color: #990000; font-size: x-large;}h2 { color: #990000; font-size: medium;}/* Make the text shrink to fit narrow windows, but not stretch too far in wide windows. */ p,h1,h2,div.content div { max-width: 600px; /* Hack for IE6 */ width: auto !important; width: 600px;}pre.codeinput { background: #EEEEEE; padding: 10px;}@media print { pre.codeinput {word-wrap:break-word; width:100%;}} span.keyword {color: #0000FF}span.comment {color: #228B22}span.string {color: #A020F0}span.untermstring {color: #B20000}span.syscmd {color: #B28C00}pre.codeoutput { color: #666666; padding: 10px;}pre.error { color: red;}p.footer { text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;} </style></head> <body> <div class="content"> <h1>Computing a fractal dimension with Matlab: 1D, 2D and 3D Box-counting</h1> <introduction> <p>F. Moisy, 9 july 2008.</p> <p>Laboratory FAST, University Paris Sud.</p> </introduction> <h2>Contents</h2> <div> <ul> <li><a href="#1">About Fractals and box-counting</a></li> <li><a href="#2">About the 'boxcount' package for Matlab</a></li> <li><a href="#3">Box-counting of a 2D image</a></li> <li><a href="#7">Local scaling exponent</a></li> <li><a href="#11">Box-counting of a natural image.</a></li> <li><a href="#15">Generalized random Cantor sets</a></li> <li><a href="#19">1D random Cantor set</a></li> <li><a href="#20">3D random Cantor set</a></li> <li><a href="#21">More?</a></li> </ul> </div> <h2>About Fractals and box-counting<a name="1"></a></h2> <p>A set (e.g. an image) is called "fractal" if it displays self-similarity: it can be split into parts, each of which is (at least approximately) a reduced-size copy of the whole. </p> <p>A possible characterisation of a fractal set is provided by the "box-counting" method: The number N of boxes of size R needed to cover a fractal set follows a power-law, N = N0 * R^(-DF), with DF<=D (D is the dimension of the space, usually D=1, 2, 3). </p> <p>DF is known as the Minkowski-Bouligand dimension, or Kolmogorov capacity, or Kolmogorov dimension, or simply box-counting dimension. </p> <p>To learn more about box-counting, fractals and fractal dimensions:</p> <p>- <a href="http://en.wikipedia.org/wiki/Fractal">http://en.wikipedia.org/wiki/Fractal</a></p> <p>- <a href="http://en.wikipedia.org/wiki/Box_counting_dimension">http://en.wikipedia.org/wiki/Box_counting_dimension</a></p> <p>- <a href="http://mathworld.wolfram.com/Fractal.html">http://mathworld.wolfram.com/Fractal.html</a></p> <p>- <a href="http://mathworld.wolfram.com/CapacityDimension.html">http://mathworld.wolfram.com/CapacityDimension.html</a></p> <h2>About the 'boxcount' package for Matlab<a name="2"></a></h2> <p>The following examples illustrate how to use the Matlab package 'boxcount' to compute the fractal dimension of 1D, 2D or 3D sets, using the 'box-counting' method. </p> <p>The directory contains the main function 'boxcount', three sample images, and an additional function 'randcantor' to generate 1D, 2D and 3D generalized random Cantor sets. </p> <p>Type 'help boxcount' or 'help randcantor' for more details.</p> <h2>Box-counting of a 2D image<a name="3"></a></h2> <p>Let's start with the image 'dla.gif', a 800x800 logical array (i.e., it contains only 0 and 1). It originates from a numerical simulation of a "Diffusion Limited Aggregation" process, in which particles move randomly until they hit a central seed. (see P. Bourke, <a href="http://local.wasp.uwa.edu.au/~pbourke/fractals/dla/">http://local.wasp.uwa.edu.au/~pbourke/fractals/dla/</a> ) </p><pre class="codeinput">c = imread(<span class="string">'dla.gif'</span>);imagesc(~c)colormap <span class="string">gray</span>axis <span class="string">square</span></pre><img vspace="5" hspace="5" src="demo_01.png"> <p>Calling boxcount without output arguments simply displays N (the number of boxes needed to cover the set) as a function of R (the size of the boxes). If the set is a fractal, then a power-law N = N0 * R^(-DF) should appear, with DF the fractal dimension (Kolmogorov capacity). </p><pre class="codeinput">boxcount(c)</pre><img vspace="5" hspace="5" src="demo_02.png"> <p>The result of the box count can be obtained using:</p><pre class="codeinput">[n, r] = boxcount(c)loglog(r, n,<span class="string">'bo-'</span>, r, (r/r(end)).^(-2), <span class="string">'r--'</span>)xlabel(<span class="string">'r'</span>)ylabel(<span class="string">'n(r)'</span>)legend(<span class="string">'actual box-count'</span>,<span class="string">'space-filling box-count'</span>);</pre><pre class="codeoutput">n = Columns 1 through 7 44000 27466 11786 4265 1386 421 121 Columns 8 through 11 37 12 4 1r = Columns 1 through 7 1 2 4 8 16 32 64 Columns 8 through 11 128 256 512 1024</pre><img vspace="5" hspace="5" src="demo_03.png"> <p>The red dotted line shows the scaling N® = R^-2 for comparision, expected for a space-filling 2D image. The discrepancy between the two curves indicates a possible fractal behaviour. </p> <h2>Local scaling exponent<a name="7"></a></h2> <p>If the set has some fractal properties over a limited range of box size R, this may be appreciated by plotting the local exponent, D = - d ln N / ln R. For this, use the option 'slope': </p><pre class="codeinput">boxcount(c, <span class="string">'slope'</span>)</pre><img vspace="5" hspace="5" src="demo_04.png"> <p>Strictly speaking, the local exponent is not constant, but lies in the range [1.6 1.8].</p> <p>Let's try now with another image, the so-called Apollonian gasket (Wikipedia, <a href="http://en.wikipedia.org/wiki/Image:Apollonian_gasket.gif">http://en.wikipedia.org/wiki/Image:Apollonian_gasket.gif</a> ). The background level is 198 for this image, so this value is used to binarize the image: </p><pre class="codeinput">c = imread(<span class="string">'Apollonian_gasket.gif'</span>);c = (c<198);imagesc(~c)colormap <span class="string">gray</span>axis <span class="string">square</span>figureboxcount(c)figureboxcount(c,<span class="string">'slope'</span>)</pre><img vspace="5" hspace="5" src="demo_05.png"> <img vspace="5" hspace="5" src="demo_06.png"> <img vspace="5" hspace="5" src="demo_07.png"> <p>The local slope shows that the image is indeed approximately fractal, with a fractal dimension DF = 1.4 +/- 0.1 for scales R < 100. </p> <h2>Box-counting of a natural image.<a name="11"></a></h2> <p>Consider now this RGB (2272x1704) picture of a tree (J.A. Adam, <a href="http://epod.usra.edu/archive/images/fractal_tree.jpg">http://epod.usra.edu/archive/images/fractal_tree.jpg</a> ): </p><pre class="codeinput">c = imread(<span class="string">'fractal_tree.jpg'</span>);image(c)axis <span class="string">image</span></pre><img vspace="5" hspace="5" src="demo_08.png"> <p>Let's extract a rectangle in the blue (3rd) plane, and binarize the image for levels < 80 (white pixels are logical 'true'):</p><pre class="codeinput">i = c(1:1200, 120:2150, 3);bi = (i<80);imagesc(bi)colormap <span class="string">gray</span>axis <span class="string">image</span></pre><img vspace="5" hspace="5" src="demo_09.png"> <pre class="codeinput">[n,r] = boxcount(bi,<span class="string">'slope'</span>);</pre><img vspace="5" hspace="5" src="demo_10.png"> <p>The boxcount shows that the local exponent is approximately constant for less than one decade, in the range 8 < R < 128 (the 'exact' value of Df depends on the threshold, 80 gray levels here): </p><pre class="codeinput">df = -diff(log(n))./diff(log(r));disp([<span class="string">'Fractal dimension, Df = '</span> num2str(mean(df(4:8))) <span class="string">' +/- '</span> num2str(std(df(4:8)))]);</pre><pre class="codeoutput">Fractal dimension, Df = 1.801 +/- 0.06394</pre><h2>Generalized random Cantor sets<a name="15"></a></h2> <p>Fractal sets may be obtained from an IFS (iterated function system). For example, the function 'randcantor' provided with the package generates a 1D, 2D or 3D generalized random Cantor set. This set is obtained by iteratively dividing an initial set filled with 1 into 2^D subsets, and setting each subset to 0 with probability P. The result is a fractal set (or "fractal dust") of dimension DF = D + log(P)/log(2) < D. </p> <p>The following example generates a 2048x2048 image with probability P=0.8, i.e. fractal dimension DF = 1.678.</p><pre class="codeinput">c = randcantor(0.8, 2048, 2);imagesc(~c)colormap <span class="string">gray</span>axis <span class="string">image</span></pre><img vspace="5" hspace="5" src="demo_11.png"> <p>Let's see its box-count and local exponent</p><pre class="codeinput">boxcount(c)figureboxcount(c, <span class="string">'slope'</span>)</pre><img vspace="5" hspace="5" src="demo_12.png"> <img vspace="5" hspace="5" src="demo_13.png"> <p>For such set generated by an iterated scheme, the local slope shows as expected a well defined plateau, around DF = 1.7.</p> <h2>1D random Cantor set<a name="19"></a></h2> <p>1D random Cantor sets may also be generated. Here, a 2^18 = 262144 long set with P = 0.9 and expected fractal dimension DF = 1 + log(P)/log(2) = 0.848: </p><pre class="codeinput">ticc = randcantor(0.9, 2^18, 1, <span class="string">'show'</span>);figureboxcount(c, <span class="string">'slope'</span>);toc</pre><pre class="codeoutput">Elapsed time is 4.022846 seconds.</pre><img vspace="5" hspace="5" src="demo_14.png"> <img vspace="5" hspace="5" src="demo_15.png"> <h2>3D random Cantor set<a name="20"></a></h2> <p>Now a 3D random Cantor set of size (2^7)^3 = 128^3 with P = 0.7 and expected fractal dimension DF = 3 + log(P)/log(2) = 2.485. Note that 3D sets cannot be displayed using randcantor. </p><pre class="codeinput">ticc = randcantor(0.7, 2^7, 3);tocticboxcount(c, <span class="string">'slope'</span>);toc</pre><pre class="codeoutput">Elapsed time is 12.197119 seconds.Elapsed time is 0.186419 seconds.</pre><img vspace="5" hspace="5" src="demo_16.png"> <h2>More?<a name="21"></a></h2> <p>That's all. To learn more about this package, type:</p><pre class="codeinput">help <span class="string">boxcount.m</span></pre><pre class="codeoutput"> BOXCOUNT Box-Counting of a D-dimensional array (with D=1,2,3). [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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -