0% found this document useful (0 votes)
3 views17 pages

_Computer Vision LAB Assignment (1)

The document outlines a series of lab assignments for an MTech course in Computer Science and Engineering at SR University, focusing on various topics such as image formation, stereo image rectification, feature detection, object tracking, and photometric stereo. Each unit includes a programming task with Python code examples to implement concepts like perspective projection, Harris corner detection, and optical flow. The assignments are designed to enhance practical understanding of computer vision techniques.

Uploaded by

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

_Computer Vision LAB Assignment (1)

The document outlines a series of lab assignments for an MTech course in Computer Science and Engineering at SR University, focusing on various topics such as image formation, stereo image rectification, feature detection, object tracking, and photometric stereo. Each unit includes a programming task with Python code examples to implement concepts like perspective projection, Harris corner detection, and optical flow. The assignments are designed to enhance practical understanding of computer vision techniques.

Uploaded by

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

SR UNIVERSITY

SCHOOL OF COMPUTER SCIENCE AND ENGINEERING

Course: MTech Name: Kola Vinay Kumar

Type: Core HT.NO: 2403B05107

Course Code: 24CSMTB01 Course: Computer

Vision Year: 2025-2026

Lab Assignment

…………………………………………………………………………………………………………………………………………………………………..

UNIT – 1

Image Formation and Transformations

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 :

Lab Assignment 1: Perspective Projection

Python Program:

import numpy as np

import matplotlib.pyplot as plt

def perspective_projection(points_3d, f):

"""Projects 3D points onto 2D using a simple perspective projection."""

projected_points = []

for point in points_3d:


X, Y, Z = point

x=f*X/Z

y=f*Y/Z

projected_points.append([x, y])

return np.array(projected_points)

# Generate some 3D points (a cube)

cube_3d = np.array([

[-1, -1, 5], [1, -1, 5], [1, 1, 5], [-1, 1, 5],

[-1, -1, 7], [1, -1, 7], [1, 1, 7], [-1, 1, 7]

])

# Project and visualize

focal_length = 1.5

cube_2d = perspective_projection(cube_3d,

focal_length) # Plotting

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

plt.scatter(cube_2d[:, 0], cube_2d[:, 1], color='blue')

for i, (x, y) in enumerate(cube_2d):

plt.text(x, y, f"P{i}", fontsize=9)

plt.title("Perspective Projection of 3D Cube")

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 :

Lab Assignment 2: Stereo Image Rectification

Python Program:

import cv2

import numpy as np

import matplotlib.pyplot as plt

# Load stereo images

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()

kp1, des1 = sift.detectAndCompute(left_img, None)

kp2, des2 = sift.detectAndCompute(right_img, None)

bf = cv2.BFMatcher()

matches = bf.knnMatch(des1, des2, k=2)

# Filter good matches using Lowe's ratio

good_matches = []

for m, n in matches:

if m.distance < 0.75 * n.distance:

good_matches.append(m)

pts1 = np.float32([kp1[m.queryIdx].pt for m in

good_matches]) pts2 = np.float32([kp2[m.trainIdx].pt for m in

good_matches])

# Compute Fundamental Matrix

F, mask = cv2.findFundamentalMat(pts1, pts2, cv2.FM_LMEDS)

# Draw epipolar lines

def drawlines(img1, img2, lines, pts1, pts2):

'''Draw epipolar lines on the images.'''

r, c = img1.shape
img1_color = cv2.cvtColor(img1, cv2.COLOR_GRAY2BGR)

img2_color = cv2.cvtColor(img2, cv2.COLOR_GRAY2BGR)

for r, pt1, pt2 in zip(lines, pts1, pts2):

color = tuple(np.random.randint(0, 255, 3).tolist())

x0, y0 = map(int, [0, -r[2]/r[1]])

x1, y1 = map(int, [c, -(r[2]+r[0]*c)/r[1]])

img1_color = cv2.line(img1_color, (x0,y0), (x1,y1), color,1)

img1_color = cv2.circle(img1_color, tuple(pt1),5,color,-1)

img2_color = cv2.circle(img2_color, tuple(pt2),5,color,-1)

return img1_color, img2_color

# Find epilines for points in right image

lines1 = cv2.computeCorrespondEpilines(pts2.reshape(-1,1,2), 2, F)

lines1 = lines1.reshape(-1,3)

img5, img6 = drawlines(left_img, right_img, lines1, pts1.astype(int), pts2.astype(int))

# Display

plt.subplot(121), plt.imshow(img5)

plt.subplot(122), plt.imshow(img6)

plt.suptitle("Epipolar Lines")

plt.show()

OUTPUT :
UNIT – 3

Feature Detection and Description

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 :

Lab Assignment 3: Harris Corner Detector

Python Program:

import cv2

import numpy as np

import matplotlib.pyplot as plt


# Load image

img = cv2.imread('building.jpg')

gray = cv2.cvtColor(img,

cv2.COLOR_BGR2GRAY) gray = np.float32(gray)

# Harris Corner Detection

dst = cv2.cornerHarris(gray, 2, 3, 0.04)

dst = cv2.dilate(dst, None)

# Threshold to mark the corners

img[dst > 0.01 * dst.max()] = [0, 0, 255]

# Display

plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

plt.title('Harris Corners Detected')

plt.axis('off')

plt.show()

OUTPUT :
UNIT – 4

Object Recognition and Tracking

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 :

Lab Assignment 4: Optical Flow (Lucas-Kanade)

Python Program:

!pip install opencv-python-headless

import cv2

import numpy as np

from google.colab.patches import cv2_imshow # Import cv2_imshow

# Load video

cap = cv2.VideoCapture('/content/Sample.mp4')

# Feature params for ShiTomasi corner detection

feature_params = dict(maxCorners=100, qualityLevel=0.3, minDistance=7, blockSize=7)

# Parameters for Lucas-Kanade optical flow

lk_params = dict(winSize=(15,15), maxLevel=2,

criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))

# Take first frame and find corners


ret, old_frame = cap.read()

old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)

p0 = cv2.goodFeaturesToTrack(old_gray, mask=None, **feature_params)

# Create mask for drawing

mask = np.zeros_like(old_frame)

while True:

ret, frame = cap.read()

if not ret:

break

frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# calculate optical flow

p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)

# Select good points

if p1 is not None:

good_new = p1[st==1]

good_old = p0[st==1]

# Draw tracks

for i, (new, old) in enumerate(zip(good_new, good_old)):

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

mask = cv2.line(mask, (a, b), (c, d), color=(0,255,0), thickness=2)

frame = cv2.circle(frame, (a, b), 5, color=(0,0,255), thickness=-1)

img = cv2.add(frame, mask)

cv2_imshow(img) # Use cv2_imshow instead of cv2.imshow

if cv2.waitKey(30) & 0xFF == 27:

break

# Update

old_gray = frame_gray.copy()

p0 = good_new.reshape(-1, 1, 2)

cap.release()

cv2.destroyAllWindows()

OUTPUT :

Requirement already satisfied: opencv-python-headless in /usr/local/lib/python3.11/dist-


packages (4.11.0.86)

Requirement already satisfied: numpy>=1.21.2 in /usr/local/lib/python3.11/dist-packages


(from opencv-python-headless) (2.0.2)
UNIT – 5

Shape Recovery and Deep Learning Applications

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 :

Lab Assignment 5: Photometric Stereo

Python Program:

import numpy as np

import cv2

import matplotlib.pyplot as plt

# 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

img2 = cv2.resize(img2, (w, h))

img3 = cv2.resize(img3, (w, h))

# Stack images

I = np.stack([img1, img2, img3], axis=-1)


# Light directions (example)

L = np.array([

[0, 0, 1],

[1, 0, 1],

[0, 1, 1]

])

# Solve for normals

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

normals = G / (np.linalg.norm(G, axis=2, keepdims=True) + 1e-5)

# Visualization

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

plt.quiver(normals[:,:,0], normals[:,:,1])

plt.title('Surface Normals')

plt.show()
OUTPUT :

You might also like