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

Lecture 7 AI Summary

The document provides an overview of various computer vision techniques including Hough Circle Transform, Generalized Hough Transform, and Harris Corner Detector, along with their implementations in Python using OpenCV. It discusses the advantages and limitations of these techniques, as well as applications of Singular Value Decomposition (SVD) in image compression, noise reduction, and feature extraction. Additionally, it includes code examples and explanations for detecting shapes and corners in images.

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)
13 views

Lecture 7 AI Summary

The document provides an overview of various computer vision techniques including Hough Circle Transform, Generalized Hough Transform, and Harris Corner Detector, along with their implementations in Python using OpenCV. It discusses the advantages and limitations of these techniques, as well as applications of Singular Value Decomposition (SVD) in image compression, noise reduction, and feature extraction. Additionally, it includes code examples and explanations for detecting shapes and corners in images.

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/ 45

import cv2

import numpy as np
# Load image

image = cv2.imread("circle.png", cv2.IMREAD_GRAYSCALE)

# Apply Hough Circle Transform

circles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT, dp=1.2, minDist=30,

param1=50, param2=30, minRadius=5, maxRadius=50)

# Convert the circle coordinates to integers

if circles is not None:

circles = np.uint16(np.around(circles))

for i in circles[0, :]:

# Draw the detected circle

cv2.circle(image, (i[0], i[1]), i[2], (255, 0, 0), 2)

# Draw the center of the circle

cv2.circle(image, (i[0], i[1]), 2, (0, 255, 0), 3)

# Show the image with detected circles

cv2.imshow("Detected Circles", image)

cv2.waitKey(0)

cv2.destroyAllWindows()

Code Explanation (Line by Line)

1. cv2.imread("circle.png", cv2.IMREAD_GRAYSCALE):

o Loads an image in grayscale mode to simplify processing.

2. cv2.HoughCircles(...):

o Detects circles using the Hough Transform.

o dp=1.2: Inverse ratio of the accumulator resolution to the image resolution.

o minDist=30: Minimum distance between detected circles.

o param1=50, param2=30: Thresholds for edge detection.


o minRadius=5, maxRadius=50: Defines the range of circle sizes.

3. np.uint16(np.around(circles)):

o Converts circle values to integers.

4. cv2.circle(image, (i[0], i[1]), i[2], (255, 0, 0), 2):

o Draws the detected circles in blue.

5. cv2.circle(image, (i[0], i[1]), 2, (0, 255, 0), 3):

o Marks the center in green.


import cv2

import numpy as np

# Load the template image (reference shape)

template = cv2.imread("template.png", cv2.IMREAD_GRAYSCALE)

edges_template = cv2.Canny(template, 50, 150)

# Load the target image (where we search for the shape)

image = cv2.imread("image.png", cv2.IMREAD_GRAYSCALE)

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

# Create the Generalized Hough Transform object

ght = cv2.createGeneralizedHoughBallard()

ght.setTemplate(edges_template)
# Detect the shape in the target image

positions, votes = ght.detect(edges_image)

# Draw detected shapes

for position in positions[0]:

x, y, scale, angle = position

cv2.circle(image, (int(x), int(y)), 10, (255, 0, 0), 2)

# Show the result

cv2.imshow("Detected Shape", image)

cv2.waitKey(0)

cv2.destroyAllWindows()

Code Explanation (Line by Line)

1️⃣ Load and preprocess images

 template.png is the reference shape.

 image.png is where we search for the shape.

 cv2.Canny() detects edges in both images.

2️⃣ Create the Generalized Hough Transform object

 cv2.createGeneralizedHoughBallard() initializes Ballard’s GHT.

3️⃣ Detect the shape

 ght.setTemplate(edges_template): Stores the reference shape.

 positions, votes = ght.detect(edges_image): Finds possible locations.

4️⃣ Draw detected shapes

 For each detected position, we draw a circle marking the center.

Advantages of GHT
✅ Can detect any shape, even if it does not have a mathematical equation.
✅ Works even if the object is rotated or partially occluded.
✅ Can detect multiple occurrences of the same shape.

Limitations

❌ Slower than normal Hough Transform.


❌ Requires an edge-based template for training.
❌ May fail if there’s too much noise in the image.

Corner Detection (Harris Corner Detector)

The Harris Corner Detector is used to detect corners in an image.


A corner is a point where intensity changes in two directions.

Why Detect Corners?

Corners are important features in computer vision because they are: ✅ Easier to match
across images.
✅ Used in image alignment, motion tracking, and 3D reconstruction.
✅ Robust to rotation and noise compared to edges.

How to Find Corners?

A corner is a point where shifting a small window in any direction causes a significant
intensity change.
This is different from edges, where changes happen in only one direction.

For example:

 Flat region: No intensity change in any direction.

 Edge: Intensity change in one direction only.

 Corner: Intensity changes in two directions.

Mathematical Explanation

1. Change in Intensity (Error Function)

We define a small window in the image and shift it by (u, v).


import cv2

import numpy as np

from google.colab.patches import cv2_imshow

import matplotlib.pyplot as plt

# Load image in grayscale

image = cv2.imread("/content/chesss.jpg", cv2.IMREAD_GRAYSCALE)

# Convert to float32

image_float = np.float32(image)

# Apply Harris Corner Detector

harris_response = cv2.cornerHarris(image_float, blockSize=2, ksize=3, k=0.04)

# Mark the detected corners


image[harris_response > 0.01 * harris_response.max()] = 255

# Display using Matplotlib

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

plt.title("Harris Corners")

plt.show()

Code Explanation (Line by Line)

1️ cv2.imread("chessboard.jpg", cv2.IMREAD_GRAYSCALE):

 Loads the image in grayscale for processing.

2️ np.float32(image):

 Converts the image to float32, required for Harris Detector.

3️ cv2.cornerHarris(image_float, 2, 3, 0.04):

 Computes the Harris Corner Response.

 blockSize=2: Neighborhood size for gradients.

 ksize=3: Aperture size for Sobel operator.

 k=0.04: Sensitivity factor.

4️ harris_response > 0.01 * harris_response.max():

 Finds pixels where R is above a threshold (corners).

5️ cv2.imshow("Harris Corners", image):

 Displays the detected corners.

Interpreting the Output

🔹 Corners appear as bright white dots.


🔹 Works best on high-contrast regions (e.g., chessboard).
🔹 The detector is rotation-invariant but not scale-invariant (SIFT is better for scale changes).

Advantages of Harris Corner Detector


✅ Fast and computationally efficient.
✅ Works well on rotated images.
✅ Finds good key points for feature matching.

Limitations

❌ Not scale-invariant (fails if image is resized).


❌ Sensitive to noise.
❌ May detect false corners in noisy images.

Summary

✔ Harris Corner Detector finds corners using intensity changes in two directions.
✔ Uses the Harris Matrix and eigenvalues to classify points.
✔ Python implementation uses OpenCV’s cornerHarris() function.
✔ Works well for feature detection in computer vision.
Python Code for SVD

Now, let's implement SVD in Python using NumPy.

import numpy as np

# Define matrix A

A = np.array([[4, 0], [3, -5]])

# Compute SVD

U, Sigma, Vt = np.linalg.svd(A)

# Print results

print("Matrix A:\n", A)

print("\nU (Left Singular Vectors):\n", U)

print("\nSigma (Singular Values):\n", Sigma)

print("\nV Transpose (Right Singular Vectors):\n", Vt)


Code Explanation (Line by Line)

1️⃣ np.array([[4, 0], [3, -5]]):

 Defines matrix A.

2️⃣ np.linalg.svd(A):

 Computes SVD decomposition.

3️⃣ print(U, Sigma, Vt):

Applications of SVD

✔ Image Compression (removing lower singular values reduces file size).


✔ Noise Reduction (low-rank approximation removes noise).
✔ Face Recognition (feature extraction).
✔ Recommender Systems (Netflix, Amazon).
4. Python Code to Compute Eigenvalues and Eigenvectors
Let's implement this in Python using NumPy.

import numpy as np

import matplotlib.pyplot as plt

# Define matrix A

A = np.array([[4, 2], [2, 3]])

# Compute eigenvalues and eigenvectors

eigenvalues, eigenvectors = np.linalg.eig(A)

# Print results

print("Matrix A:\n", A)

print("\nEigenvalues:\n", eigenvalues)

print("\nEigenvectors:\n", eigenvectors)

# Visualizing eigenvectors

origin = [0, 0] # Origin for vectors

plt.quiver(*origin, eigenvectors[0, 0], eigenvectors[1, 0], color='r', angles='xy',


scale_units='xy', scale=1, label="v1")

plt.quiver(*origin, eigenvectors[0, 1], eigenvectors[1, 1], color='b', angles='xy',


scale_units='xy', scale=1, label="v2")

plt.xlim(-3, 3)

plt.ylim(-3, 3)

plt.axhline(y=0, color='k')

plt.axvline(x=0, color='k')

plt.grid()

plt.legend()

plt.title("Eigenvectors of Matrix A")


plt.show()

5. Code Explanation (Line by Line)

1️⃣ np.array([[4, 2], [2, 3]]): Defines the matrix A.


2️⃣ np.linalg.eig(A): Computes eigenvalues and eigenvectors.
3️⃣ Plot eigenvectors:

 plt.quiver(): Draws vectors.

 plt.axhline() & plt.axvline(): Draws X and Y axes.

 plt.legend() & plt.grid(): Adds labels and a grid.

🔹 Red vector → First eigenvector


🔹 Blue vector → Second eigenvector

This visualizes how the matrix transforms vectors.

6. Applications of Eigenvalues in Computer Vision

✔ Feature Detection (Harris Corner Detector uses eigenvalues).


✔ Face Recognition (PCA & Eigenfaces use eigenvalues).
✔ Image Compression (SVD is based on eigenvalues).
✔ Object Tracking (Eigenvalues detect movement direction).

Summary

✔ Quadratic forms help in transformations.


✔ Eigenvalues tell us how a matrix scales vectors.
✔ Eigenvectors show directions that stay unchanged.
✔ Python code visualizes eigenvectors easily.
4. Python Code for Harris Detector (With Step-by-Step Computation)

Now, let’s implement this numerical example in Python.

import numpy as np

import cv2

# Step 1: Define the gradients

Ix = np.array([2, 1, 3]) # x-gradient

Iy = np.array([3, 4, 2]) # y-gradient

# Step 2: Compute Harris Matrix Components


A = np.sum(Ix ** 2)

B = np.sum(Ix * Iy)

C = np.sum(Iy ** 2)

# Step 3: Form Harris Matrix M

M = np.array([[A, B], [B, C]])

# Step 4: Compute determinant and trace

det_M = np.linalg.det(M)

trace_M = np.trace(M)

# Step 5: Compute Harris Response

k = 0.04 # Harris constant

R = det_M - k * (trace_M ** 2)

# Print results

print("Harris Matrix M:\n", M)

print("\nDeterminant of M:", det_M)

print("Trace of M:", trace_M)

print("Harris Response R:", R)

# Check if it is a corner

if R > 0:

print("\nCorner Detected!")

else:

print("\nNot a Corner")
8.
Practical Use: Detecting Corners in an Image

Now, let's apply Harris Corner Detection to a real image using OpenCV.

import cv2

import numpy as np

# Load image

image = cv2.imread("chessboard.jpg")

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply Harris Corner Detector

gray = np.float32(gray)

harris_response = cv2.cornerHarris(gray, blockSize=2, ksize=3, k=0.04)

# Mark corners in red

image[harris_response > 0.01 * harris_response.max()] = [0, 0, 255]

# Show result

cv2.imshow("Harris Corners", image)

cv2.waitKey(0)

cv2.destroyAllWindows()

9. Explanation of Image-Based Code

1️⃣ Load and Convert to Grayscale

 cv2.imread("chessboard.jpg"): Loads the image.

 cv2.cvtColor(image, cv2.COLOR_BGR2GRAY): Converts to grayscale.

2️⃣ Convert Image to Float32

 gray = np.float32(gray): Required for Harris Corner Detector.


3️⃣ Apply Harris Corner Detection

 cv2.cornerHarris(gray, 2, 3, 0.04):

o blockSize=2: Neighborhood size.

o ksize=3: Sobel operator aperture size.

o k=0.04: Harris constant.

4️⃣ Threshold and Mark Corners

 image[harris_response > 0.01 * harris_response.max()] = [0, 0, 255]:

o Marks high-response pixels (corners) in red.

5️⃣ Display Image with Corners

 cv2.imshow("Harris Corners", image): Shows the image.

 cv2.waitKey(0) & cv2.destroyAllWindows(): Waits for user input and closes the
window.

10. Summary of Harris Corner Detection

✔ Mathematical Error Function helps find corners based on intensity changes.


✔ Structure Tensor (Matrix M) is key to computing corner strength.
✔ Harris Response RRR determines whether a point is a corner, edge, or flat.
✔ Python Code allows numerical verification and real image detection using OpenCV

Singular Value Decomposition (SVD) Applications

SVD is widely used in computer vision, machine learning, and data processing. Here are
some key applications:

1. Image Compression

 Large images have thousands of pixels, making storage and transmission costly.

 SVD helps reduce image size while keeping important details.

 By keeping only the largest singular values, we can reconstruct the image with fewer
details.

2. Noise Reduction

 SVD helps remove noise by ignoring small singular values (which correspond to
noise).
 Only the most significant singular values are kept.

3. Face Recognition (Eigenfaces)

 Face images can be decomposed into eigenfaces using SVD.

 Each face is represented as a linear combination of basis faces (eigenfaces).

4. Principal Component Analysis (PCA)

 PCA uses SVD to reduce dimensions of data.

 Helps in feature selection and data visualization.

Numerical Example: Image Compression Using SVD

Let's take a grayscale image, apply SVD, and compress it.

Step 1: Load and Display the Image

python

CopyEdit

import numpy as np

import cv2

import matplotlib.pyplot as plt

# Load grayscale image

image = cv2.imread("image.jpg", cv2.IMREAD_GRAYSCALE)

# Display original image

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

plt.title("Original Image")

plt.show()

Step 2: Apply SVD

python

CopyEdit
# Compute SVD

U, Sigma, Vt = np.linalg.svd(image, full_matrices=False)

# Convert Sigma to a diagonal matrix

Sigma = np.diag(Sigma)

🔹 np.linalg.svd(image, full_matrices=False): Computes U, Sigma, Vt.


🔹 np.diag(Sigma): Converts singular values into a diagonal matrix.

Step 3: Image Reconstruction Using Fewer Singular Values

python

CopyEdit

# Keep only first 50 singular values (compression)

k = 50

compressed_image = np.dot(U[:, :k], np.dot(Sigma[:k, :k], Vt[:k, :]))

# Display compressed image

plt.imshow(compressed_image, cmap="gray")

plt.title("Compressed Image with k=50")

plt.show()
import numpy as np

import matplotlib.pyplot as plt

# Define matrix A

A = np.array([[4, 2], [2, 3]])

# Compute eigenvalues and eigenvectors

eigenvalues, eigenvectors = np.linalg.eig(A)

# Print results

print("Eigenvalues:", eigenvalues)

print("Eigenvectors:\n", eigenvectors)
Code Explanation

1️⃣ Compute eigenvalues and eigenvectors using np.linalg.eig(A).


2️⃣ Use plt.quiver() to draw eigenvectors.
3️⃣ The red and blue arrows show the principal directions of transformation.

Summary

✔ SVD Applications: Image compression, noise reduction, face recognition, PCA.


✔ SVD Code: We compressed an image using the top k singular values.
✔ Visualizing Quadratics: Eigenvalues reveal function shapes.
✔ Python Code for Eigenvectors: Showed how transformations affect geometry.
import numpy as np
# Define gradients for 3 pixels

Ix = np.array([2, 1, 3]) # x-gradient

Iy = np.array([3, 4, 2]) # y-gradient

# Compute elements of the Harris Matrix

A = np.sum(Ix ** 2)

B = np.sum(Ix * Iy)

C = np.sum(Iy ** 2)

# Form Harris Matrix M

M = np.array([[A, B], [B, C]])

# Compute determinant and trace

det_M = np.linalg.det(M)

trace_M = np.trace(M)

# Compute Harris Response R

k = 0.04

R = det_M - k * (trace_M ** 2)

# Print results

print("Harris Matrix M:\n", M)

print("\nDeterminant of M:", det_M)

print("Trace of M:", trace_M)

print("Harris Response R:", R)

# Check if it is a corner

if R > 0:
print("\nCorner Detected!")

else:

print("\nNot a Corner")

5. Summary

✔ We approximated the error function using Taylor Series Expansion.


✔ The Harris Matrix M describes local structure using gradients.
✔ Eigenvalues of M determine if a point is a corner, edge, or flat.
✔ We solved a numerical example manually and in Python.

How Do You Find a Corner? (Aperture Problem & Small Window Analysis)

In this section, we will explore how to recognize a corner and why edges alone are not
enough for detecting feature points.

1. Understanding the Aperture Problem

🔹 What is the Aperture Problem?


The Aperture Problem occurs when a small window (patch of pixels) sees only a small part
of the image.
As a result, we cannot distinguish between different movements in the image.

💡 Example: Imagine looking at a moving striped pattern through a small hole (aperture).

 The stripes appear to move in one direction (e.g., left to right).

 But we cannot tell if they are moving diagonally or vertically.

 This happens because edges do not provide enough information to uniquely


determine motion.

📌 Key Problem:

 If we look at a single edge, shifting it along the edge direction does not change
intensity.

 We need to detect points where intensity changes in two directions (i.e., corners).

2. How to Find a Corner?


🔹 What is a Corner?
A corner is a point where intensity changes in both directions.

 Flat Region: No intensity change → No feature.

 Edge: Intensity changes in one direction only → Ambiguous feature.

 Corner: Intensity changes in both directions → Good feature!

Mathematical Definition
4. Python Code for Corner Detection

Now, let’s implement this numerical example in Python.

import numpy as np

# Define image gradients (3×3 window)

Ix = np.array([[3, 2, 1], [4, 2, 1], [3, 2, 1]]) # x-gradient

Iy = np.array([[2, 1, 2], [3, 2, 4], [1, 3, 2]]) # y-gradient

# Compute elements of the Harris Matrix

A = np.sum(Ix ** 2)

B = np.sum(Ix * Iy)

C = np.sum(Iy ** 2)

# Form Harris Matrix M

M = np.array([[A, B], [B, C]])


# Compute determinant and trace

det_M = np.linalg.det(M)

trace_M = np.trace(M)

# Compute Harris Response R

k = 0.04

R = det_M - k * (trace_M ** 2)

# Print results

print("Harris Matrix M:\n", M)

print("\nDeterminant of M:", det_M)

print("Trace of M:", trace_M)

print("Harris Response R:", R)

# Check if it is a corner

if R > 0:

print("\nCorner Detected!")

else:

print("\nNot a Corner (Edge or Flat Region)")

5. Summary

✔ Aperture Problem: Edges alone are not enough to determine feature points.
✔ Harris Corner Detection: Uses gradients and eigenvalues to classify points.
✔ Mathematical Solution: Eigenvalues of Harris Matrix M tell us if a point is a corner, edge,
or flat region.
✔ Python Implementation: We computed A,B,C, determinant, trace, and Harris Response to
classify points.
import cv2

import numpy as np

# Load image and convert to grayscale


image = cv2.imread("chessboard.jpg")

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Step 1: Compute Image Gradients using Sobel filters

Ix = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3) # Gradient in X direction

Iy = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3) # Gradient in Y direction

# Step 2: Compute elements of the Harris Matrix

Ixx = Ix ** 2

Iyy = Iy ** 2

Ixy = Ix * Iy

# Step 3: Apply Gaussian filter to smooth the values

A = cv2.GaussianBlur(Ixx, (3, 3), 1)

B = cv2.GaussianBlur(Ixy, (3, 3), 1)

C = cv2.GaussianBlur(Iyy, (3, 3), 1)

# Step 4: Compute Harris Response Function

det_M = (A * C) - (B ** 2)

trace_M = A + C

k = 0.04

R = det_M - k * (trace_M ** 2)

# Step 5: Thresholding

threshold = 0.01 * R.max()

corners = np.zeros_like(gray)

corners[R > threshold] = 255


# Step 6: Non-Maximum Suppression (Keeping only the strongest corners)

dilated = cv2.dilate(corners, None)

final_corners = np.zeros_like(gray)

final_corners[(corners == 255) & (corners == dilated)] = 255

# Mark detected corners on the original image

image[final_corners == 255] = [0, 0, 255] # Red color for corners

# Display results

cv2.imshow("Harris Corners", image)

cv2.waitKey(0)

cv2.destroyAllWindows()
import cv2

import numpy as np

# Load image and convert to grayscale

image = cv2.imread("chessboard.jpg")

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Step 1: Compute Image Gradients (Sobel filters)

Ix = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3) # x-gradient

Iy = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3) # y-gradient


# Step 2: Compute Harris Matrix Elements

Ixx = Ix * Ix

Iyy = Iy * Iy

Ixy = Ix * Iy

# Step 3: Apply Gaussian Blur (Smoothing)

A = cv2.GaussianBlur(Ixx, (3, 3), 1)

B = cv2.GaussianBlur(Ixy, (3, 3), 1)

C = cv2.GaussianBlur(Iyy, (3, 3), 1)

# Step 4: Compute Harris Response Function

det_M = (A * C) - (B ** 2)

trace_M = A + C

k = 0.04 # Empirical constant (0.04 - 0.06)

R = det_M - k * (trace_M ** 2)

# Step 5: Thresholding (Keeping Strongest Corners)

threshold = 0.01 * R.max()

corners = np.zeros_like(gray)

corners[R > threshold] = 255 # Mark corners in white

# Step 6: Non-Maximum Suppression (Keeping Only the Strongest Corners)

dilated = cv2.dilate(corners, None)

final_corners = np.zeros_like(gray)

final_corners[(corners == 255) & (corners == dilated)] = 255

# Step 7: Mark Corners on Original Image

image[final_corners == 255] = [0, 0, 255] # Red color for corners


# Display results

cv2.imshow("Optimized Harris Corners", image)

cv2.waitKey(0)

cv2.destroyAllWindows()

You might also like