0
0
Computer Visionml~5 mins

Edge detection (Canny) in Computer Vision

Choose your learning style9 modes available
Introduction

Edge detection helps find the outlines of objects in images. The Canny method is a popular way to do this clearly and accurately.

To find shapes or boundaries in photos for object recognition.
To prepare images for further analysis like counting items or measuring sizes.
To detect road lanes in self-driving car cameras.
To highlight important features in medical images like X-rays.
To improve image quality by focusing on edges for artistic effects.
Syntax
Computer Vision
edges = cv2.Canny(image, threshold1, threshold2)

image: The input image, usually in grayscale.

threshold1 and threshold2: Values to decide which edges are strong or weak.

Examples
Detect edges with lower threshold 50 and upper threshold 150 on a grayscale image.
Computer Vision
edges = cv2.Canny(gray_image, 50, 150)
Use higher thresholds to detect only stronger edges.
Computer Vision
edges = cv2.Canny(image, 100, 200)
Sample Model

This code creates a black image with a white square. Then it finds edges using Canny. Finally, it counts and prints how many edge pixels were found.

Computer Vision
import cv2
import numpy as np

# Create a simple black image with a white square
image = np.zeros((100, 100), dtype=np.uint8)
cv2.rectangle(image, (30, 30), (70, 70), 255, -1)

# Apply Canny edge detection
edges = cv2.Canny(image, 50, 150)

# Count how many edge pixels were detected
edge_count = np.sum(edges > 0)

print(f"Number of edge pixels detected: {edge_count}")
OutputSuccess
Important Notes

Use grayscale images for Canny; color images should be converted first.

Choosing thresholds affects how many edges you get: lower values detect more edges but can include noise.

Canny uses steps like smoothing, gradient calculation, and edge tracking for good results.

Summary

Canny edge detection finds clear edges by using two threshold values.

It works best on grayscale images and helps highlight object boundaries.

Adjust thresholds to balance between detecting real edges and ignoring noise.