KEMBAR78
Lab 3 Edge Detection Feature Detection | PDF | Python (Programming Language) | Computer Graphics
0% found this document useful (0 votes)
24 views3 pages

Lab 3 Edge Detection Feature Detection

Edge Detection

Uploaded by

Syed Luqman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

Lab 3 Edge Detection Feature Detection

Edge Detection

Uploaded by

Syed Luqman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab 3

Edge and Feature Detection


This lab focuses on understanding and implementing edge and feature detection algorithms
using Python and OpenCV.

Objectives
1. Understand and apply the Sobel and Canny edge detection methods.
2. Implement feature detection using the Harris Corner algorithm and SIFT.
3. Gain practical experience with these techniques on real-world image data.

Materials Needed
1. Computer with Python and OpenCV installed.
2. Sample images for processing.

Exercise 1: Implementing Sobel Edge Detector


Steps:

1. Type 'python' to enter the Python interpreter.


2. Import necessary libraries: 'import cv2' and 'import numpy as np'.
3. Load an image using:
o ‘image_path=r'filename'’. Replace ‘filename’ with the path to your image file.
o 'img = cv2.imread(filename)'.
o Check if the image is loaded correctly with 'if img is None: ‘print("Error:
Image not found"); exit(1)'.
4. Convert the image to grayscale to simplify edge detection:
`gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) `
5. Apply the Sobel operator both horizontally and vertically:
`sobel_horizontal = cv2.Sobel(gray_image, cv2.CV_64F, 1, 0, ksize=5)`
`sobel_vertical = cv2.Sobel(gray_image, cv2.CV_64F, 0, 1, ksize=5)`
6. Display the results:
`cv2.imshow('Horizontal Edges', sobel_horizontal)`
`cv2.imshow('Vertical Edges', sobel_vertical)`
`cv2.waitKey(0)`
`cv2.destroyAllWindows()`
Real-world application: Lane detection in autonomous driving systems.
Exercise 2: Using the Canny Edge Detector
1. Type 'python' to enter the Python interpreter.
2. Import necessary libraries: 'import cv2' and 'import numpy as np'.
3. Load an image using:
o ‘image_path=r'filename'’. Replace ‘filename’ with the path to your image file.
o 'img = cv2.imread(filename)'.
o Check if the image is loaded correctly with 'if img is None: ‘print("Error:
Image not found"); exit(1)'.
4. Convert the image to grayscale to simplify edge detection:
`gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) `
5. Apply Gaussian blurring to reduce noise:
`blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)`
6. Detect edges using the Canny algorithm:
`edges = cv2.Canny(blurred_image, 50, 150)`
7. Display the edges:
`cv2.imshow('Canny Edges', edges)`
`cv2.waitKey(0)`
`cv2.destroyAllWindows()`
Real-world application: Object and scene boundary detection in security
systems.

Exercise 3: Feature Detection with Harris Corner


1. Type 'python' to enter the Python interpreter.
2. Import necessary libraries: 'import cv2' and 'import numpy as np'.
3. Load an image using:
o ‘image_path=r'filename'’. Replace ‘filename’ with the path to your image file.
o 'img = cv2.imread(filename)'.
o Check if the image is loaded correctly with 'if img is None: ‘print("Error:
Image not found"); exit(1)'.
4. Convert the image to grayscale to simplify edge detection:
`gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) `
5. Apply the Harris Corner detection algorithm:
`gray = np.float32(gray_image)`
`harris_corners = cv2.cornerHarris(gray, 2, 3, 0.04)`
`img[harris_corners > 0.01 * harris_corners.max()] = [0, 0, 255]`
6. Display the corners:
`cv2.imshow('Harris Corners', img)`
`cv2.waitKey(0)`
`cv2.destroyAllWindows()`
Real-world application: Feature tracking in video editing and augmented
reality.
Exercise 4: Feature Detection with SIFT:
1. Type 'python' to enter the Python interpreter.
2. Import necessary libraries: 'import cv2' and 'import numpy as np'.
3. Load an image using:
o ‘image_path=r'filename'’. Replace ‘filename’ with the path to your image file.
o 'img = cv2.imread(filename)'.
o Check if the image is loaded correctly with 'if img is None: ‘print("Error:
Image not found"); exit(1)'.
4. Convert the image to grayscale to simplify edge detection:
`gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) `
5. Initialize SIFT detector:
sift = cv2.SIFT_create()

6. Detect keypoints and compute descriptors:


keypoints, descriptors = sift.detectAndCompute(gray_image, None)
7. Draw keypoints on the image:
8. sift_image = cv2.drawKeypoints(img, keypoints, None,
flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
9. # Show the image
cv2.imshow('SIFT Keypoints', sift_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Summary
Discuss the strengths and limitations of each method and potential improvements or
alternatives.

You might also like