0
0
Computer Visionml~20 mins

Edge detection (Canny) in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Edge detection (Canny)
Problem:Detect edges in images using the Canny edge detection algorithm.
Current Metrics:Edges detected are noisy with many false edges and some important edges missing.
Issue:The current edge detection produces too many false positives and misses some real edges, indicating poor parameter tuning.
Your Task
Improve the quality of edge detection by tuning the Canny algorithm parameters to reduce noise and better capture true edges.
Must use the Canny edge detection method.
Can only adjust the threshold parameters and Gaussian blur kernel size.
Must keep the code runnable with OpenCV in Python.
Hint 1
Hint 2
Hint 3
Solution
Computer Vision
import cv2
import numpy as np
import matplotlib.pyplot as plt

# Load image in grayscale
image = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE)

# Apply Gaussian blur to reduce noise
blurred = cv2.GaussianBlur(image, (5, 5), 1.4)

# Apply Canny edge detection with tuned thresholds
edges = cv2.Canny(blurred, threshold1=50, threshold2=150)

# Show original and edge images
plt.figure(figsize=(10,5))
plt.subplot(1,2,1)
plt.title('Original Grayscale Image')
plt.axis('off')
plt.imshow(image, cmap='gray')

plt.subplot(1,2,2)
plt.title('Edges Detected (Canny)')
plt.axis('off')
plt.imshow(edges, cmap='gray')

plt.show()
Added Gaussian blur with kernel size (5,5) and sigma 1.4 to reduce noise before edge detection.
Adjusted Canny thresholds to 50 (lower) and 150 (upper) for better edge selection.
Results Interpretation

Before: Noisy edges with many false detections and missing important edges.
After: Cleaner edges with reduced noise and better true edge detection.

Tuning parameters like thresholds and applying noise reduction before edge detection improves the quality of detected edges in images.
Bonus Experiment
Try using different Gaussian blur kernel sizes and observe how it affects edge detection quality.
💡 Hint
Increase the kernel size to smooth more noise but watch out for losing fine edges.