pythonpython-3.xnumpymatplotlibfractals

Box counting function for fractals not working


im making a python program to take an image and determine the fractal dimension with different grid sizes for the box counting. i had previously had it working and it got deleted and i cant remember exactly how i had it but i have something similar to it now. the main problem i have now is when the function count runs, N=1 for every size box.

this is my current code

def count(image, size):
    N=0
    step=size
    for i in range(0, Lx, step):
       for j in range(0, Ly, step):
           if (img_matrix[i:step,j:step] == 0).any(): 
               N += 1
               return N
       
size=np.arange(0,10, 1)
N=0
Ns=[]
for s in size:
    N=count(img,s)
    Ns.append(N)`

it only gives 1 as the value of Ns. how do i fix this?


Solution

  • To get it back to the values you recorded you just need to make a few minor tweaks.

    1. in your count_boxes function you need to change your slices from i:step and j:step -> to i:i+step and j:j+step

    2. You are returning from that same function on the first iteration every time so you need to dedent the return statement so it doesn't occur until after the loops have completed.

    the function would look something like this:

    
    def count_boxes(image, box_size):
        N=0
        step=box_size
        for i in range(0, Lx, step):
           for j in range(0, Ly, step):
               if (img_matrix[i:i+step,j:j+step] == 0).any():
                   N += 1
        return N
    

    Additional notes: