Edge detection helps find the outlines of objects in images. The Canny method is a popular way to do this clearly and accurately.
Edge detection (Canny) in 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.
edges = cv2.Canny(gray_image, 50, 150)
edges = cv2.Canny(image, 100, 200)
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.
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}")
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.
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.