Lecture 7 AI Summary
Lecture 7 AI Summary
import numpy as np
# Load image
circles = np.uint16(np.around(circles))
cv2.waitKey(0)
cv2.destroyAllWindows()
1. cv2.imread("circle.png", cv2.IMREAD_GRAYSCALE):
2. cv2.HoughCircles(...):
3. np.uint16(np.around(circles)):
import numpy as np
ght = cv2.createGeneralizedHoughBallard()
ght.setTemplate(edges_template)
# Detect the shape in the target image
cv2.waitKey(0)
cv2.destroyAllWindows()
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
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.
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:
Mathematical Explanation
import numpy as np
# Convert to float32
image_float = np.float32(image)
plt.imshow(image, cmap='gray')
plt.title("Harris Corners")
plt.show()
1️ cv2.imread("chessboard.jpg", cv2.IMREAD_GRAYSCALE):
2️ np.float32(image):
3️ cv2.cornerHarris(image_float, 2, 3, 0.04):
Limitations
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
import numpy as np
# Define matrix A
# Compute SVD
U, Sigma, Vt = np.linalg.svd(A)
# Print results
print("Matrix A:\n", A)
Defines matrix A.
2️⃣ np.linalg.svd(A):
Applications of SVD
import numpy as np
# Define matrix A
# Print results
print("Matrix A:\n", A)
print("\nEigenvalues:\n", eigenvalues)
print("\nEigenvectors:\n", eigenvectors)
# Visualizing eigenvectors
plt.xlim(-3, 3)
plt.ylim(-3, 3)
plt.axhline(y=0, color='k')
plt.axvline(x=0, color='k')
plt.grid()
plt.legend()
Summary
import numpy as np
import cv2
B = np.sum(Ix * Iy)
C = np.sum(Iy ** 2)
det_M = np.linalg.det(M)
trace_M = np.trace(M)
R = det_M - k * (trace_M ** 2)
# Print results
# 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 = np.float32(gray)
# Show result
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.cornerHarris(gray, 2, 3, 0.04):
cv2.waitKey(0) & cv2.destroyAllWindows(): Waits for user input and closes the
window.
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.
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.
python
CopyEdit
import numpy as np
import cv2
plt.imshow(image, cmap="gray")
plt.title("Original Image")
plt.show()
python
CopyEdit
# Compute SVD
Sigma = np.diag(Sigma)
python
CopyEdit
k = 50
plt.imshow(compressed_image, cmap="gray")
plt.show()
import numpy as np
# Define matrix A
# Print results
print("Eigenvalues:", eigenvalues)
print("Eigenvectors:\n", eigenvectors)
Code Explanation
Summary
A = np.sum(Ix ** 2)
B = np.sum(Ix * Iy)
C = np.sum(Iy ** 2)
det_M = np.linalg.det(M)
trace_M = np.trace(M)
k = 0.04
R = det_M - k * (trace_M ** 2)
# Print results
# Check if it is a corner
if R > 0:
print("\nCorner Detected!")
else:
print("\nNot a Corner")
5. Summary
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.
💡 Example: Imagine looking at a moving striped pattern through a small hole (aperture).
📌 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).
Mathematical Definition
4. Python Code for Corner Detection
import numpy as np
A = np.sum(Ix ** 2)
B = np.sum(Ix * Iy)
C = np.sum(Iy ** 2)
det_M = np.linalg.det(M)
trace_M = np.trace(M)
k = 0.04
R = det_M - k * (trace_M ** 2)
# Print results
# Check if it is a corner
if R > 0:
print("\nCorner Detected!")
else:
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
Ixx = Ix ** 2
Iyy = Iy ** 2
Ixy = Ix * Iy
det_M = (A * C) - (B ** 2)
trace_M = A + C
k = 0.04
R = det_M - k * (trace_M ** 2)
# Step 5: Thresholding
corners = np.zeros_like(gray)
final_corners = np.zeros_like(gray)
# Display results
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np
image = cv2.imread("chessboard.jpg")
Ixx = Ix * Ix
Iyy = Iy * Iy
Ixy = Ix * Iy
det_M = (A * C) - (B ** 2)
trace_M = A + C
R = det_M - k * (trace_M ** 2)
corners = np.zeros_like(gray)
final_corners = np.zeros_like(gray)
cv2.waitKey(0)
cv2.destroyAllWindows()