About Lesson
In this Python OpenCV lesson we want to learn about Canny Edge Detection in Python OpenCV, so it is a popular edge detection algorithm. It was developed by John F. Canny in 1986. It is a multi-stage algorithm it means that there are 5 stage involving for canny edge detection. 1 Noise Reduction, 2 Calculate the gradients , 3 Non-maximum Suppression , 4 Double threshold on all the detected edges , 5 Analyzes all the edges and their connection. now there is a function in opencv cv2.Canny() and you can do all the process using this function.
This is the complete code for this lesson.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import cv2 import matplotlib.pyplot as plt image = cv2.imread("lena.tif") edges = cv2.Canny(image, 100,200) #threshold of min and max value #image_convert = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) plt.subplot(121),plt.imshow(image) plt.title('Original Image') plt.xticks([]), plt.yticks([]) plt.subplot(122) plt.imshow(edges,cmap = 'gray') plt.title('Edge Image') plt.xticks([]), plt.yticks([]) plt.show() cv2.waitKey(0) cv2.destroyAllWindows() |
This is our Canny function, we have our input image, minValue and maxValue.
1 |
edges = cv2.Canny(image, 100,200) |
Run the complete code and this is the result.