Finding edges using the Canny algorithm
Edges are a useful image feature that can be used in many computer vision applications. In this recipe, you will learn how to detect edges in images using the Canny algorithm.
Getting ready
Install the OpenCV 3.x Python API package and the matplotlib
package.
How to do it...
Here are the steps needed to complete this recipe:
- Import the modules:
import cv2 import matplotlib.pyplot as plt
- Load the test image:
image = cv2.imread('../data/Lena.png')
- Detect the edges using the Canny algorithm:
edges = cv2.Canny(image, 200, 100)
- Visualize the results:
plt.figure(figsize=(8,5)) plt.subplot(121) plt.axis('off') plt.title('original') plt.imshow(image[:,:,[2,1,0]]) plt.subplot(122) plt.axis('off') plt.title('edges') plt.imshow(edges, cmap='gray') plt.tight_layout() plt.show()
How it works...
Canny edge detection is a very powerful and popular tool in computer vision. It's named after John F. Canny, who proposed the algorithm in 1986. OpenCV implements the algorithm in the...