0% found this document useful (0 votes)
2 views

Lecture 3 AI Summary

The document discusses various image processing techniques, particularly focusing on edge detection methods such as Sobel, Laplacian of Gaussian (LoG), and Canny edge detection. It highlights the importance of Gaussian smoothing in reducing noise and improving edge detection accuracy, along with practical applications in fields like medical imaging, autonomous vehicles, and object recognition. Additionally, it covers advanced techniques and challenges associated with edge detection, including parameter sensitivity and computational costs.

Uploaded by

g23ai2114
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 3 AI Summary

The document discusses various image processing techniques, particularly focusing on edge detection methods such as Sobel, Laplacian of Gaussian (LoG), and Canny edge detection. It highlights the importance of Gaussian smoothing in reducing noise and improving edge detection accuracy, along with practical applications in fields like medical imaging, autonomous vehicles, and object recognition. Additionally, it covers advanced techniques and challenges associated with edge detection, including parameter sensitivity and computational costs.

Uploaded by

g23ai2114
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

import cv2

import numpy as np

import matplotlib.pyplot as plt

# Load the image in grayscale

image = cv2.imread('example_image.jpg', cv2.IMREAD_GRAYSCALE)

# Define Sobel filters

sobel_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])

sobel_y = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])

# Apply filters using convolution

gradient_x = cv2.filter2D(image, -1, sobel_x)

gradient_y = cv2.filter2D(image, -1, sobel_y)

# Compute gradient magnitude

gradient_magnitude = np.sqrt(np.square(gradient_x) + np.square(gradient_y))

gradient_magnitude = np.uint8(gradient_magnitude / np.max(gradient_magnitude) * 255)

# Display the result

plt.figure(figsize=(10, 10))

plt.subplot(1, 3, 1), plt.title('Original Image'), plt.imshow(image, cmap='gray')

plt.subplot(1, 3, 2), plt.title('Gradient Magnitude'), plt.imshow(gradient_magnitude,


cmap='gray')

plt.show()
import cv2

import numpy as np

import matplotlib.pyplot as plt

# Load the image in grayscale

image = cv2.imread('example_image.jpg', cv2.IMREAD_GRAYSCALE)

# Apply Sobel filters

sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3) # Horizontal edges

sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3) # Vertical edges


# Compute gradient magnitude

gradient_magnitude = np.sqrt(sobel_x**2 + sobel_y**2)

gradient_magnitude = np.uint8(gradient_magnitude / gradient_magnitude.max() * 255)

# Plot results

plt.figure(figsize=(15, 5))

plt.subplot(1, 3, 1), plt.title('Original Image'), plt.imshow(image, cmap='gray')

plt.subplot(1, 3, 2), plt.title('Sobel X'), plt.imshow(sobel_x, cmap='gray')

plt.subplot(1, 3, 3), plt.title('Gradient Magnitude'), plt.imshow(gradient_magnitude,


cmap='gray')

plt.show()

Challenges: Noise

 Noise introduces spurious edges.

 Solutions include:

o Gaussian Smoothing: Pre-process the image to reduce noise.

o Thresholding: Eliminate weak edges.

Practical Use Cases

 Detecting object boundaries in medical images.

 Lane detection in self-driving cars.

 Extracting text from document images.


import cv2

import numpy as np

import matplotlib.pyplot as plt

# Load the image in grayscale

image = cv2.imread('example_image.jpg', cv2.IMREAD_GRAYSCALE)

# Apply Gaussian blur

sigma = 1.0 # Standard deviation

blurred = cv2.GaussianBlur(image, (5, 5), sigma)

# Compute Laplacian of the blurred image

laplacian = cv2.Laplacian(blurred, cv2.CV_64F)

# Detect zero-crossings (approximation for edges)

edges = np.where(np.diff(np.sign(laplacian), axis=0), 255, 0).astype(np.uint8)

# Plot results

plt.figure(figsize=(15, 5))

plt.subplot(1, 3, 1), plt.title('Original Image'), plt.imshow(image, cmap='gray')

plt.subplot(1, 3, 2), plt.title('Blurred Image'), plt.imshow(blurred, cmap='gray')

plt.subplot(1, 3, 3), plt.title('LoG Edges'), plt.imshow(edges, cmap='gray')

plt.show()

Challenges with LoG

 Susceptible to noise: Increasing Gaussian smoothing (σ) can help reduce spurious
edges.

 No edge orientation: Unlike Sobel or Prewitt, LoG does not indicate the direction of
edges.
Comparison: DoG vs. LoG

Feature DoG LoG

Purpose Gradient-based edge detection Zero-crossing-based edge detection

Noise Handling Moderate Better with larger σ

Complexity Slightly simpler More computationally intensive


import cv2

import numpy as np

import matplotlib.pyplot as plt


# Load the image in grayscale

image = cv2.imread('example_image.jpg', cv2.IMREAD_GRAYSCALE)

# Apply Gaussian blur

sigma = 1.0

blurred = cv2.GaussianBlur(image, (5, 5), sigma)

# Compute Laplacian of the blurred image

laplacian = cv2.Laplacian(blurred, cv2.CV_64F)

# Normalize the result

laplacian = np.uint8(np.abs(laplacian) / np.max(np.abs(laplacian)) * 255)

# Plot results

plt.figure(figsize=(15, 5))

plt.subplot(1, 3, 1), plt.title('Original Image'), plt.imshow(image, cmap='gray')

plt.subplot(1, 3, 2), plt.title('Blurred Image'), plt.imshow(blurred, cmap='gray')

plt.subplot(1, 3, 3), plt.title('Laplacian of Gaussian'), plt.imshow(laplacian, cmap='gray')

plt.show()

Advantages of LoG

1. Detects edges while reducing noise.

2. Identifies edges through zero-crossings, providing a reliable method for edge


localization.
5. Challenges with Laplace and LoG Filters

 Noise Sensitivity: The Laplace operator amplifies noise.

o Mitigation: Use Gaussian smoothing with a higher σ

 No Orientation Information: The Laplace filter detects edges without direction.


import cv2

import numpy as np

import matplotlib.pyplot as plt

# Load the image

image = cv2.imread('example_image.jpg', cv2.IMREAD_GRAYSCALE)

# Apply Gaussian filter

sigma = 1.0

gaussian_blur = cv2.GaussianBlur(image, (5, 5), sigma)

# Plot results

plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1), plt.title('Original Image'), plt.imshow(image, cmap='gray')

plt.subplot(1, 2, 2), plt.title('Gaussian Blurred'), plt.imshow(gaussian_blur, cmap='gray')

plt.show()
import cv2

import numpy as np

import matplotlib.pyplot as plt

# Load the image

image = cv2.imread('example_image.jpg', cv2.IMREAD_GRAYSCALE)

# Step 1: Apply Gaussian smoothing


sigma = 1.5

blurred = cv2.GaussianBlur(image, (5, 5), sigma)

# Step 2: Apply Sobel filter for edge detection

sobel_x = cv2.Sobel(blurred, cv2.CV_64F, 1, 0, ksize=3) # Horizontal edges

sobel_y = cv2.Sobel(blurred, cv2.CV_64F, 0, 1, ksize=3) # Vertical edges

gradient_magnitude = np.sqrt(sobel_x**2 + sobel_y**2)

gradient_magnitude = np.uint8(gradient_magnitude / gradient_magnitude.max() * 255)

# Display results

plt.figure(figsize=(15, 5))

plt.subplot(1, 3, 1), plt.title('Original Image'), plt.imshow(image, cmap='gray')

plt.subplot(1, 3, 2), plt.title('Blurred Image'), plt.imshow(blurred, cmap='gray')

plt.subplot(1, 3, 3), plt.title('Gradient Magnitude'), plt.imshow(gradient_magnitude,


cmap='gray')

plt.show()

3. Scale-Space Representation

Gaussian filters are used to create a multi-scale representation of an image:

 At each scale, images are smoothed with increasingly larger σ

 This helps analyze image features at different levels of detail.

Application: Blob Detection

 Used in SIFT (Scale-Invariant Feature Transform) to detect keypoints across multiple


scales.

4. Enhancing Image Features

Gaussian filters can selectively emphasize or suppress certain features:

 Low σ: Preserve fine details.

 High σ: Focus on larger structures.


Pipeline Example: Texture Analysis

1. Smooth the image with multiple Gaussian filters of varying σ

2. Subtract the filtered images to isolate specific textures or patterns.

Real-World Applications of Gaussian Filters

Gaussian filters are widely used in real-world scenarios across various domains due to their
simplicity and effectiveness. Let’s explore some key applications:

1. Image Preprocessing

Gaussian filters are a common first step in processing images for further analysis.

Applications:

 Medical Imaging: Reduce noise in X-rays, MRIs, and CT scans to improve clarity and
enhance diagnostic accuracy.
 Object Detection: Smooth images before detecting objects to minimize false
positives caused by noise.

Example:

In self-driving cars, Gaussian filters preprocess camera feeds to ensure reliable detection of
road signs and obstacles.

2. Edge Detection

Gaussian filters help refine the results of edge detection algorithms like Sobel, Prewitt, and
Canny by reducing noise.

Applications:

 Robotics: Identify object boundaries for robotic manipulation.

 Traffic Monitoring: Detect lane markers and vehicle edges in video streams.

Canny Edge Detection with Gaussian Filter

Canny uses a Gaussian filter internally for smoothing before detecting edges.

import cv2

import matplotlib.pyplot as plt

# Load the image

image = cv2.imread('example_image.jpg', cv2.IMREAD_GRAYSCALE)

# Apply Canny Edge Detection

edges = cv2.Canny(image, 50, 150) # Threshold values

# Display the result

plt.title('Canny Edge Detection'), plt.imshow(edges, cmap='gray')

plt.show()
3. Scale-Invariant Feature Transform (SIFT)

Gaussian filters are used in SIFT to detect keypoints in an image across multiple scales. These
keypoints are essential for tasks like:

 Image Matching: Matching features between two images.

 3D Reconstruction: Mapping object surfaces from multiple images.

4. Blurring for Artistic Effects

Gaussian filters create aesthetic effects by softening images.

Applications:

 Photography: Achieve a bokeh effect by blurring the background while keeping the
subject sharp.

 Film Industry: Smooth images for stylistic or dreamlike sequences.

import cv2

import matplotlib.pyplot as plt

# Load the image

image = cv2.imread('example_image.jpg')

# Apply Gaussian blur

blurred = cv2.GaussianBlur(image, (15, 15), 5) # Higher kernel size for stronger blur

# Display results

plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1), plt.title('Original Image'), plt.imshow(cv2.cvtColor(image,


cv2.COLOR_BGR2RGB))

plt.subplot(1, 2, 2), plt.title('Blurred Image'), plt.imshow(cv2.cvtColor(blurred,


cv2.COLOR_BGR2RGB))

plt.show()
5. Denoising in Signal Processing

Gaussian filters are not limited to images; they’re also used in signal processing to smooth
data.

Applications:

 Stock Market Analysis: Remove noise from price trends for better forecasting.

 Audio Processing: Reduce background noise in speech and music.

6. Object Recognition

Gaussian filters preprocess images for object recognition tasks. Smoothing enhances the
stability of algorithms against minor distortions.

Applications:

 Facial Recognition Systems: Improve detection accuracy by normalizing face images.

 Retail Automation: Detect and count products on shelves.

7. Focus Stacking

Gaussian filters are used in multi-focus image blending. In focus stacking:

1. Multiple images with varying focus levels are taken.

2. Gaussian filters blend them into a single image, preserving sharp details.

Applications:

 Microscopy: Combine images at different depths for better visualization.

 Macro Photography: Create fully focused images of small objects.

8. Image Compression

Gaussian filters are used in lossy compression algorithms to reduce high-frequency details
before encoding.

Applications:

 JPEG Compression: Gaussian smoothing reduces file size without significant quality
loss.
import cv2

import matplotlib.pyplot as plt

# Load the image in grayscale

image = cv2.imread('example_image.jpg', cv2.IMREAD_GRAYSCALE)


# Apply Canny Edge Detection

edges = cv2.Canny(image, threshold1=50, threshold2=150)

# Display the results

plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1), plt.title('Original Image'), plt.imshow(image, cmap='gray')

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

plt.show()

Advantages of Canny Edge Detection

1. Noise Resistance: Incorporates Gaussian smoothing to handle noisy images.

2. Accurate Edge Localization: Non-maximum suppression ensures thin and precise


edges.

3. Hysteresis Thresholding: Distinguishes between significant and spurious edges.


Challenges

1. Parameter Sensitivity: Requires careful tuning of thresholds.

2. Computational Cost: The multi-stage process can be slower for high-resolution


images.

Real-World Applications

1. Autonomous Vehicles: Detect lane boundaries and road edges.

2. Medical Imaging: Outline structures like blood vessels in X-rays and CT scans.

3. Object Recognition: Preprocess images for shape analysis.

Advanced Edge Detection Techniques and Real-World Case Studies

Edge detection has evolved to address complex challenges in image processing. Advanced
methods build on basic techniques like Sobel, LoG, and Canny to improve robustness, handle
real-world noise, and enable better accuracy in complex environments.

Advanced Edge Detection Techniques

1. Multi-Scale Edge Detection

 Concept: Detect edges at different levels of detail by applying filters with varying
kernel sizes or σ values (scale-space representation).

 Example: Use a Gaussian pyramid, where the image is smoothed and subsampled at
multiple scales.

Applications:

 Detect fine and coarse edges in satellite images or medical scans.

 Identify features for object detection across varying resolutions.


import cv2

import numpy as np

# Load the image

image = cv2.imread(‘example_image.jpg’, cv2.IMREAD_GRAYSCALE)

# Compute median and derive adaptive thresholds

median = np.median(image)

lower_thresh = max(0, 0.5 * median)

upper_thresh = min(255, 1.5 * median)

# Apply Canny Edge Detection with adaptive thresholds

edges = cv2.Canny(image, lower_thresh, upper_thresh)

# Show the results

cv2.imshow(‘Edges’, edges)

cv2.waitKey(0)

cv2.destroyAllWindows()

Morphological Edge Detection

 Technique: Use morphological operations like dilation, erosion, and gradient to


highlight edges.

 Steps:

1. Erode the image to shrink bright regions.

2. Subtract the eroded image from the original to get edges.

Applications:

 Detect boundaries in binary images (e.g., printed text or barcodes).

 Highlight edges in industrial inspections.


 4.ep Learning for Edge Detection
1. Modern edge detection leverages convolutional neural networks (CNNs):

 Train deep learning models to classify pixels as edge or non-edge.

 Example: Holistically-Nested Edge Detection (HED).

Advantages:

 Handles complex textures and noise.

 Learns edge patterns directly from data.

Applications:

 Real-time edge detection in autonomous systems.

 Semantic edge detection in medical images (e.g., segmenting tumors).

Case Studies of Canny Edge Detection

1.tonomous Driving: Lane Detection

2. Problem: Identify lane markers under varying conditions (rain, shadows,


curves).

 Solution:

1. Apply Gaussian smoothing to suppress noise.

2. Use Canny edge detection to extract lane boundaries.

3. Post-process edges (e.g., Hough Transform) to detect straight or curved lanes.

Outcome: Improves safety by accurately identifying lanes even under challenging conditions.

2. Medical Imaging: Vessel Segmentation

 Problem: Detect fine structures like blood vessels in noisy scans (e.g., angiograms).

 Solution:

o Preprocess with Gaussian filters to reduce noise.

o Use Canny edge detection with adaptive thresholds to outline vessels.


Outcome: Enhanced visualization of vascular structures for diagnosis.

3. Surveillance: Object Boundary Detection

 Problem: Detect moving objects (e.g., intruders) in video feeds.

 Solution:

o Use edge detection to highlight object contours.

o Combine with background subtraction to focus on dynamic objects.

Outcome: Increased efficiency in monitoring large areas.

4. Industrial Quality Control

 Problem: Identify defects in products (e.g., cracks in materials, misaligned parts).

 Solution:

o Use Canny or LoG filters to detect edges of irregularities.

o Compare detected edges to predefined templates.

Outcome: Automated, accurate inspection reduces human error.

Exercise 1: Comparing Edge Detection Methods

1. Objective: Compare Sobel, Canny, and Laplacian edge detection on the same image.

2. Steps:
o Load a grayscale image.

o Apply Sobel, Canny, and Laplacian filters.

o Visualize and compare the results.

import cv2

import matplotlib.pyplot as plt

# Load the image in grayscale

image = cv2.imread('example_image.jpg', cv2.IMREAD_GRAYSCALE)

# Apply Sobel filters

sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)

sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)

sobel_combined = cv2.magnitude(sobel_x, sobel_y)

# Apply Laplacian filter

laplacian = cv2.Laplacian(image, cv2.CV_64F)

# Apply Canny Edge Detection

canny_edges = cv2.Canny(image, 50, 150)

# Plot results

plt.figure(figsize=(15, 10))

plt.subplot(2, 2, 1), plt.title('Original Image'), plt.imshow(image, cmap='gray')

plt.subplot(2, 2, 2), plt.title('Sobel Combined'), plt.imshow(sobel_combined, cmap='gray')

plt.subplot(2, 2, 3), plt.title('Laplacian'), plt.imshow(laplacian, cmap='gray')

plt.subplot(2, 2, 4), plt.title('Canny Edges'), plt.imshow(canny_edges, cmap='gray')

plt.show()
import numpy as np

# Compute median and adaptive thresholds

median = np.median(image)

lower_thresh = max(0, 0.5 * median)

upper_thresh = min(255, 1.5 * median)

# Apply adaptive Canny Edge Detection

adaptive_canny = cv2.Canny(image, lower_thresh, upper_thresh)

# Display results

plt.title('Adaptive Canny Edge Detection'), plt.imshow(adaptive_canny, cmap='gray')

plt.show()

Exercise 3: Edge Detection in Noisy Images

1. Objective: Explore the effect of noise on edge detection and mitigate it using
Gaussian filters.

2. Steps:

o Add random noise to an image.


o Apply Canny edge detection without smoothing.

o Use Gaussian smoothing and then apply Canny again.

o Compare results.

import numpy as np

# Add random noise to the image

noise = np.random.normal(0, 25, image.shape).astype(np.uint8)

noisy_image = cv2.add(image, noise)

# Apply Canny without smoothing

noisy_edges = cv2.Canny(noisy_image, 50, 150)

# Smooth the noisy image using Gaussian filter

smoothed_image = cv2.GaussianBlur(noisy_image, (5, 5), 1)

# Apply Canny after smoothing

smoothed_edges = cv2.Canny(smoothed_image, 50, 150)

# Plot results

plt.figure(figsize=(15, 10))

plt.subplot(2, 2, 1), plt.title('Noisy Image'), plt.imshow(noisy_image, cmap='gray')

plt.subplot(2, 2, 2), plt.title('Edges Without Smoothing'), plt.imshow(noisy_edges,


cmap='gray')

plt.subplot(2, 2, 3), plt.title('Smoothed Image'), plt.imshow(smoothed_image, cmap='gray')

plt.subplot(2, 2, 4), plt.title('Edges After Smoothing'), plt.imshow(smoothed_edges,


cmap='gray')

plt.show()
Exercise 4: Object Boundary Detection

1. Objective: Detect boundaries of objects in an image.

2. Steps:

o Use Gaussian smoothing to reduce noise.

o Apply Canny edge detection.

o Mask the edges onto the original image to highlight boundaries.

# Smooth the image

smoothed = cv2.GaussianBlur(image, (5, 5), 1)

# Apply Canny Edge Detection

edges = cv2.Canny(smoothed, 50, 150)

# Overlay edges on the original image

edges_colored = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)

highlighted = cv2.addWeighted(cv2.cvtColor(image, cv2.COLOR_GRAY2BGR), 0.8,


edges_colored, 0.2, 0)

# Display results

plt.figure(figsize=(10, 5))

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

plt.subplot(1, 2, 2), plt.title('Highlighted Boundaries'), plt.imshow(cv2.cvtColor(highlighted,


cv2.COLOR_BGR2RGB))

plt.show()

Exercise 5: Real-World Case Study

1. Objective: Simulate lane detection in a road image.

2. Steps:

o Crop the image to focus on the road.

o Apply Gaussian smoothing and Canny edge detection.


o Use Hough Transform to detect lanes.

# Crop the region of interest

height, width = image.shape

roi = image[int(height / 2):, :]

# Smooth and detect edges

smoothed_roi = cv2.GaussianBlur(roi, (5, 5), 1)

edges_roi = cv2.Canny(smoothed_roi, 50, 150)

# Use Hough Transform to detect lines

lines = cv2.HoughLinesP(edges_roi, 1, np.pi / 180, 100, minLineLength=50, maxLineGap=10)

# Draw lines on the original image

lane_image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)

if lines is not None:

for line in lines:

x1, y1, x2, y2 = line[0]

cv2.line(lane_image, (x1, y1 + height // 2), (x2, y2 + height // 2), (0, 255, 0), 2)

# Display results

plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1), plt.title('Region of Interest Edges'), plt.imshow(edges_roi, cmap='gray')

plt.subplot(1, 2, 2), plt.title('Detected Lanes'), plt.imshow(cv2.cvtColor(lane_image,


cv2.COLOR_BGR2RGB))

plt.show()

You might also like