Fractal

Fractal dimension estimation

We introduced in this lecture, several fractal object that can be found in nature. We propose an exercise to implement the estimation of the fractal dimension. To this end we use tree image, and chuck if it is a fractal object.

Image of fractal tree

Question

  • Use the given python program to estimation the fractal dimension

  • Interpret the obtained results.

Solution
1
# -*- coding: utf-8 -*-
2
"""
3
@author: yacine.mezemate
4
"""
5
import scipy as sp
6
import numpy as np
7
import matplotlib.pyplot as plt
8
9
#*************Imgae import and zoom****************
10
image = sp.misc.imread('fractal_tree.JPG')
11
RescaledImage = image[10:1034, 120:1144, 2]
12
plt.figure(0)
13
plt.imshow(image)
14
plt.title("Original image")
15
16
#************Threshold*****************
17
ThresholdImage = np.zeros((1024,1024)) #Allocation of memory
18
for i in range(1024):
19
    for j in range(1024):
20
        if RescaledImage[i,j] < 50:
21
            ThresholdImage[i,j] = 1
22
        else:
23
            ThresholdImage[i,j] = 0
24
plt.figure(1)            
25
plt.imshow(ThresholdImage, cmap=plt.cm.gray)  
26
plt.title("Imgae zoom with threshold")    
27
      
28
#*************Box counting****************
29
width = max(ThresholdImage.shape) # Maximum resolution
30
p = sp.log(width)/sp.log(2)       
31
l= np.zeros((p+1))               # Memory allocation of range of scale
32
nOnes = sp.zeros(p+1)
33
34
for n in range(int(p)+1):
35
        l[n]=sp.power(2,n)       # Resolution
36
        
37
nOnes[p] =sp.count_nonzero(ThresholdImage) # Number of ones
38
for n in range(int(p)-1, -1, -1):
39
    nElem=ThresholdImage.shape[0]
40
    ThresholdImage=(ThresholdImage[0:nElem:2,0:nElem:2]+ThresholdImage[0:nElem:2,1:nElem+1:2]+
41
                    ThresholdImage[1:nElem+1:2,0:nElem:2]+ThresholdImage[1:nElem+1:2,1:nElem+1:2])/4
42
    nOnes[n] =sp.count_nonzero(ThresholdImage)
43
44
#*************Fractal dimension****************
45
y = sp.log(nOnes)
46
x = sp.log(l)
47
a=sp.polyfit(x,y,1)       
48
Df=a[0]
49
50
print("Fractal Dimension:" + str(Df))
51
plt.figure(2)
52
plt.plot(x,y,ls='None',marker='*')
53
plt.xlabel('l,Box size', fontsize = 18)
54
plt.ylabel('N(l), Number of boxes', fontsize = 18)
55
plt.title("Fractal Dimension Df = " + str(Df))
56
  • The first remark (observation) that we can make is there is a scaling regime over the whole range of scale. We can observe also that the fractal dimension is less then the Euclidean dimension (Df<2).

PreviousPreviousNextNext
HomepageHomepagePrintPrintCreated with Scenari (new window)