- OpenCV 3 Computer Vision with Python Cookbook
- Alexey Spizhevoy Aleksandr Rybnikov
- 94字
- 2021-08-27 19:47:45
How to do it...
Perform the following steps:
- Import the packages:
import math
import cv2
import numpy as np
import matplotlib.pyplot as plt
- Read the test image:
image = cv2.imread('../data/Lena.png')
- Create an 11x11 sharpening kernel:
KSIZE = 11
ALPHA = 2
kernel = cv2.getGaussianKernel(KSIZE, 0)
kernel = -ALPHA * kernel @ kernel.T
kernel[KSIZE//2, KSIZE//2] += 1 + ALPHA
- Filter the image using the kernel we just created:
filtered = cv2.filter2D(image, -1, kernel)
- Visualize the results:
plt.figure(figsize=(8,4))
plt.subplot(121)
plt.axis('off')
plt.title('image')
plt.imshow(image[:, :, [2, 1, 0]])
plt.subplot(122)
plt.axis('off')
plt.title('filtered')
plt.imshow(filtered[:, :, [2, 1, 0]])
plt.tight_layout(True)
plt.show()