0
0
Computer Visionml~20 mins

Image gradients (Sobel, Laplacian) in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Image gradients (Sobel, Laplacian)
Problem:You want to detect edges in images using gradient filters. Currently, you apply Sobel filters but the edges are noisy and unclear.
Current Metrics:Edge clarity score: 60/100 (subjective visual score), Noise level: high
Issue:The Sobel filter output has too much noise and weak edge definition, making it hard to identify clear edges.
Your Task
Improve edge detection by reducing noise and enhancing edge clarity using image gradients. Target a clearer edge map with less noise.
Use only Sobel and Laplacian filters for gradient calculation.
Do not use advanced denoising or deep learning methods.
Work with grayscale images only.
Hint 1
Hint 2
Hint 3
Solution
Computer Vision
import cv2
import numpy as np
import matplotlib.pyplot as plt

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

# Apply Sobel filter in x and y directions
sobel_x = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
sobel_y = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)

# Calculate gradient magnitude
grad_mag = np.sqrt(sobel_x**2 + sobel_y**2)

# Normalize gradient magnitude to 0-255
grad_mag = np.uint8(255 * grad_mag / np.max(grad_mag))

# Apply threshold to reduce noise
_, thresh_grad = cv2.threshold(grad_mag, 50, 255, cv2.THRESH_BINARY)

# Apply Laplacian filter
laplacian = cv2.Laplacian(img, cv2.CV_64F)
laplacian = np.uint8(np.absolute(laplacian))

# Display results
plt.figure(figsize=(12,6))
plt.subplot(1,3,1)
plt.title('Original Grayscale')
plt.imshow(img, cmap='gray')
plt.axis('off')

plt.subplot(1,3,2)
plt.title('Sobel Gradient Magnitude + Threshold')
plt.imshow(thresh_grad, cmap='gray')
plt.axis('off')

plt.subplot(1,3,3)
plt.title('Laplacian Filter Output')
plt.imshow(laplacian, cmap='gray')
plt.axis('off')

plt.show()
Combined Sobel x and y gradients to get gradient magnitude for better edge strength representation.
Normalized gradient magnitude to 0-255 scale for visualization.
Applied thresholding to gradient magnitude to reduce noise and keep strong edges only.
Added Laplacian filter output to compare edge detection results.
Results Interpretation

Before: Sobel filter alone produced noisy edges with unclear boundaries.

After: Combining Sobel x and y gradients and thresholding reduced noise and enhanced edge clarity. Laplacian filter provided a complementary edge map highlighting different edge features.

Using gradient magnitude from Sobel filters and thresholding helps reduce noise and improve edge detection. Laplacian filter offers an alternative edge detection method that can complement Sobel results.
Bonus Experiment
Try applying Gaussian blur before gradient calculation to see if smoothing the image further reduces noise in edge detection.
💡 Hint
Use cv2.GaussianBlur with a small kernel size (e.g., 3x3) before applying Sobel and Laplacian filters.