[go: up one dir, main page]

0% found this document useful (0 votes)
17 views34 pages

Lecture 6 AI Summary

The document provides a comprehensive overview of image processing techniques including downsampling, Gaussian and Laplacian pyramid construction, image blending, edge detection, and boundary detection. It includes practical code examples using OpenCV and Matplotlib to demonstrate these techniques, along with discussions on their applications and challenges. Key observations highlight the importance of these methods in various fields such as image compression, object detection, and medical imaging.

Uploaded by

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

Lecture 6 AI Summary

The document provides a comprehensive overview of image processing techniques including downsampling, Gaussian and Laplacian pyramid construction, image blending, edge detection, and boundary detection. It includes practical code examples using OpenCV and Matplotlib to demonstrate these techniques, along with discussions on their applications and challenges. Key observations highlight the importance of these methods in various fields such as image compression, object detection, and medical imaging.

Uploaded by

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

import cv2

import matplotlib.pyplot as plt

# Load the image

image = cv2.imread('sample_image.jpg', cv2.IMREAD_COLOR)

image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convert to RGB for matplotlib

# Naive downsampling: Select every Nth pixel


downsample_factor = 8

downsampled_image = image[::downsample_factor, ::downsample_factor, :]

# Display the original and downsampled images

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

plt.subplot(1, 2, 1)

plt.title("Original Image")

plt.imshow(image)

plt.axis("off")

plt.subplot(1, 2, 2)

plt.title("Naively Downsampled Image")

plt.imshow(downsampled_image)

plt.axis("off")

plt.show()
Repeat for the rest of the matrix.

Practical Example: Code

import numpy as np

import matplotlib.pyplot as plt

# Original image (example matrix)

image = np.array([

[10, 20, 30, 40],

[50, 60, 70, 80],

[90, 100, 110, 120],


[130, 140, 150, 160]

])

# Downsampling factor

N=2

# Downsample the image

downsampled_image = image[::N, ::N]

# Print the results

print("Original Image:")

print(image)

print("\nDownsampled Image:")

print(downsampled_image)

# Visualization (optional for large images)

plt.figure(figsize=(8, 4))

plt.subplot(1, 2, 1)

plt.title("Original Image")

plt.imshow(image, cmap='gray')

plt.colorbar()

plt.subplot(1, 2, 2)

plt.title(f"Downsampled (N={N})")

plt.imshow(downsampled_image, cmap='gray')

plt.colorbar()
plt.show()

Code Example: Anti-Aliasing with Gaussian Blur

import cv2

import matplotlib.pyplot as plt


# Load the image

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

# Apply Gaussian Blur

blurred_image = cv2.GaussianBlur(image, (5, 5), sigmaX=1)

# Downsampling

downsampled_image = blurred_image[::2, ::2]

# Display results

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

plt.subplot(1, 3, 1)

plt.title("Original Image")

plt.imshow(image, cmap='gray')

plt.axis('off')

plt.subplot(1, 3, 2)

plt.title("Blurred Image")

plt.imshow(blurred_image, cmap='gray')

plt.axis('off')

plt.subplot(1, 3, 3)

plt.title("Downsampled Image")

plt.imshow(downsampled_image, cmap='gray')

plt.axis('off')

plt.show()
Repeat for subsequent levels.

Code Example: Gaussian Pyramid Construction

import cv2

import matplotlib.pyplot as plt

# Load the image

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

# Generate Gaussian Pyramid

gaussian_pyramid = [image] # Start with the original image

for i in range(3): # Create 3 levels


image = cv2.pyrDown(image) # Downsample after applying Gaussian blur

gaussian_pyramid.append(image)

# Display the Gaussian Pyramid

plt.figure(figsize=(12, 8))

for i, level in enumerate(gaussian_pyramid):

plt.subplot(1, 4, i+1)

plt.title(f"Level {i}")

plt.imshow(level, cmap='gray')

plt.axis('off')

plt.show()

Key Observations

 Higher levels of the pyramid contain smoothed and smaller versions of the image.

 Details are lost as we move up the pyramid (e.g., fine edges blur out).

Applications of Gaussian Pyramid

1. Image Compression:

o Store only the coarse level and reconstruct lower levels when needed.

2. Object Detection:

o Detect objects at different scales.

3. Image Blending:

o Blend two images seamlessly (e.g., panoramas).


Code Example: Laplacian Pyramid Construction

import cv2

import matplotlib.pyplot as plt

# Load the image

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

# Generate Gaussian Pyramid


gaussian_pyramid = [image]

for i in range(3): # Create 3 levels

image = cv2.pyrDown(image)

gaussian_pyramid.append(image)

# Generate Laplacian Pyramid

laplacian_pyramid = []

for i in range(len(gaussian_pyramid) - 1):

# Upsample the next level

upsampled = cv2.pyrUp(gaussian_pyramid[i + 1], dstsize=gaussian_pyramid[i].shape[::-1])

# Subtract to get Laplacian

laplacian = cv2.subtract(gaussian_pyramid[i], upsampled)

laplacian_pyramid.append(laplacian)

# Add the last Gaussian level as the final Laplacian level

laplacian_pyramid.append(gaussian_pyramid[-1])

# Display Laplacian Pyramid

plt.figure(figsize=(12, 8))

for i, level in enumerate(laplacian_pyramid):

plt.subplot(1, 4, i+1)

plt.title(f"Level {i}")

plt.imshow(level, cmap='gray')

plt.axis('off')

plt.show()
Code Example: Image Blending Using Laplacian Pyramid

import cv2

import numpy as np

import matplotlib.pyplot as plt

# Load the two images (ensure they are the same size)

image_A = cv2.imread('image_A.jpg', cv2.IMREAD_GRAYSCALE)

image_B = cv2.imread('image_B.jpg', cv2.IMREAD_GRAYSCALE)

# Ensure both images are of the same size

image_B = cv2.resize(image_B, (image_A.shape[1], image_A.shape[0]))

# Create a binary mask (for simplicity, here we use a gradient mask)

mask = np.zeros_like(image_A, dtype=np.float32)


mask[:, :image_A.shape[1]//2] = 1 # Left half from image A, right half from image B

# Step 1: Generate Gaussian pyramids for both images and mask

gaussian_A = [image_A]

gaussian_B = [image_B]

gaussian_mask = [mask]

for i in range(3): # Create 3 levels

image_A = cv2.pyrDown(image_A)

image_B = cv2.pyrDown(image_B)

mask = cv2.pyrDown(mask)

gaussian_A.append(image_A)

gaussian_B.append(image_B)

gaussian_mask.append(mask)

# Step 2: Generate Laplacian pyramids for both images

laplacian_A = []

laplacian_B = []

for i in range(len(gaussian_A) - 1):

# Upsample the next level and subtract to get Laplacian

upsampled_A = cv2.pyrUp(gaussian_A[i + 1])

upsampled_B = cv2.pyrUp(gaussian_B[i + 1])

laplacian_A.append(cv2.subtract(gaussian_A[i], upsampled_A))

laplacian_B.append(cv2.subtract(gaussian_B[i], upsampled_B))
# Add the last level of Gaussian pyramid as Laplacian

laplacian_A.append(gaussian_A[-1])

laplacian_B.append(gaussian_B[-1])

# Step 3: Blend the Laplacian pyramids using the mask

laplacian_blend = []

for i in range(len(laplacian_A)):

blended_level = gaussian_mask[i] * laplacian_A[i] + (1 - gaussian_mask[i]) * laplacian_B[i]

laplacian_blend.append(blended_level)

# Step 4: Reconstruct the final blended image

blended_image = laplacian_blend[-1]

for i in range(len(laplacian_blend) - 2, -1, -1):

blended_image = cv2.pyrUp(blended_image)

blended_image = cv2.add(blended_image, laplacian_blend[i])

# Step 5: Display the result

plt.imshow(blended_image, cmap='gray')

plt.axis('off')

plt.title("Blended Image Using Laplacian Pyramid")

plt.show()

Key Observations

1. Masking: The mask determines which parts of the images are used. In the code
above, the left half of the blended image comes from image A, and the right half
from image B.

2. Seamless Transition: The Laplacian Pyramid ensures that the blending is smooth,
without harsh transitions between the images.
Applications of Image Blending

1. Panorama Stitching: When combining multiple images to form a panorama,


Laplacian pyramid blending helps in aligning and merging images seamlessly.

2. Image Editing: Useful in tasks like face swapping or combining different objects from
different images.

3. Image Compression: Blending images at different resolutions allows for more


efficient storage and manipulation.
import cv2

import numpy as np

import matplotlib.pyplot as plt

# Load the image

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

# Step 1: Apply Canny Edge Detection

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


# Step 2: Perform Hough Line Transform

lines = cv2.HoughLines(edges, rho=1, theta=np.pi/180, threshold=100)

# Step 3: Draw the lines on the original image

result_image = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR) # Convert to color for drawing

if lines is not None:

for line in lines:

rho, theta = line[0]

a = np.cos(theta)

b = np.sin(theta)

x0 = a * rho

y0 = b * rho

x1 = int(x0 + 1000 * (-b)) # Extend the line

y1 = int(y0 + 1000 * (a))

x2 = int(x0 - 1000 * (-b))

y2 = int(y0 - 1000 * (a))

cv2.line(result_image, (x1, y1), (x2, y2), (0, 0, 255), 2)

# Step 4: Display the results

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

plt.subplot(1, 2, 1)

plt.title("Edges")

plt.imshow(edges, cmap='gray')

plt.axis('off')

plt.subplot(1, 2, 2)
plt.title("Detected Lines")

plt.imshow(result_image)

plt.axis('off')

plt.show()

Boundary Detection

Boundary detection is a key step in understanding and analyzing objects in an image. While
edges highlight areas of high intensity change, boundaries specifically represent the
outermost contours of objects. The challenge lies in distinguishing true object boundaries
from other edges in the image.

What is Boundary Detection?

1. Edge Detection:

o Detects areas in the image where intensity changes sharply (e.g., Canny or
Sobel filters).
o However, not all edges correspond to object boundaries.

2. Boundary Detection:

o Focuses on identifying object contours or regions that enclose objects.

o Often requires interpreting edges to form continuous boundaries.

Why Boundary Detection is Challenging

1. Not all edges represent boundaries:

o Example: Shadows or texture patterns may create edges unrelated to object


boundaries.

2. Boundaries may be weak or discontinuous:

o Real-world images often have noise or occlusions, making boundaries harder


to identify.

Steps for Boundary Detection

1. Edge Detection:

o Use algorithms like Canny or Sobel to find edges in the image.

2. Boundary Linking:

o Connect edges to form complete boundaries using techniques like:

 Hough Transform for lines.

 Active Contours (Snakes) for curved boundaries.

3. Multi-Scale Edge Detection:

o Analyze edges at multiple resolutions to handle fine and coarse details.

4. Post-Processing:

o Remove weak edges or artifacts to retain only object boundaries.


Code Example: Boundary Detection

import cv2

import numpy as np

import matplotlib.pyplot as plt

# Load the image

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

# Step 1: Apply Canny Edge Detection

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

# Step 2: Apply Contour Detection

contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Step 3: Draw the contours on the original image


contoured_image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) # Convert to color for
visualization

cv2.drawContours(contoured_image, contours, -1, (0, 255, 0), 2) # Draw contours in green

# Display the results

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

plt.subplot(1, 3, 1)

plt.title("Original Image")

plt.imshow(image, cmap='gray')

plt.axis('off')

plt.subplot(1, 3, 2)

plt.title("Edges (Canny)")

plt.imshow(edges, cmap='gray')

plt.axis('off')

plt.subplot(1, 3, 3)

plt.title("Detected Boundaries")

plt.imshow(contoured_image)

plt.axis('off')

plt.show()
Shape Recognition:

o Identify the contours of objects for classification or analysis.

Applications of Boundary Detection

1. Object Detection and Segmentation

o Boundary detection is a precursor to segmentation, where an object is


separated from the background.

o Example: In autonomous vehicles, detecting the boundaries of pedestrians,


road lanes, or vehicles is critical.

2. Medical Imaging

o Boundary detection is used in segmenting organs or abnormalities, such as


tumors, in X-rays, CT scans, or MRIs.
o Example: Identifying the boundary of a heart chamber in an ultrasound
image.

3. Industrial Inspection

o Detect boundaries of products in quality control pipelines to identify defects


or misalignments.

o Example: Checking the shape of a manufactured part against its design


blueprint.

4. Scene Understanding

o Boundaries are used to group parts of a scene into coherent regions for
further analysis.

o Example: Detecting object boundaries in a cluttered environment for robotic


manipulation.

5. Shape Recognition

o Many objects are defined by their shape. Boundary detection helps extract
the contours for matching and classification.

o Example: Detecting handwritten text by analyzing the contours of each


character.

Challenges in Boundary Detection

1. Noise in Images

o Real-world images often contain noise due to poor lighting, shadows, or


sensor errors.

o Solution: Use preprocessing techniques like denoising filters (Gaussian,


bilateral filters) before boundary detection.

2. Weak Boundaries

o Some boundaries may have low contrast, making them hard to detect.

o Solution: Multi-scale edge detection or using advanced deep learning models


for boundary enhancement.

3. Discontinuous Edges

o Boundaries may appear broken or incomplete due to occlusions or missing


data.
o Solution: Use algorithms like active contours (snakes) or graph-based
segmentation to close gaps.

4. Complex Shapes

o Natural objects often have irregular, non-linear shapes that are challenging to
model.

o Solution: Advanced contouring techniques like the Chan-Vese algorithm


handle such cases effectively.

Code Example: Advanced Boundary Detection Using Active Contours

from skimage import io, color, filters

from skimage.segmentation import active_contour

import numpy as np

import matplotlib.pyplot as plt

# Load and preprocess the image

image = io.imread('sample_image.jpg', as_gray=True)


image = filters.gaussian(image, sigma=2) # Smooth the image to remove noise

# Initial contour (a circle around the object)

s = np.linspace(0, 2 * np.pi, 400)

x = 200 + 100 * np.cos(s)

y = 200 + 100 * np.sin(s)

init = np.array([x, y]).T

# Perform active contour

snake = active_contour(image, init, alpha=0.015, beta=10, gamma=0.001)

# Plot the results

fig, ax = plt.subplots(figsize=(7, 7))

ax.imshow(image, cmap='gray')

ax.plot(init[:, 0], init[:, 1], '--r', label='Initial Contour') # Initial contour

ax.plot(snake[:, 0], snake[:, 1], '-b', label='Final Contour') # Final contour

ax.legend()

ax.axis('off')

plt.show()
Code Example: Line Fitting Using Least Squares

import numpy as np

import matplotlib.pyplot as plt

# Sample edge points

x = np.array([0, 1, 2, 3, 4, 5])

y = np.array([1, 2.2, 3.1, 4.3, 5.1, 6.2])

# Step 1: Fit a line using least squares

A = np.vstack([x, np.ones_like(x)]).T # Create matrix with x and intercept term


m, c = np.linalg.lstsq(A, y, rcond=None)[0] # Solve for m (slope) and c (intercept)

# Step 2: Generate the fitted line

fitted_y = m * x + c

# Step 3: Plot the points and the fitted line

plt.figure(figsize=(8, 6))

plt.scatter(x, y, color='red', label='Data Points') # Original points

plt.plot(x, fitted_y, color='blue', label=f'Fitted Line: y = {m:.2f}x + {c:.2f}')

plt.title('Line Fitting Using Least Squares')

plt.xlabel('x')

plt.ylabel('y')

plt.legend()

plt.grid()

plt.show()

Code Example: Robust Line Fitting with RANSAC

RANSAC (Random Sample Consensus) is commonly used to fit a line in the presence of noise
or outliers.

from sklearn.linear_model import RANSACRegressor

from sklearn.linear_model import LinearRegression


# Sample edge points with noise

x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8])

y = np.array([1, 1.9, 3, 4.2, 4.8, 6.1, 5.9, 7.1, 15]) # Last point is an outlier

x = x.reshape(-1, 1) # Reshape for sklearn

# Step 1: Fit a line using RANSAC

ransac = RANSACRegressor(LinearRegression(), residual_threshold=1.0)

ransac.fit(x, y)

# Step 2: Generate predictions

line_x = np.linspace(0, 8, 100).reshape(-1, 1)

line_y = ransac.predict(line_x)

# Step 3: Plot results

plt.figure(figsize=(8, 6))

plt.scatter(x, y, color='red', label='Data Points') # Original points

plt.plot(line_x, line_y, color='blue', label='RANSAC Fitted Line')

plt.title('Robust Line Fitting with RANSAC')

plt.xlabel('x')

plt.ylabel('y')

plt.legend()

plt.grid()

plt.show()

Applications of Line Fitting

1. Road Detection:

o Detecting lane boundaries for autonomous vehicles.

2. Structural Analysis:
o Analyzing cracks or linear patterns in buildings or materials.

3. Shape Modeling:

o Identifying linear features in objects for 3D modeling.

You might also like