import matplotlib.pyplot as plt
from diptorch.filters import gaussian_filter
from diptorch.utils import astronaut, imshow
diptorch
Digital image processing in PyTorch
Install
pip install diptorch
Hello, World!
# Zero-th order Gaussian filter (smoothing)
= astronaut()
img = gaussian_filter(img, sigma=2.5)
img_filtered
imshow(img, img_filtered) plt.show()
# First-order Gaussian filter
= astronaut()
img = gaussian_filter(img, sigma=2.5, order=1)
img_filtered
imshow(img, img_filtered) plt.show()
# Second-order Gaussian filter on the height dimension (y-axis)
= astronaut()
img = gaussian_filter(img, sigma=2.5, order=[2, 0])
img_filtered
imshow(img, img_filtered) plt.show()
Hessian matrix
from diptorch.filters import hessian, hessian_eigenvalues
from einops import rearrange
# Hessian matrix of an image (all second-order partial derivatives)
= astronaut()
img = hessian(img, sigma=2.5, as_matrix=True)
H = rearrange(H, "B C1 C2 H W -> B (C1 H) (C2 W)").squeeze()
H
="gray")
plt.imshow(H, cmap"off")
plt.axis( plt.show()
# Eigenvalues of the Hessian matrix of an image
# sorted by the magnitude of the eigenvalues
= astronaut()
img = hessian_eigenvalues(img, sigma=2.5)
eig = imshow(img, *eig.split(1, 1))
_, axs 1].set(title="Smallest magnitude\neigenvalue")
axs[2].set(title="Largest magnitude\neigenvalue")
axs[ plt.show()