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?
To get it back to the values you recorded you just need to make a few minor tweaks.
in your
count_boxesfunction you need to change your slices fromi:stepandj:step-> toi:i+stepandj:j+stepYou 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:
Additional notes:
you have a few indentation errors that I am assuming are just from copy and paste formatting errors.
you create the
imgat the lineimg = Image.fromarray(...)object and then pass it to thecount_boxesfunction but then never do anything with it, so it is useless.you should look at opencv
cv2for the threshold and grayscaling features.