7
$\begingroup$

I am trying to discover the fractal dimension of the image of the following dendrite that was obtained by SEM (scanning electron microscope) by the box-counting method (Python). I found this code on the Internet here. I tried to place the image to obtain the fractal dimension but it gave me 1.999872162725305, which does not coincide with the literature, as the literature says that usually the fractal dimensions of these structures must be below 1.80. The image found on the site as an example is the Sierpinski triangle whose fractal dimension is log (3)/log (2) and the code gives the right value, but the image has RGB colors and my SEM image is black and white. Is that the reason influencing the wrong result? How can I correct the code in order to obtain a fractal dimension below 1.80 as is supposed? Best regards.

import numpy as np
import pylab as pl
 
def rgb2gray(rgb):
    r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]
    gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
    return gray
 
image=rgb2gray(pl.imread("dendrite.png"))
# finding all the non-zero pixels
pixels=[]
for i in range(image.shape[0]):
    for j in range(image.shape[1]):
        if image[i,j]>0:
            pixels.append((i,j))
 
Lx=image.shape[1]
Ly=image.shape[0]
print (Lx, Ly)
pixels=pl.array(pixels)
print (pixels.shape)
 
# computing the fractal dimension
#considering only scales in a logarithmic list
scales=np.logspace(0.01, 1, num=10, endpoint=False, base=2)
Ns=[]
# looping over several scales
for scale in scales:
    print ("======= Scale :",scale)
    # computing the histogram
    H, edges=np.histogramdd(pixels, bins=(np.arange(0,Lx,scale),np.arange(0,Ly,scale)))
    Ns.append(np.sum(H>0))
 
# linear fit, polynomial of degree 1
coeffs=np.polyfit(np.log(scales), np.log(Ns), 1)
 
pl.plot(np.log(scales),np.log(Ns), 'o', mfc='none')
pl.plot(np.log(scales), np.polyval(coeffs,np.log(scales)))
pl.xlabel('log $\epsilon$')
pl.ylabel('log N')
pl.savefig('sierpinski_dimension.pdf')
 
print ("The Hausdorff dimension is", -coeffs[0]) #the fractal dimension is the OPPOSITE of the fitting coefficient
np.savetxt("scaling.txt", list(zip(scales,Ns)))

SEM image (dendrite.png):

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ Maybe you can artificially raise the contrast. I suspect its considering the noise of the background as part of the fractal, from a quick read it doesn't seem that grayscale should be the issue. $\endgroup$ Jan 29 at 15:29
  • $\begingroup$ @TristanMaxson Thank you. I changed the background and it started working. Regards. $\endgroup$ Feb 6 at 15:46
3
$\begingroup$

As stated in my comment, the easiest thing to look at is the contrast of the image. It seems you were able to change the background to prevent the code from detecting the shape incorrectly.

You can post process these images in programs such as paint.net, gimp, photoshop, etc. Changing the image from grayscale to black and white is probably the best thing to do.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.