Edge detection is an image processing technique in computer vision that involves identifying the outline of objects in an image.
Canny edge detection is one of the best techniques for edge detection. It’s designed to detect clean, well-defined edges while reducing noise and avoiding false edges. It uses a double thresholding method to detect edges in an image: a high and a low threshold.
img = cv2.Canny('photo.jpg')
img_edges = cv2.Canny(img, 100, 200)
// 100 is the low threshold
// 200 is the high threshold
The thresholds decide what becomes an edge and what doesn't. To make this decision, we use gradient values:
- If a gradient value is above the high threshold, it’s considered a strong edge and added to the edge map. (strong edge)
- If it’s below the low threshold, it’s ignored. (non edge)
- If it is between the high and low threshold, it is only kept if it is connected to a strong edge. (potential edge)
What are gradient values?
Gradient values are not the raw image values. They are computed numbers derived from the raw image by checking how much the pixel intensity changes in an image. We use gradient values because the raw image values don’t directly tell us where the edges are.
A simple example to illustrate changes in pixel intensity: if two neighboring pixels have very different values (e.g. 50 and 200 and the gradient value is 150), there’s a big change — it might be an edge. But if two neighboring pixels have similar values (e.g. 50 and 52 and the gradient value is 2), there’s little change & very little possibility of being an edge.
After the gradient values are computed, they are then compared against the thresholds to decide what qualifies as a strong edge, a potential edge or a non edge.
How do we know values in between thresholds are connected to a strong edge?
By using a method called edge tracking by hysteresis which decides edges that are connected and should be kept VS discarded. This algorithm works by looking at the 8 neighbors (directly adjacent pixels - top, bottom, left, right, and diagonals) of each potential edge pixel. Any pixel directly or indirectly connected to a strong edge is included in the final result.
How edge tracking works:
50 80 110 90
70 250 190 120
60 180 150 70
40 60 80 50
Imagine this gradient map above:
After applying thresholds (low = 100, high = 200), the strong edge pixels ( > 200) are immediately kept as edges. Here, only the pixel 250 is marked as a strong edge.
The potential edge pixels (100–200) are 110, 190, 120, 180 and 150. Now that we have a pool of potential edges, we perform edge tracking to decide what gets to stay & what is discarded. The algorithm checks if any of the potential edges are directly or indirectly connected to the strong edge (250).
For example:
- 190 is a neighbor of 250, it is directly connected to a strong edge so it's kept.
- 150 is a neighbor of 190, it is indirectly connected to a strong edge so it’s also kept.
Weak edge pixels (< 100) like 80, 90 and the rest are completely ignored, as they are considered noise.They will not be a part of the final image.
Top comments (0)