In this case study, we will explore how to implement Canny edge detection with OpenCV. By following the step-by-step process, we will be able to create a Canny edge detection workflow and analyze the results.
The Ikomia API simplifies the development of Computer Vision workflows and provides easy experimentation with different parameters to achieve optimal results.
You can easily create a Canny edge detection workflow with just a few lines of code. All you need to do is install the API in a virtual environment.
How to install a virtual environment
You can also charge directly the open-source notebook we have prepared.
For a step-by-step detailed approach jump to this section.
On top, you see the original input image. And underneath, you see the output image, commonly known as the edge map.
Learn more about Canny edge detection in OpenCV.
Edge detection is an essential image processing technique commonly employed in various computer vision applications, including data extraction, image segmentation, feature extraction, and pattern recognition.
This technique helps reduce the amount of noise and irrelevant details in an image while retaining its structural information. As a result, edge detection plays a crucial role in enhancing the accuracy and performance of computer vision algorithms.
Whether you're working on object detection, image recognition, or other Computer Vision tasks, edge detection is a critical step in your workflow.
Canny edge detection is widely regarded as one of the most popular and effective methods for edge detection in Computer Vision. It employs a multi-stage algorithm to detect a wide range of edges in images. This algorithm can be broken down into four basic steps:
The first step in Canny edge detection involves preprocessing the image to remove any noise or blur. This is usually achieved by applying a Gaussian filter to smooth the image.
The second step calculates the image's gradient by applying Sobel filters in the x and y directions to get the derivatives Gx and Gy. These derivatives provide the gradient intensity matrix and gradient angles, which help identify regions with significant intensity changes, indicating edges.
In the third step, the algorithm thins out the edges by suppressing any non-maximum gradient pixels. Each pixel's gradient magnitude is compared to its neighboring pixels in the gradient direction. If the gradient magnitude is not the highest in that direction, the pixel is suppressed, and the edge is thinned resulting in a one-pixel-wide edge representation.
The final step determines which edges to keep and discard. It applies two thresholds: a high threshold and a low threshold. Edge pixels with a gradient magnitude above the high threshold are kept, while those with a gradient magnitude below the low threshold are discarded. Edge pixels with a gradient magnitude between the high and low thresholds are preserved only if they are connected to a strong edge pixel.
Now let's dive into the detailed steps of creating Canny edge detection workflows using the Ikomia API.
We initialize a workflow instance. The “wf” object can then be used to add tasks to the workflow instance, configure their parameters, and run them on input data.
We can use the ik namespace to search for algorithms.
The OpenCV Canny edge detection takes several parameters that control its behavior:
Adjusting these parameters can significantly impact the performance of the Canny edge detection. For example, increasing the threshold values will result in detecting fewer edges, while increasing the aperture size will result in detecting more detailed edges.
Here, we use the auto-completion to find the names of the available parameters.
The run_on() function allows you to apply your workflow to the image. For this example, we get our image from an URL:
Finally, you can display our image results using the display function:
Default parameters:
By increasing the threshold1 value, fewer edges are detected
By increasing the aperture size, more detailed edges can be detected.
To learn more about the API, refer to the documentation. You may also check out the list of state-of-the-art algorithms on Ikomia HUB and try out Ikomia STUDIO, which offers a friendly UI with the same features as the API.