Lecture 4 AI Summary
Lecture 4 AI Summary
Disadvantages
import cv2
"""
Parameters:
Returns:
"""
return filtered_image
# Display results
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.title("Original Image")
plt.imshow(image, cmap='gray')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(filtered_image, cmap='gray')
plt.axis('off')
plt.show()
import cv2
import numpy as np
"""
Parameters:
Returns:
"""
return blurred_image
sigma = 1.5
# Display results
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.title("Original Image")
plt.imshow(image, cmap='gray')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(blurred_image, cmap='gray')
plt.axis('off')
plt.show()
import cv2
import numpy as np
"""
Parameters:
Returns:
"""
return filtered_image
# Display results
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.title("Original Image")
plt.imshow(image, cmap='gray')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.axis('off')
plt.show()
Non-Maximum Suppression
For each pixel, compare the gradient magnitude with its neighbors along the gradient
direction:
Hysteresis Thresholding
If a weak edge is connected to a strong edge, retain it; otherwise, discard it.
import cv2
import numpy as np
"""
Apply Canny edge detection to an image.
Parameters:
Returns:
"""
return edges
sigma = 1.0
low_threshold = 50
high_threshold = 150
# Display results
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.title("Original Image")
plt.imshow(image, cmap='gray')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(edges, cmap='gray')
plt.axis('off')
plt.show()
Comparison of Edge Detection Methods
Edge detection methods are crucial for identifying object boundaries and significant
transitions in images. Here’s a detailed comparison of the Canny Edge Detector with other
common edge detection methods:
1. Sobel Operator
The Sobel operator detects edges using convolution with derivative filters.
Advantages:
o Sensitive to noise.
Applications:
2. Prewitt Operator
Similar to the Sobel operator but uses simpler kernels for gradient calculation.
Advantages:
Disadvantages:
Applications:
LoG combines Gaussian smoothing with the Laplacian operator to detect edges.
Advantages:
Applications:
o LoG does not use gradient direction or hysteresis thresholding, making it less
effective in producing thin, connected edges compared to Canny.
Advantages:
o Computationally inexpensive.
Disadvantages:
Applications:
o Robert’s operator is simpler but much less accurate, especially in noisy images
and when edges are complex.
Python code to compare the outputs of different edge-detection methods side by side. It
processes an example grayscale image using the Sobel, Prewitt, LoG, Robert's Cross, and
Canny methods.
import cv2
import numpy as np
def sobel_edge_detection(image):
return cv2.convertScaleAbs(sobel)
sobel_edges = sobel_edge_detection(image)
def prewitt_edge_detection(image):
return cv2.convertScaleAbs(prewitt)
prewitt_edges = prewitt_edge_detection(image)
def log_edge_detection(image):
log_edges = log_edge_detection(image)
def roberts_edge_detection(image):
return cv2.convertScaleAbs(roberts)
roberts_edges = roberts_edge_detection(image)
plt.figure(figsize=(15, 10))
plt.subplot(2, 3, 1)
plt.title("Original Image")
plt.imshow(image, cmap='gray')
plt.axis('off')
plt.subplot(2, 3, 2)
plt.axis('off')
plt.subplot(2, 3, 3)
plt.imshow(prewitt_edges, cmap='gray')
plt.axis('off')
plt.subplot(2, 3, 4)
plt.imshow(log_edges, cmap='gray')
plt.axis('off')
plt.subplot(2, 3, 5)
plt.imshow(roberts_edges, cmap='gray')
plt.axis('off')
plt.subplot(2, 3, 6)
plt.imshow(canny_edges, cmap='gray')
plt.axis('off')
plt.tight_layout()
plt.show()