Perform the following steps:
- Import the packages:
import cv2import numpy as npimport matplotlib.pyplot as plt
- Read the image as grayscale:
image = cv2.imread('../data/Lena.png', 0)
- Compute the gradient approximations using the Sobel operator:
dx = cv2.Sobel(image, cv2.CV_32F, 1, 0)dy = cv2.Sobel(image, cv2.CV_32F, 0, 1)
- Visualize the results:
plt.figure(figsize=(8,3))plt.subplot(131)plt.axis('off')plt.title('image')plt.imshow(image, cmap='gray')plt.subplot(132)plt.axis('off')plt.imshow(dx, cmap='gray')plt.title(r'$\frac{dI}{dx}$')plt.subplot(133)plt.axis('off')plt.title(r'$\frac{dI}{dy}$')plt.imshow(dy, cmap='gray')plt.tight_layout()plt.show()