-
-
Save caiofcm/462f1177b7252df6755c5fc6a3ed1a7c to your computer and use it in GitHub Desktop.
Revisions
-
caiofcm revised this gist
Jan 18, 2021 . 1 changed file with 25 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,9 +6,10 @@ # fractal dimension of a set S in a Euclidean space Rn, or more generally in a # metric space (X, d). # ----------------------------------------------------------------------------- import cv2 import numpy as np import imageio def fractal_dimension(Z, threshold=0.9): @@ -49,6 +50,25 @@ def boxcount(Z, k): coeffs = np.polyfit(np.log(sizes), np.log(counts), 1) return -coeffs[0] def main_01(): filename = 'Koch_curve2.png' #'sierpinski2.png' #'t02.jpg' originalImage = cv2.imread(filename) I = cv2.cvtColor(originalImage, cv2.COLOR_BGR2GRAY)/256.0 # Koch curve should be 1.26 EXPECTEDS = { 'koch': 1.26, 'sierpinski': 1.5849 } Db = fractal_dimension(I, 0.9) print("Minkowski–Bouligand dimension (computed): ", Db) # print("Theoretical: ", (np.log(3)/np.log(2))) return if __name__ == "__main__": main_01() -
rougier revised this gist
Oct 21, 2016 . 1 changed file with 8 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,11 +15,15 @@ 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) @@ -33,20 +37,18 @@ def boxcount(Z, k): # 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))) -
rougier revised this gist
Oct 21, 2016 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -48,5 +48,5 @@ def boxcount(Z, k): # See https://en.wikipedia.org/wiki/Sierpinski_triangle to get one image 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))) -
rougier revised this gist
Oct 20, 2016 . No changes.There are no files selected for viewing
-
rougier created this gist
Oct 20, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,52 @@ # ----------------------------------------------------------------------------- # 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) 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) return np.count_nonzero(S) # 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**0) sizes = 2**np.arange(n, 0, -1) # Actual box counting with decreasing size counts = [] for size in sizes: counts.append(boxcount(Z, size)) # Fit coeffs = np.polyfit(np.log(sizes), np.log(counts), 1) return -coeffs[0] # See https://en.wikipedia.org/wiki/Sierpinski_triangle to get one image I = scipy.misc.imread("sierpinski.png")/256.0 print("Computed Hausdorff dimension: ", fractal_dimension(I)) print("Theoretical Hausdorff dimension:", (np.log(3)/np.log(2)))