Lecture 6 AI Summary
Lecture 6 AI Summary
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.imshow(downsampled_image)
plt.axis("off")
plt.show()
Repeat for the rest of the matrix.
import numpy as np
image = np.array([
])
# Downsampling factor
N=2
print("Original Image:")
print(image)
print("\nDownsampled Image:")
print(downsampled_image)
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()
import cv2
# Downsampling
# 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.
import cv2
gaussian_pyramid.append(image)
plt.figure(figsize=(12, 8))
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).
1. Image Compression:
o Store only the coarse level and reconstruct lower levels when needed.
2. Object Detection:
3. Image Blending:
import cv2
image = cv2.pyrDown(image)
gaussian_pyramid.append(image)
laplacian_pyramid = []
laplacian_pyramid.append(laplacian)
laplacian_pyramid.append(gaussian_pyramid[-1])
plt.figure(figsize=(12, 8))
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
# Load the two images (ensure they are the same size)
gaussian_A = [image_A]
gaussian_B = [image_B]
gaussian_mask = [mask]
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)
laplacian_A = []
laplacian_B = []
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])
laplacian_blend = []
for i in range(len(laplacian_A)):
laplacian_blend.append(blended_level)
blended_image = laplacian_blend[-1]
blended_image = cv2.pyrUp(blended_image)
plt.imshow(blended_image, cmap='gray')
plt.axis('off')
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
2. Image Editing: Useful in tasks like face swapping or combining different objects from
different images.
import numpy as np
a = np.cos(theta)
b = np.sin(theta)
x0 = a * rho
y0 = b * rho
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.
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:
1. Edge Detection:
2. Boundary Linking:
4. Post-Processing:
import cv2
import numpy as np
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:
2. Medical Imaging
3. Industrial Inspection
4. Scene Understanding
o Boundaries are used to group parts of a scene into coherent regions for
further analysis.
5. Shape Recognition
o Many objects are defined by their shape. Boundary detection helps extract
the contours for matching and classification.
1. Noise in Images
2. Weak Boundaries
o Some boundaries may have low contrast, making them hard to detect.
3. Discontinuous Edges
4. Complex Shapes
o Natural objects often have irregular, non-linear shapes that are challenging to
model.
import numpy as np
ax.imshow(image, cmap='gray')
ax.legend()
ax.axis('off')
plt.show()
Code Example: Line Fitting Using Least Squares
import numpy as np
x = np.array([0, 1, 2, 3, 4, 5])
fitted_y = m * x + c
plt.figure(figsize=(8, 6))
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.grid()
plt.show()
RANSAC (Random Sample Consensus) is commonly used to fit a line in the presence of noise
or outliers.
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
ransac.fit(x, y)
line_y = ransac.predict(line_x)
plt.figure(figsize=(8, 6))
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.grid()
plt.show()
1. Road Detection:
2. Structural Analysis:
o Analyzing cracks or linear patterns in buildings or materials.
3. Shape Modeling: