In this OpenCV Python article we are going to talk about Canny Edge Detection, but first of all let’s talk about OpenCV and Installation of OpenCV.
What is OpenCV?
OpenCV or Open Source Computer Vision Library is an open-source computer vision and machine learning software library. It’s used for building computer vision applications and to accelerate the development of different vision-related tasks.
How to Install OpenCV for Python?
You can install OpenCV for Python using pip command like this.
1 |
pip install opencv-python |
What Is Canny Edge Detection ?
OpenCV also offers a very handy function called Canny Edge Detection, the inventor of this algorithm was, John F. Canny), which is very popular not only because of its effectiveness, but also the simplicity of its implementation in an OpenCV program.
These are the steps for Canny Edge Detection
1: Noise Reduction : Since edge detection is susceptible to noise in the image, first step is to remove the noise in the image with a 5×5 Gaussian filter.
2: Calculate the gradients
3: Non-maximum Suppression: After getting gradient magnitude and direction, a full scan of image is done to remove any unwanted pixels
which may not constitute the edge. for this, at every pixel, pixel is checked if it is a local maximum in its neighborhood in the direction of gradient.
4: Double threshold on all the detected edges
5: Analyzes all the edges and their connection
So now this is the complete code for OpenCV Python Canny Edge Detection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import cv2 # Read image from in grayscale mode (0) image = cv2.imread("lena.tif", 0) # Apply the Canny edge detection algorithm to the image # The function parameters are: image, threshold1, threshold2 # threshold1 and threshold2 are the lower and upper thresholds edges = cv2.Canny(image, 100, 200) # Save resulte edge-detected image to a new file cv2.imwrite("canny.jpg", edges) # Display edge detected image in a window cv2.imshow("canny", cv2.imread("canny.jpg")) # Display original grayscale image in a window cv2.imshow("Original Image", image) # Wait indefinitely until a key is pressed cv2.waitKey() # Destroy all the windows created by OpenCV cv2.destroyAllWindows() |
So in all five steps OpenCV puts all the above in single function, cv2.Canny(). We will see how to use it. First argument is our input image. Second and third arguments are our minVal and maxVal respectively. Third argument is aperture_size. It is the size of Sobel kernel used for find image gradients. By default it is 3. Last argument is L2gradient which specifies the equation for finding gradient magnitude. If it is True, it uses the equation mentioned above which is more accurate, otherwise it uses this function: Edge_Gradient(G)=|Gx|+|Gy|. By default, it is False.
This line of code is for reading our image make sure to have an image in your working directory.
1 |
image = cv2.imread("lena.tif", 0) |
Also this is our Canny function, we have our input image, minValue and maxValue.
1 |
cv2.Canny(image, 100, 200) |
Run the complete code and this will be the result
FAQs:
How to use Canny edge detection in Python?
For using Canny edge detection in Python with OpenCV, follow these steps:
- Install OpenCV: Ensure you have OpenCV installed. You can install it using pip:
1 |
pip install opencv-python |
- Read the Image: Load the image you want to process.
1 2 3 |
import cv2 image = cv2.imread('your_image.jpg', 0) |
- Apply Canny Edge Detection: Use cv2.Canny() function
1 |
edges = cv2.Canny(image, 100, 200) |
- Display the Result: Show the original and edge-detected images.
1 2 3 4 |
cv2.imshow('Original Image', image) cv2.imshow('Edges', edges) cv2.waitKey(0) cv2.destroyAllWindows() |
What is Canny edge detection OpenCV?
Canny edge detection is a multi step algorithm, and it is used to detect different edges in images. It was developed by John F. Canny in 1986 and is known for its efficiency and accuracy. OpenCV provides a function cv2.Canny() for applying this algorithm. The steps involved in Canny edge detection are:
- Noise Reduction: Applying a Gaussian filter to smooth the image and reduce noise.
- Gradient Calculation: Calculating intensity gradients of the image.
- Non-Maximum Suppression: Removing spurious response to edge detection.
- Double Thresholding: Determining potential edges.
- Edge Tracking by Hysteresis: Finalizing edge detection by suppressing all the other edges that are weak and not connected to strong edges.
Subscribe and Get Free Video Courses & Articles in your Email