-
-
Save rougier/e5eafc276a4e54f516ed5559df4242c0 to your computer and use it in GitHub Desktop.
# ----------------------------------------------------------------------------- | |
# From https://en.wikipedia.org/wiki/Minkowski–Bouligand_dimension: | |
# | |
# In fractal geometry, the Minkowski–Bouligand dimension, also known as | |
# Minkowski dimension or box-counting dimension, is a way of determining the | |
# fractal dimension of a set S in a Euclidean space Rn, or more generally in a | |
# metric space (X, d). | |
# ----------------------------------------------------------------------------- | |
import scipy.misc | |
import numpy as np | |
def fractal_dimension(Z, threshold=0.9): | |
# Only for 2d image | |
assert(len(Z.shape) == 2) | |
# From https://github.com/rougier/numpy-100 (#87) | |
def boxcount(Z, k): | |
S = np.add.reduceat( | |
np.add.reduceat(Z, np.arange(0, Z.shape[0], k), axis=0), | |
np.arange(0, Z.shape[1], k), axis=1) | |
# We count non-empty (0) and non-full boxes (k*k) | |
return len(np.where((S > 0) & (S < k*k))[0]) | |
# Transform Z into a binary array | |
Z = (Z < threshold) | |
# Minimal dimension of image | |
p = min(Z.shape) | |
# Greatest power of 2 less than or equal to p | |
n = 2**np.floor(np.log(p)/np.log(2)) | |
# Extract the exponent | |
n = int(np.log(n)/np.log(2)) | |
# Build successive box sizes (from 2**n down to 2**1) | |
sizes = 2**np.arange(n, 1, -1) | |
# Actual box counting with decreasing size | |
counts = [] | |
for size in sizes: | |
counts.append(boxcount(Z, size)) | |
# Fit the successive log(sizes) with log (counts) | |
coeffs = np.polyfit(np.log(sizes), np.log(counts), 1) | |
return -coeffs[0] | |
I = scipy.misc.imread("sierpinski.png")/256.0 | |
print("Minkowski–Bouligand dimension (computed): ", fractal_dimension(I)) | |
print("Haussdorf dimension (theoretical): ", (np.log(3)/np.log(2))) |
hello
sorry, can anyone tell me how to change the data that I want to get from the link to my laptop data?
I want to try to extract features of an iris
thanks, @jankaWIS
and sorry if I ask again is the 52 line is
images = [
'https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/Peano_Sierpinski_carpet_4.svg/440px-Peano_Sierpinski_carpet_4.svg.png',
'https://upload.wikimedia.org/wikipedia/commons/thumb/b/b7/Sierpinski_carpet_3.svg/244px-Sierpinski_carpet_3.svg.png',
'https://upload.wikimedia.org/wikipedia/commons/f/f0/Flocke.PNG',
'https://upload.wikimedia.org/wikipedia/commons/thumb/3/3b/Quadratic_Koch_2.svg/2880px-Quadratic_Koch_2.svg.png',
'https://upload.wikimedia.org/wikipedia/commons/thumb/5/55/Quadratic_Koch.svg/300px-Quadratic_Koch.svg.png'
]
?
sorry I using the jupyter notebook so not to know about the number
Why are boxes of 2x2 excluded?
Glad the code is useful but I'm afraid there there's no proper way to cite a it. Maybe as a misc bibitem with the relevant url/author.