gpt4 book ai didi

Python分形盒计数——分形维数

转载 作者:太空狗 更新时间:2023-10-29 21:48:35 26 4
gpt4 key购买 nike

我有一些图像要计算 Minkowski/box count dimension以确定图像中的分形特征。以下是 2 个示例图片:

10.jpg:

enter image description here

24.jpg:

enter image description here

我正在使用以下代码来计算分形维数:

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

我希望自然图像的分形维数比城市图像大

我在代码中计算的值是否错误?或者我只是错误地解释了结果?

最佳答案

对于物理事物的分形维度,该维度可能会在不同阶段收敛到不同的值。例如,一条非常细的线(但宽度有限)最初看起来是一维的,然后最终变成二维的,因为它的宽度变得与所使用的框的大小相当。

让我们看看您生成的维度:

你看到了什么?那么线性拟合不是很好。维度正朝着二的方向发展。要进行诊断,让我们看一下生成的灰度图像,以及您拥有的阈值(即 0.9):

enter image description here

enter image description here

大自然的画面几乎成了一团墨迹。正如图表告诉我们的那样,维度很快就会达到 2 的值。那是因为我们几乎失去了形象。现在阈值为 50?

enter image description here

enter image description here

有了更好的新线性拟合,城市和自然的维度分别为 1.6 和 1.8。请记住,城市图片实际上有很多结构,特别是在有纹理的墙壁上。

将来好的阈值会更接近灰度图像的平均值,这样您的图像就不会变成一团墨水!

迈克尔·巴恩斯利 (Michael F. Barnsley) 的“无处不在的分形”是一本很好的教科书。

关于Python分形盒计数——分形维数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44793221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com