计算3D渐变 [英] Calculating gradient in 3D

查看:337
本文介绍了计算3D渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在3d空间中具有以下点集,D希望在各处计算梯度,即返回一个矢量场.

I have the following set of points in 3d-space and D'd like to calculate the gradient everywhere, i.e. have a vector field returned.

points = []
for i in np.linspace(-20,20,100):   
    for j in np.linspace(-20,20,100):
        points.append([i,j,i**2+j**2])
points = np.array(points)

这是一个椭圆形的抛物面.

It's an elliptic paraboloid.

使用np.gradient(points) http://docs.scipy.org/doc/numpy/reference /generation/numpy.gradient.html

我既没有获得正确的值,也没有得到期望的尺寸.谁能给我提示吗?

I neither get the correct values nor the dimension I would expect. Can anyone give me a hint?

推荐答案

您正在将索引和点"中的值混合在一起,因此渐变会给您错误的结果.这是用numpy构造点并计算梯度的更好方法:

You are mixing together the indices and the values in 'points', so the gradient is giving you wrong results. Here is a better way to construct the points with numpy and calculate the gradient:

x, y = np.mgrid[-20:20:100j, -20:20:100j]
z = x**2 + y**2
grad = np.gradient(z)

得到的梯度是一个具有两个数组的元组,一个用于第一个方向的梯度,另一个用于第二个方向的梯度.请注意,此梯度未考虑点之间的距离(即,增量x和增量y),因此要获得导数,需要除以它:

The resulting gradient is a tuple with two arrays, one for the gradient on the first direction, another for the gradient on the second direction. Note that this gradient doesn't take into account the separation between points (ie, delta x and delta y), so to get the derivative you need to divide by it:

deriv = grad/(40./100.)

如果您想像以前一样重建点",只需要做:

If you want to reconstruct your 'points' as before, you just need to do:

points = np.array([x.ravel(), y.ravel(), z.ravel()]).T

您可能还对numpy的 diff 感兴趣函数,它给出了沿给定轴的离散差异.

You may also be interested in numpy's diff function, that gives the discrete difference along a given axis.

这篇关于计算3D渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆