_Computer Vision LAB Assignment (1)
_Computer Vision LAB Assignment (1)
Lab Assignment
…………………………………………………………………………………………………………………………………………………………………..
UNIT – 1
QUESTION – 01 :
Write a program to simulate the geometry of image formation by projecting 3D points onto
a 2D plane using Perspective Projection. Visualize how changing the camera's position and
orientation affects the 2D image.
ANSWER :
Python Program:
import numpy as np
projected_points = []
x=f*X/Z
y=f*Y/Z
projected_points.append([x, y])
return np.array(projected_points)
cube_3d = np.array([
[-1, -1, 5], [1, -1, 5], [1, 1, 5], [-1, 1, 5],
])
focal_length = 1.5
cube_2d = perspective_projection(cube_3d,
focal_length) # Plotting
plt.figure(figsize=(8, 6))
plt.xlabel("x")
plt.ylabel("y"
plt.grid(True)
plt.show()
OUTPUT :
UNIT – 2
Two-View
Geometry and
Depth Estimation
QUESTION - 02 :
Implement Stereo Image Rectification for a pair of stereo images. Using Epipolar Geometry,
rectify the images and display the corresponding epipolar lines.
ANSWER :
Python Program:
import cv2
import numpy as np
left_img = cv2.imread('/content/Naruto.jpg', 0)
right_img = cv2.imread('/content/Naruto(1).jpg', 0)
# Assume you have matched points or use a feature matcher
sift = cv2.SIFT_create()
bf = cv2.BFMatcher()
good_matches = []
for m, n in matches:
good_matches.append(m)
good_matches])
r, c = img1.shape
img1_color = cv2.cvtColor(img1, cv2.COLOR_GRAY2BGR)
lines1 = cv2.computeCorrespondEpilines(pts2.reshape(-1,1,2), 2, F)
lines1 = lines1.reshape(-1,3)
# Display
plt.subplot(121), plt.imshow(img5)
plt.subplot(122), plt.imshow(img6)
plt.suptitle("Epipolar Lines")
plt.show()
OUTPUT :
UNIT – 3
QUESTION – 03 :
Develop a feature detection module using the Harris Corner Detector and visualize the
detected corners on a real-world image (e.g., an image of a building facade).
ANSWER :
Python Program:
import cv2
import numpy as np
img = cv2.imread('building.jpg')
gray = cv2.cvtColor(img,
# Display
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()
OUTPUT :
UNIT – 4
QUESTION – 04 :
Create an object tracking system using Optical Flow (Lucas-Kanade Method). Track a moving
object (like a ball or a person) across frames from a video clip.
ANSWER :
Python Program:
import cv2
import numpy as np
# Load video
cap = cv2.VideoCapture('/content/Sample.mp4')
mask = np.zeros_like(old_frame)
while True:
if not ret:
break
if p1 is not None:
good_new = p1[st==1]
good_old = p0[st==1]
# Draw tracks
a, b = new.ravel()
c, d = old.ravel()
a, b, c, d = int(a), int(b), int(c), int(d) # <--- This line fixes the error
break
# Update
old_gray = frame_gray.copy()
p0 = good_new.reshape(-1, 1, 2)
cap.release()
cv2.destroyAllWindows()
OUTPUT :
QUESTION – 05 :
Implement Photometric Stereo using multiple images of an object under different lighting
conditions. Estimate the object's surface normals and reconstruct the object's 3D shape.
Assignment
ANSWER :
Python Program:
import numpy as np
import cv2
# Load images
img1 = cv2.imread('/content/Naruto.jpg', 0)
img2 = cv2.imread('/content/Naruto(1).jpg', 0)
img3 = cv2.imread('/content/Naruto(2).jpg', 0)
# Resize all images to the same size (use img1 size as reference)
h, w = img1.shape
# Stack images
L = np.array([
[0, 0, 1],
[1, 0, 1],
[0, 1, 1]
])
h, w, _ = I.shape
I = I.reshape(-1, 3).T
G = np.linalg.lstsq(L, I, rcond=None)[0]
G = G.T.reshape(h, w, 3)
# Surface normals
# Visualization
plt.figure(figsize=(8,8))
plt.quiver(normals[:,:,0], normals[:,:,1])
plt.title('Surface Normals')
plt.show()
OUTPUT :