Reputation: 450
In order to understand visually the involved vector, scalar fields of an image operation which involves calculating, gradient, divergence, laplacian etc, I am trying to plot them also on the image involved. I started with gradient as below, but
MWE:
test_img = cv2.imread('images/ring.png', cv2.IMREAD_GRAYSCALE)
r, c = test_img.shape
gd = 15
test_slice = test_img[::gd,::gd] # every 15th point
X, Y = np.mgrid[0:r:gd, 0:c:gd]
dY, dX = np.gradient(test_slice)
plt.figure(figsize=(10,10))
plt.quiver(X, Y, dX, dY, color='y')
plt.imshow(test_img, cmap='gray')
plt.show()
Output:
Desired style : (vector field with image underneath instead):
Sample Image used: link
Note: I initially used a png, then then alpha area was giving a nan, so now I have the jpg uploaded.
Upvotes: 3
Views: 5204
Reputation: 5247
The short answer is: np.mgrid()
is giving you a transposed (i.e. rotated) matrix, see this article for example.
In the following, I'm using matplotlib.image
to load the image (which I first converted back into a .png).
I flatten the image (i.e. remove the alpha channel) and use imshow
with a fitting colormap ("Greys_r"). The important part however is in Y, X = np.mgrid[0:r:gd, 0:c:gd]
, which you probably would have spotted yourself if your image weren't square to begin with.
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
fname="/path/to/ring.png"
im = mpimg.imread(fname)
flat_image=(im[:,:,0]+im[:,:,1]+im[:,:,2])/3.
r, c = np.shape(flat_image)
gd = 4
test_slice = flat_image[::gd,::gd] # sampling
fig,ax=plt.subplots(1,1)
the_image = ax.imshow(
flat_image,
zorder=0,alpha=1.0,
cmap="Greys_r",
origin="upper",
interpolation="hermite",
)
plt.colorbar(the_image)
Y, X = np.mgrid[0:r:gd, 0:c:gd]
dY, dX = np.gradient(test_slice)
ax.quiver(X, Y, dX, dY, color='r')
plt.show()
The resulting image (with the colormap viridis
, however) seems to do what you want.
Upvotes: 3