【发布时间】:2019-07-13 15:39:00
【问题描述】:
我正在尝试创建一个矩阵,其元素是到我定义的曲线的距离(代码如下):
我想对这张图片进行一些操作,它给了我一个矩阵,其中包含该点与螺旋上任何点之间的所有最小欧几里得距离。
我试过像这样使用scipy的ndimage.distance_transform_edt:
import scipy.ndimage as ndi
transformed = ndi.distance_transform_edt(spiral())
但是输出并没有给我我想要的东西!
有人知道如何生成这个矩阵吗?
下面的螺旋生成代码:
import numpy as np
import matplotlib.pyplot as plt
def pol2cart(rho, phi):
# https://stackoverflow.com/questions/20924085/python-conversion-between-coordinates
x = rho * np.cos(phi)
y = rho * np.sin(phi)
return(y, x)
def spiral():
C = 0.15
phi = np.linspace(6, 540, 1000)
rho = (1 - C * np.log(phi - 5))
# Now convert back to x, y coordinates
y, x = pol2cart(rho, np.deg2rad(phi))
# Center the spiral so we can see it better.
x -= x.min()
y -= y.min()
x += 1
y += 1.5
m = np.zeros((100, 100))
for i in range(len(x)):
try:
# Include some scaling factor to increase the size of the curve
m[int(x[i]*30), int(y[i]*30)] = 1
except IndexError:
continue
return m
plt.imshow(spiral())
【问题讨论】: