Lab 11-12
Lab 11-12
Name
Student ID
Date
Objectives:
1. Understand and implement advanced edge detection techniques, including Sobel, Prewitt,
Kirsch, and Canny operators.
2. Explore the impact of smoothing on edge detection results.
3. Segment images using global thresholding.
Explanation: The Laplacian operator is a second-order differential filter that highlights regions
of rapid intensity change. It is sensitive to noise, so images may need pre-processing.
Example Code:
import cv2
import numpy as np
import matplotlib.pyplot as plt
def apply_laplacian(image):
laplacian = cv2.Laplacian(image, cv2.CV_64F)
return np.abs(laplacian)
plt.imshow(laplacian_image, cmap='gray')
plt.title("Laplacian Edge Detection")
plt.show()
Tasks:
Digital Image Processing
22CS Fall 2024
Lab: 11-12 (Graded) Advanced Edge Detection and Image Segmentation
Task 1: Implement the Laplacian operator with a 3x3 kernel and visualize the result.
Task 2: Apply Gaussian noise to the image and observe the effect on the Laplacian
output. Try different noise intensities
Explanation: The Sobel and Prewitt operators are first-order differential filters that detect edges
by calculating the gradient in x and y directions. Sobel gives more weight to the central pixel,
while Prewitt treats all pixels equally.
Example Code:
plt.subplot(1, 2, 1)
plt.imshow(sobel_x, cmap='gray')
plt.title("Sobel X")
plt.subplot(1, 2, 2)
plt.imshow(sobel_y, cmap='gray')
plt.title("Sobel Y")
plt.show()
Tasks:
Task 4: Implement Prewitt filters and compare the results with Sobel. Visualize the
magnitude of the gradient by combining x and y results.
Explanation: Edge detection can be enhanced by using additional directions such as 45° and -
45°, which help in identifying edges that are not aligned with the primary axes.
Tasks:
M =G x +G y + G45+G−45
Explanation: The Kirsch compass kernel enhances edges in multiple directions (eight in total). It
can be particularly useful for detecting edges in images with significant noise or complex
structures.
Example Code:
kirsch_edge = apply_kirsch(image)
plt.imshow(kirsch_edge, cmap='gray')
plt.title("Kirsch Compass Edge Detection")
plt.show()
Tasks:
Task 7: Apply the Kirsch compass kernel in all eight directions and combine results.
Task 8: Compare the Kirsch edge map with Sobel and Prewitt edge maps.
Explanation: Smoothing the image before edge detection (typically with a Gaussian filter)
reduces noise and results in cleaner edges, though it may slightly blur fine details.
Example Code:
smooth_sobel = sobel_with_smoothing(image)
Digital Image Processing
22CS Fall 2024
Lab: 11-12 (Graded) Advanced Edge Detection and Image Segmentation
plt.imshow(smooth_sobel, cmap='gray')
plt.title("Sobel with Smoothing")
plt.show()
Tasks:
Step-by-Step Task:
1. Calculate Gradients: Use the Sobel filter to compute g x and g y , the gradients in the x
and y directions.
2. Compute Gradient Magnitude and Direction:
o Calculate the magnitude M ( x , y )=√ g x + g y.
2 2
−1 g y
o Calculate the direction α ( x , y )=tan .
gx
3. Apply Thresholding:
o Define a threshold value T . Set any values in M(x,y) below T to zero, and set
value above or equal to T to 255 (or 1 in normalized image).
o This step will help isolate only the most prominent edges.
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Display Results
plt.figure(figsize=(12, 8))
plt.subplot(2, 2, 1)
plt.title("Original Image")
plt.imshow(image, cmap='gray')
plt.subplot(2, 2, 2)
plt.title("Gradient Magnitude")
plt.imshow(magnitude, cmap='gray')
plt.subplot(2, 2, 3)
plt.title("Direction (Degrees)")
plt.imshow(direction, cmap='jet') # Display with
color map for orientation
plt.subplot(2, 2, 4)
plt.title("Thresholded Magnitude")
plt.imshow(magnitude_thresholded, cmap='gray')
Digital Image Processing
22CS Fall 2024
Lab: 11-12 (Graded) Advanced Edge Detection and Image Segmentation
plt.tight_layout()
plt.show()
canny_edges = apply_canny(image)
plt.imshow(canny_edges, cmap='gray')
plt.title("Canny Edge Detection")
plt.show()
Tasks:
Task 12: Experiment with different threshold values and levels of smoothing.
Explanation: Thresholding is a basic image segmentation technique, dividing the image into
foreground and background regions based on intensity levels.
Tasks:
Task 13: Implement the global thresholding discussed in the provided slides.
Additional Tasks:
Digital Image Processing
22CS Fall 2024
Lab: 11-12 (Graded) Advanced Edge Detection and Image Segmentation
Task 14: Modify the source code of box filter to create a function named Edge_sodel that
calculate sobel g x , g y , M (x , y) in the loop. Once loop calculate M (x , y ) for each pixel, apply
threshold outside of the loop. The function should take threshold and input image as input and
return the edge image.
Submission