0% found this document useful (0 votes)
16 views20 pages

Computer Vision

The document outlines a practical record for the Computer Vision course (CCS338) at C.S.I. Institute of Technology, detailing various experiments conducted from February to May 2024. It includes aims, procedures, coding examples, and results for tasks such as OpenCV installation, image annotation, pose estimation, 3D reconstruction, and object detection using Kalman Filter and Camshift. Each experiment concludes with a verification of successful implementation.

Uploaded by

V Revathi
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)
16 views20 pages

Computer Vision

The document outlines a practical record for the Computer Vision course (CCS338) at C.S.I. Institute of Technology, detailing various experiments conducted from February to May 2024. It includes aims, procedures, coding examples, and results for tasks such as OpenCV installation, image annotation, pose estimation, 3D reconstruction, and object detection using Kalman Filter and Camshift. Each experiment concludes with a verification of successful implementation.

Uploaded by

V Revathi
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/ 20

C.S.I.

INSTITUTE OF TECHNOLOGY
THOVALAI – 629 302

Affiliated to ANNA UNIVERSITY, CHENNAI - 600 025

CCS338
COMPUTER VISION

DEPARTMENT OF INFORMATION TECHNOLOGY

NAME :

REGISTER NO. :

SEMESTER :

YEAR :

C.S.I INSTITUTE OF TECHNOLOGY


THOVALAI – 629 302
Affiliated to ANNA UNIVERSITY, CHENNAI - 600 025

COMPUTER VISION RECORD


University Register No:

Name……………………………............. Class No……………………………..

Branch of study…………………............ Semester/Year……………………….

Subject Name........................................... Subject Code.......................................

Certified that this is the Bonafide Record of work done by………………...….....……………………….

From___________________year/semester of Information Technology branch during the academic


year February 2024 to May 2024 for the subject, CCS338 – Computer Vision.

Head of Department Staff in - Charge

Certified that this record has been submitted for the practical examination held at C.S.I. INSTITUTE
OF TECHNOLOGY, THOVALAI on …………….

Internal Examiner External Examiner

INDEX
SI.No Date Programs Page No Signature
1 OpenCV Installation and
working with Python.

2 Image Annotation – Drawing lines,


text circle, rectangle, ellipse on
images

3 Pose Estimation

4 3D Reconstruction – Creating Depth


map from stereo images

5 Object Detection and Tracking using


Kalman Filter, Camshift
Ex.No: 01 OpenCV Installation and working with Python.
Date:

AIM:
To install OpenCV and work with Python,

PROCEDURE:

1.To install OpenCV, we must have Python and PIP installed on your system. To
check if your system already contains Python, go through the following instructions:
Open the Command Prompt or Terminal.
Type the following command: python –version
2. If Python is already installed, it will display the Python version.
If Python is already installed, it will display the Python version.

3.To install OpenCV, go to the command line interface and type the following
command:
pip install opencv-python

4.Type the command in the Terminal and proceed!

5.Collecting information and downloading data:


6.Installing packages:

7.Finished Installation:

8. To check if OpenCV is correctly installed, just run the following commands to


perform a version check:

RESULT:
Thus, the installation of OpenCV and working with python is
successfully verified.
Ex.No:02 Image Annotation
Date:
AIM:
To implement image rotation, text insertion, drawing circles,
rectangles, and ellipses in images using OpenCV.

PROCEDURE:

To implement image rotation, text insertion, drawing circles, rectangles, and ellipses
in images using OpenCV,

1.Image Rotation:
Use the cv2.rotate() function to rotate images.
Provide the image and the rotation direction (e.g.,
cv2.ROTATE_90_CLOCKWISE, cv2.ROTATE_90_COUNTERCLOCKWISE) as
parameters.

2.Text Insertion:
Utilize the cv2.putText() function to insert text into images.
Specify the text, position, font, font scale, color, thickness, and line type as
parameters.

3.Drawing Circles:
Use the cv2.circle() function to draw circles on images.
Provide the image, center coordinates, radius, color, thickness, and line type as
parameters.

4.Drawing Rectangles:
Utilize the cv2.rectangle() function to draw rectangles on images.
Specify the image, top-left and bottom-right coordinates of the rectangle, color,
thickness, and line type as parameters.

5.Drawing Ellipses:
Use the cv2.ellipse() function to draw ellipses on images.
Provide the image, center coordinates, axes lengths, angle, start angle, end angle,
color, thickness, and line type as parameters.
CODING:

import cv2

# Read Images
img = cv2.imread('D:\dog.webp')

# Display Image
cv2.imshow('Original Image', img)
cv2.waitKey(0)

# Print error message if image is not read


if img is None:
print('Could not read image')

# Draw line on image


image_line = img.copy()
# Draw the image from point A to B
point_A = (200, 80)
point_B = (450, 80)
cv2.line(image_line, point_A, point_B, (255, 255, 0), thickness=3,
lineType=cv2.LINE_AA)
cv2.imshow('Image with Line', image_line)
cv2.waitKey(0)

# Draw a circle on the image


image_circle = img.copy()
# Define the center of circle
circle_center = (415, 190)
# Define the radius of the circle
radius = 100
cv2.circle(image_circle, circle_center, radius, (0, 0, 255), thickness=3,
lineType=cv2.LINE_AA)
cv2.imshow('Image with Circle', image_circle)
cv2.waitKey(0)

# Draw a fixed circle on the image


image_fixed_circle = img.copy()
# Define center of the circle
circle_center = (415, 190)
# Define the radius of the circle
radius = 100
cv2.circle(image_fixed_circle, circle_center, radius, (255, 0, 0), thickness=1,
lineType=cv2.LINE_8)
cv2.imshow('Image with Fixed Circle', image_fixed_circle)
cv2.waitKey(0)

# Draw a rectangle on the image


image_rectangle = img.copy()
# Define the starting and ending points of the rectangle
start_point = (300, 115)
end_point = (475, 225)
cv2.rectangle(image_rectangle, start_point, end_point, (0, 0, 255), thickness=3,
lineType=cv2.LINE_8)
cv2.imshow('Image with Rectangle', image_rectangle)
cv2.waitKey(0)

# Draw an ellipse on the image


image_ellipse = img.copy()
# Define the center point of ellipse
ellipse_center = (415, 190)
# Define the major and minor axes of the ellipse
axis1 = (100, 50)
axis2 = (125, 50)
# Horizontal
cv2.ellipse(image_ellipse, ellipse_center, axis1, 0, 0, 360, (255, 0, 0), thickness=3,
lineType=cv2.LINE_AA)
# Vertical
cv2.ellipse(image_ellipse, ellipse_center, axis2, 90, 0, 360, (0, 0, 255), thickness=3,
lineType=cv2.LINE_AA)
cv2.imshow('Ellipse Image', image_ellipse)
cv2.waitKey(0)

# Write text on the image


image_text = img.copy()
# Set the text you want to put on the image
text = 'I am a Happy dog!'
# Where you want to put the text
org = (50, 350)
# Write the text on the image
cv2.putText(image_text, text, org, cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 0),
thickness=2)
# Display the output image with text overlay
cv2.imshow('Image with Text', image_text)
cv2.waitKey(0)
# Destroy all windows
cv2.destroyAllWindows()

OUTPUT:

1.Image Rotation:

2.Text Insertion:
3.Drawing Circles:

4.Drawing Rectangles:
5.Drawing Ellipses:

RESULT:

Thus, the Image Annotation techniques were implemented


successfully.

Ex.No: 03 Pose Estimation


Date:

AIM:

To implement pose estimation for the video.

PROCEDURE:

1. Install Required Libraries:


2. Ensure you have Python installed on your system. Then, install the
required libraries using pip: pip install opencv-python mediapipe.
3. Import Libraries:
4. Import the necessary libraries in your Python script:
5. import cv2
6. import mediapipe as mp
7. Initialize Pose Estimator:
8. Initialize the pose estimator from the mediapipe library with desired
confidence levels for detection and tracking.
9. Initialize Video Capture:
 Initialize video capture using OpenCV, specifying the path to your
video file.
 Read frames from the video capture and process them for pose
detection.
 Once the processing is complete, release the video capture and close
any OpenCV windows.
 Save the script and execute it. The program will read the specified
video file, perform pose estimation on each frame, draw the detected
landmarks, and display the output frame in a window. Press 'q' to exit
the program.
CODING:

import cv2
import mediapipe as mp

# Initialize pose estimator


mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
pose = mp_pose.Pose()

# Initialize video capture


cap = cv2.VideoCapture(r'D:\video1.mp4')
while cap.isOpened():
# Read frame
ret, frame = cap.read()
if not ret:
break

try:
# Resize the frame for portrait video
frame = cv2.resize(frame, (600, 350))

# Convert to RGB
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

# Process the frame for pose detection


pose_results = pose.process(frame_rgb)

# Draw landmarks on the frame


if pose_results.pose_landmarks:
mp_drawing.draw_landmarks(frame, pose_results.pose_landmarks,
mp_pose.POSE_CONNECTIONS)

# Display the frame


cv2.imshow('Output', frame)

except Exception as e:
print(e)
break

if cv2.waitKey(1) & 0xFF == ord('q'):


break
cap.release()
cv2.destroyAllWindows()

OUTPUT:

RESULT:

Thus, implementation of post estimation is verified successfully.

Ex.No:04 3D Reconstruction – Creating Depth map from stereo images


Date:
AIM:
To implement 3D Reconstruction, creating depth map from stereo images using
Open CV.

PROCEDURE:

 Collect or take stereo images.


 Import Open CV and matplotlib libraries.
 Read both left and right images.
 Calculate disparity using stereo.compute.

CODING:

import cv2 as cv
import matplotlib.pyplot as plt
imgR = cv.imread(r'D:\right.png', 0)
imgL = cv.imread(r'D:\left.png', 0)
stereo = cv.StereoBM_create(numDisparities=16, blockSize=15)
disparity = stereo.compute(imgL, imgR)
plt.imshow(disparity, 'gray')
plt.show()

INPUT:

OUTPUT:
RESULT:

Thus, the expectation of 3D Reconstruction creating depth map from


stereo images is verified successful
Ex.No:05 Object Detection and Tracking using Kalman Filter,
Date: Camshift

AIM:
To implement Object Detection and Tracking using Gaussian Filter, Camshift
using OpenCV.

PROCEDURE:

1. Import Libraries: Import the necessary libraries, including OpenCV and


NumPy.
2. Capture Video: Initialize a video capture object to read the input video.
3. Initialize Object Tracker: Define the initial bounding box coordinates for the
object to be tracked.
4. Read First Frame: Read the first frame from the video.
5. Preprocess Frame: Optionally, preprocess the frame if required, such as
resizing, converting color spaces, or applying filters.
6. Initialize Kalman Filter: Optionally, initialize a Kalman filter to predict the next
state of the object.
7. Initialize Histogram Backprojection: Convert the region of interest (ROI) in the
first frame to the HSV color space and compute its histogram.
8. Define Criteria for Camshift: Define termination criteria for the Camshift
algorithm, such as the maximum number of iterations or a minimum movement
threshold.
9. Object Tracking Loop:
10.Read Frame: Read the next frame from the video.
11.Preprocess Frame: Preprocess the frame if necessary.
12.Apply Gaussian Filter: Optionally, apply a Gaussian filter to the frame to reduce
noise.
13.Compute Histogram Backprojection: Backproject the histogram of the ROI onto
the current frame.
14.Apply Camshift Algorithm: Use the Camshift algorithm to update the object's
location based on the histogram backprojection.
15.Draw Bounding Box: Draw the updated bounding box around the tracked object
on the frame.
16.Display Frame: Display the frame with the bounding box.
17.Update Kalman Filter (Optional): If using a Kalman filter, update the filter with
the new object location.
18.Check for Termination: Check if the video has ended or if the user has pressed a
key to exit the loop.

CODING:

import cv2
import numpy as np
cap = cv2.VideoCapture(r'D:\video1.mp4')
x, y, width, height = 400, 440, 150, 150
tracker_window = (x, y, width, height)
roi = (y, y + height, x, x + width) # Define roi here
_, frame = cap.read()
hsv_roi = cv2.cvtColor(frame[roi[0]:roi[1], roi[2]:roi[3]], cv2.COLOR_BGR2HSV)
roi_hist = cv2.calcHist([hsv_roi], [0], None, [180], [0, 180])
term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 15, 2)
while True:
ret, frame = cap.read()
if not ret:
break

frame_resized = cv2.resize(frame, (720, 720), interpolation=cv2.INTER_CUBIC)


cv2.imshow('Original', frame_resized)

# Perform thresholding on the frame


_, frame_thresholded = cv2.threshold(frame, 180, 155,
cv2.THRESH_TOZERO_INV)

# Convert the frame from BGR to HSV format


hsv = cv2.cvtColor(frame_thresholded, cv2.COLOR_BGR2HSV)

# Backproject the histogram to the frame


dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)

# Apply meanshift to get the new location


ret, tracker_window = cv2.CamShift(dst, tracker_window, term_crit)

# Draw the tracking window on the frame


pts = cv2.boxPoints(ret)
pts = np.int0(pts)
result = cv2.polylines(frame_resized, [pts], True, (0, 255, 255), 2)

cv2.imshow('CamShift', result)

# Set ESC key as the exit button


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

# Release the capture object and close any opened windows


cap.release()
cv2.destroyAllWindows()
OUTPUT:

RESULT:
Thus, implementation of object detection and tracking using camshift is
verified successfully.

You might also like