Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating gradient map of 2D array

Tags:

python

numpy

I have a 2D array that stores values of a property of each point as its element: f(x,y) = f[x][y]. Now I want to find the gradient of this array. I looked into np.gradient but it just gives two arrays as return, first with derivative in x direction and second in y direction.

I want to learn how can I use this or any other way to create a gradient map that shows the change in gradient of the 2D array.
varray is the 2D array I want to create gradient map of. Following is the only things I can think of right now. I know there should be clever way to use x gradientand y gradient generated by np.gradient() but I cannot think of it. lx and ly are x and y dimension of the 2D array.

vgrad = np.gradient(varray)
xgrad = vgrad[0]
x, y = range(0, lx), range(0,ly)
xi, yi = np.meshgrid(x, y)
rbf = scipy.interpolate.Rbf(xi, yi, xgrad)
plt.imshow(v, vmin = np.amin(xgrad), vmax=np.amax(xgrad))
plt.colorbar()
plt.show()  

I want to get basically the second image from the first image. The second image is described as σ = \alpha*grad(varray).

Using magnitude of gradient as suggested by @Mad Physicist below.

vgrad = np.gradient(varray)
fulgrad = np.sqrt(vgrad[0]**2 + vgrad[1]**2)
plt.imshow(fulgrad,cmap=plt.get_cmap('hot'), vmin = np.amin(fulgrad),vmax = np.amax(fulgrad))  
plt.colorbar()
plt.show()  

the image i get : enter image description here

I am interpreting this wrong from basic understanding of the equation?

So here is my images. On left: image of the initial 2D map. On right: Image of the gradient map. @Mad Physicist do you think they are similar to above with only difference of colors?

enter image description here enter image description here

like image 460
kada Avatar asked Nov 30 '15 17:11

kada


People also ask

How do you find the gradient using NumPy?

We can use the numpy. gradient() function to find the gradient of an N-dimensional array. For gradient approximation, the function uses either first or second-order accurate one-sided differences at the boundaries and second-order accurate central differences in the interior (or non-boundary) points.

What does NumPy gradient do?

gradient. Return the gradient of an N-dimensional array. The gradient is computed using second order accurate central differences in the interior points and either first or second order accurate one-sides (forward or backwards) differences at the boundaries.

How do you find the gradient of a vector in Python?

We will use numdifftools to find Gradient of a function. Examples: Input : x^4+x+1 Output :Gradient of x^4+x+1 at x=1 is 4.99 Input :(1-x)^2+(y-x^2)^2 Output :Gradient of (1-x^2)+(y-x^2)^2 at (1, 2) is [-4.


1 Answers

If you are looking for the magnitude of the gradient, you can just do

mag = np.sqrt(vgrad[0]**2 + vgrad[1]**2)

Then plot mag instead of xgrad as above. If, you want to plot the gradient as a vector map or stream plot, do something like

plt.streamplot(xi, yi, vgrad[0], vgrad[1])

You may also be interested in the visual representation of the slope that can be obtained from just plotting the original surface in 3D:

fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(xi, yi, varray)
plt.show()

See What is the equivalent of Matlab's surf(x,y,z,c) in matplotlib? and http://matplotlib.org/examples/mplot3d/surface3d_demo.html

like image 99
Mad Physicist Avatar answered Oct 08 '22 01:10

Mad Physicist