天天看點

python藝術分形數_Python分形盒計數-分形維數

我有一些圖像,我想計算Minkowski/box count dimension來确定圖像中的分形特征。下面是兩個示例圖像:

10.jpg:

python藝術分形數_Python分形盒計數-分形維數

24.jpg:

python藝術分形數_Python分形盒計數-分形維數

我使用以下代碼計算分形維數:import numpy as np

import scipy

def rgb2gray(rgb):

r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]

gray = 0.2989 * r + 0.5870 * g + 0.1140 * b

return gray

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 = rgb2gray(scipy.misc.imread("24.jpg"))

print("Minkowski–Bouligand dimension (computed): ", fractal_dimension(I))

從我讀過的文獻來看,有人認為自然場景(如24.jpg)在本質上更具分形性,是以應該具有更大的分形維數值

它給我的結果與文獻所暗示的相反:10.jpg:1.259

24.jpg:1.073

我希望自然圖像的分形維數大于城市圖像的分形維數

我計算代碼中的值是否不正确?或者我隻是不正确地解釋結果?