Hisham Ragheb
Hisham Ragheb

Reputation: 144

Can I use numpy gradient function with images

I have been trying to test the numpy.gradient function recently. However, it's behavior is little bit strange for me. I have created an array with random variables and then applied the numpy.gradient over it, but the values seems crazy and irrelevant. But when using numpy.diff the values are correct.

So, after viewing the documentation of numpy.gradient, I see that it uses distance=1 over the desired dimension.

This is what I mean:

import numpy as np;

a= np.array([10, 15, 13, 24, 15, 36, 17, 28, 39]);
np.gradient(a)
"""
Got this: array([  5. ,   1.5,   4.5,   1. ,   6. ,   1. ,  -4. ,  11. ,  11. ])
"""
np.diff(a)
"""
Got this:  array([  5,  -2,  11,  -9,  21, -19,  11,  11])
"""

I don't understand how the values in first result came. If the default distance is supposed to be 1, then I should have got the same results as numpy.diff.

Could anyone explain what distance means here. Is it relative to the array index or to the value in the array? If it depends on the value, then does that mean that numpy.gradient could not be used with images since values of neighbor pixels have no fixed value differences?

Upvotes: 4

Views: 11148

Answers (3)

Robert McLean MD PhD
Robert McLean MD PhD

Reputation: 456

For the boundary points, np.gradient uses the formulas

f'(x) = [f(x+h)-f(x)]/h for the left endpoint, and f'(x) = [f(x)-f(x-h)]/h for the right endpoint.

For the interior points, it uses the formula

f'(x) = [f(x+h)-f(x-h)]/2h

The second approach is more accurate - O(h^2) vs O(h). Thus at the second data point, np.gradient estimates the derivative as (13-10)/2 = 1.5.

I made a video explaining the mathematics: https://www.youtube.com/watch?v=NvP7iZhXqJQ

Upvotes: 1

Walid Bousseta
Walid Bousseta

Reputation: 1469

# load image
img = np.array([[21.0, 20.0, 22.0, 24.0, 18.0, 11.0, 23.0],
                [21.0, 20.0, 22.0, 24.0, 18.0, 11.0, 23.0],
                [21.0, 20.0, 22.0, 24.0, 18.0, 11.0, 23.0],
                [21.0, 20.0, 22.0, 99.0, 18.0, 11.0, 23.0],
                [21.0, 20.0, 22.0, 24.0, 18.0, 11.0, 23.0],
                [21.0, 20.0, 22.0, 24.0, 18.0, 11.0, 23.0],
                [21.0, 20.0, 22.0, 24.0, 18.0, 11.0, 23.0]])
print "image =", img

# compute gradient of image
gx, gy = np.gradient(img)
print "gx =", gx
print "gy =", gy

# plotting
plt.close("all")
plt.figure()
plt.suptitle("Image, and it gradient along each axis")
ax = plt.subplot("131")
ax.axis("off")
ax.imshow(img)
ax.set_title("image")

ax = plt.subplot("132")
ax.axis("off")
ax.imshow(gx)
ax.set_title("gx")

ax = plt.subplot("133")
ax.axis("off")
ax.imshow(gy)
ax.set_title("gy")
plt.show()

Upvotes: 7

Philipp Braun
Philipp Braun

Reputation: 1573

Central differences in the interior and first differences at the boundaries.

15 - 10
13 - 10 / 2
24 - 15 / 2
...
39 - 28

Upvotes: 0

Related Questions