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
plt.ylabel('N(l), Number of boxes', fontsize = 18)
55
plt.title("Fractal Dimension Df = "+str(Df))
56
# -*- coding: utf-8 -*-
"""
@author: yacine.mezemate
"""
import scipy as sp
import numpy as np
import matplotlib.pyplot as plt
#*************Imgae import and zoom****************
image = sp.misc.imread('fractal_tree.JPG')
RescaledImage = image[10:1034, 120:1144, 2]
plt.figure(0)
plt.imshow(image)
plt.title("Original image")
#************Threshold*****************
ThresholdImage = np.zeros((1024,1024)) #Allocation of memory
for i in range(1024):
for j in range(1024):
if RescaledImage[i,j] < 50:
ThresholdImage[i,j] = 1
else:
ThresholdImage[i,j] = 0
plt.figure(1)
plt.imshow(ThresholdImage, cmap=plt.cm.gray)
plt.title("Imgae zoom with threshold")
#*************Box counting****************
width = max(ThresholdImage.shape) # Maximum resolution
p = sp.log(width)/sp.log(2)
l= np.zeros((p+1)) # Memory allocation of range of scale
nOnes = sp.zeros(p+1)
for n in range(int(p)+1):
l[n]=sp.power(2,n) # Resolution
nOnes[p] =sp.count_nonzero(ThresholdImage) # Number of ones
for n in range(int(p)-1, -1, -1):
nElem=ThresholdImage.shape[0]
ThresholdImage=(ThresholdImage[0:nElem:2,0:nElem:2]+ThresholdImage[0:nElem:2,1:nElem+1:2]+
ThresholdImage[1:nElem+1:2,0:nElem:2]+ThresholdImage[1:nElem+1:2,1:nElem+1:2])/4
nOnes[n] =sp.count_nonzero(ThresholdImage)
#*************Fractal dimension****************
y = sp.log(nOnes)
x = sp.log(l)
a=sp.polyfit(x,y,1)
Df=a[0]
print("Fractal Dimension:" + str(Df))
plt.figure(2)
plt.plot(x,y,ls='None',marker='*')
plt.xlabel('l,Box size', fontsize = 18)
plt.ylabel('N(l), Number of boxes', fontsize = 18)
plt.title("Fractal Dimension Df = " + str(Df))
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).