0% found this document useful (0 votes)
21 views47 pages

Computer Vision Lab Manual for ECE

The document is a laboratory manual for the Computer Vision Laboratory course at C.Abdul Hakeem College of Engineering and Technology, detailing experiments for third-year electronics and communication engineering students. It includes instructions for software installation, basic image processing tasks, image annotation, enhancement techniques, and object detection using OpenCV. Each experiment outlines the aim, required hardware and software, procedures, and expected results.
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)
21 views47 pages

Computer Vision Lab Manual for ECE

The document is a laboratory manual for the Computer Vision Laboratory course at C.Abdul Hakeem College of Engineering and Technology, detailing experiments for third-year electronics and communication engineering students. It includes instructions for software installation, basic image processing tasks, image annotation, enhancement techniques, and object detection using OpenCV. Each experiment outlines the aim, required hardware and software, procedures, and expected results.
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

C.

ABDUL HAKEEM COLLEGE OF ENGINEERING AND


TECHNOLOGY

MELVISHARAM, RANIPET - 632509.

DEPARTMENT OF ELECTRONICS AND COMMUNICATION


ENGINEERING

REGULATION 2021

III YEAR VI SEMESTER

CCS338 COMPUTER VISION LABORATORY

MANUAL

PREPARED BY: [Link]


[Link] HAKEEM COLLEGE OF ENGINEERING AND
TECHNOLOGY
MELVISHARAM, RANIPET - 632509.

DEPARTMENT OF ELECTRONICS AND COMMUNICATION


ENGINEERING
REGULATION 2021

III YEAR VI SEMESTER

CCS338 COMPUTER VISION LABORATORY

MANUAL
PREPARED BY: [Link]

NAME:…………………………………………………………………..

ROLL NO:………………………………………………………………

REGISTER NO:………………………………………………………..

CLASS:…………………………………………………………………
CCS338 COMPUTER VISION

LABORATORY EXPERIMENTS:
Software needed:
OpenCV computer vision Library for OpenCV in Python / PyCharm or C++ / Visual Studio or or equivalent
1 OpenCV Installation and working with Python
2 Basic Image Processing - loading images, Cropping, Resizing, Thresholding, Contour analysis, Bolb
detection
3 Image Annotation – Drawing lines, text circle, rectangle, ellipse on images
4 Image Enhancement - Understanding Color spaces, color space conversion, Histogram equalization,
Convolution, Image smoothing, Gradients, Edge Detection
5 Image Features and Image Alignment – Image transforms – Fourier, Hough, Extract ORB Image features,
Feature matching, cloning, Feature matching based image alignment
6 Image segmentation using Graphcut / Grabcut
7 Camera Calibration with circular grid
8 Pose Estimation
9 3D Reconstruction – Creating Depth map from stereo images
10 Object Detection and Tracking using Kalman Filter, Camshift
INDEX
LIST OF EXPERIMENTS

S. No Date Name of the Experiment Page No Marks Signature


Study of Opencv Installation and Working
1 1
with Python
2 Basic Image Processing 3

3 Image Annotation 8

4 Image Enhancement 10

5 Image Features and Image Alignment 14

6 Image Segmentation Using Grabcut 18

7 Camera Calibration with Circular Grid 21

8 Pose Estimation 23

9 3d Reconstruction 25

10 Object Detection Using Kalman Filter 28

CONTENT BEYOND SYLLABUS

11 Real-Time Edge Detection Using Canny 31

12 Object Detection Using Haar Cascades 33

13 Real-Time Hand Gesture Recognition 35

14 Image Region Selector 38

15 Access Image Properties 40

Annexure
Exp. No: 1
STUDY OF OPENCV INSTALLATION AND WORKING WITH PYTHON

AIM

To understand the process of installing OpenCV and explore its basic functionalities for
image processing tasks in Python.

HAEDWARE REQUIRED

Personal Computer

SOFTWARE REQUIRED

Python (version 3.x)


pip (Python package manager)
OpenCV library

THEORY

Introduction

OpenCV (Open-Source Computer Vision Library) is a widely used library for computer
vision and image processing. It provides tools to perform operations such as image manipulation,
object detection, video analysis, and more. This experiment focuses on understanding the steps to
install OpenCV and its capabilities in Python.

Prerequisites

• Basic knowledge of Python programming.


• Python and pip installed on the system.

1. OpenCV

OpenCV is an open-source computer vision library with a comprehensive set of functions for
image and video analysis. It supports multiple programming languages, including Python, C++, and
Java. OpenCV is commonly used in applications like face detection, image recognition, augmented
reality, and robotics.

2. Features of OpenCV in Python:

• Image Processing: Resizing, filtering, edge detection, etc.


• Computer Vision: Object detection, face recognition, and feature extraction.
• Video Processing: Reading, writing, and manipulating video streams.
• Machine Learning: Pre-trained models and algorithms for classification and clustering.

3. Steps to Install OpenCV:

a. Install OpenCV via pip:

1. Open a terminal or command prompt.


2. Type the command:

1
pip install opencv-python
3. For additional functionalities (like GUI support), install opencv-contrib-python:
pip install opencv-contrib-python

b. Verify Installation:

1. Open Python in the terminal or an IDE.


2. Import OpenCV and check its version:
python
import cv2
print(cv2.__version__)

c. Installing Dependencies (if required):


• Ensure NumPy is installed as it is essential for OpenCV. Install it using:
pip install numpy

4. Working with OpenCV in Python (Overview):

• Reading an Image:
OpenCV provides the [Link]() function to read an image.
• Displaying an Image:
The [Link]() function displays an image in a new window.
• Image Manipulation:
OpenCV offers tools to resize, crop, rotate, and filter images.
• Saving an Image:
Use [Link]() to save images after processing.

RESULT

Thus, the process of installing OpenCV and explore its basic functionalities for image
processing tasks in Python was understood.

2
Exp. No: 2
BASIC IMAGE PROCESSING

AIM

To perform basic image processing tasks, including loading images, cropping, resizing,
thresholding, contour analysis, and blob detection using OpenCV.

HAEDWARE REQUIRED

Personal Computer

SOFTWARE REQUIRED

Pycharm

PROCEDURE

1. Open PyCharm.
2. Click on File → New Project.
3. Set the project name.
4. Choose the base interpreter (Python) and click Create.
5. Replace 'your_image_path.jpg' with the path to your image file.
6. Type the program
7. Run the program by clicking the Run button in PyCharm.

PROGRAM CODE

Loading an Image

import cv2
image=[Link]("C:/Users/SINDHU/Pictures/[Link]")
# Convert to grayscale
gray_image = [Link](image, cv2.COLOR_BGR2GRAY)
# Save the grayscale image
[Link]('image',gray_image)
# To hold the window on screen, we use [Link] method
[Link](0)
[Link]()

3
OUTPUT

Cropping of an Image

import cv2
image=[Link](‘C:/Users/SINDHU/Pictures/[Link]’)
# Cropping an image
cropped_image = image[80:280, 150:330]
# Display cropped image
[Link]('cropped_image',cropped_image)
[Link](0)
[Link]()

OUTPUT

Resizing of an Image
import cv2
image=[Link]("C:/Users/SINDHU/Pictures/[Link]")
# Resize the image to 300x200
resized_image = [Link](image, (300, 200))
# Save the resized image
[Link]('resized image',resized_image )
[Link](0)
[Link]()

4
OUTPUT

Thresholding of an Image

import cv2
image=[Link]("C:/Users/SINDHU/Pictures/[Link]")
# Apply thresholding
_,thresh_image = [Link](image, 127, 255, cv2.THRESH_BINARY)
# Save the thresholded image
[Link]('thresh_image',thresh_image)
[Link](0)
[Link]()

OUTPUT

Contour Analysis of an Image

import cv2
import matplotlib
image=[Link]("C:/Users/SINDHU/Pictures/[Link]")
# Convert to grayscale
gray = [Link](image, cv2.COLOR_BGR2GRAY)
# Perform thresholding
_, thresh = [Link](gray, 127, 255, cv2.THRESH_BINARY)

# Find contours
contours, _ = [Link](thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

5
# Draw contours on a copy of the original image
contour_image = [Link]()
[Link](contour_image, contours, -1, (0, 255, 0), 2)
# Display the result
[Link]('Contour Image', contour_image)
[Link](0)
[Link]()
OUTPUT

Bolb Detection of an Image

import cv2
import numpy as np

# Read the input image


image = [Link]("C:/Users/SINDHU/Pictures/[Link]", cv2.IMREAD_GRAYSCALE) #
Replace with your image path

# Set up the SimpleBlobDetector parameters


params = cv2.SimpleBlobDetector_Params()

# Filter by area
[Link] = True
[Link] = 100 # Minimum area of the blob
[Link] = 2000 # Maximum area of the blob

# Filter by circularity
[Link] = True
[Link] = 0.7 # Adjust based on requirements

# Filter by convexity
[Link] = True
[Link] = 0.8

# Filter by inertia (elongation)


[Link] = True
[Link] = 0.5

6
# Create a SimpleBlobDetector
detector = cv2.SimpleBlobDetector_create(params)

# Detect blobs
keypoints = [Link](image)

# Draw detected blobs as red circles


# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle
corresponds to the blob size
output_image = [Link](image, keypoints, [Link]([]), (0, 0, 255),
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Show the blobs


[Link]("Blobs Detected", output_image)
[Link](0)
[Link]()

OUTPUT

RESULT

Thus, the basic image processing tasks, including loading images, cropping, resizing,
thresholding, contour analysis, and blob detection using OpenCV, have been executed, and the output
was verified.

7
Exp. No: 3
IMAGE ANNOTATION

AIM

To perform basic Image Annotation including drawing lines, text circle, rectangle and ellipse
on images using OpenCV.

HAEDWARE REQUIRED

Personal Computer

SOFTWAREBREQUIRED

Pycharm

PROCEDURE

1. Open PyCharm.
2. Click on File → New Project.
3. Set the project name.
4. Choose the base interpreter (Python) and click Create.
5. Replace 'your_image_path.jpg' with the path to your image file.
6. Type the program
7. Run the program by clicking the Run button in PyCharm.

PROGRAM CODE

import cv2
import numpy as np

# Create a blank image (optional)


image = [Link]((500, 500, 3), dtype='uint8') # Black image
image[:] = (255, 255, 255) # Fill with white

# Draw a Line
[Link](image, (50, 50), (450, 50), (255, 0, 0), thickness=2) # Blue line

# Draw a Rectangle
[Link](image, (100, 100), (400, 200), (0, 255, 0), thickness=3) # Green rectangle
[Link](image, (150, 250), (350, 350), (0, 0, 255), thickness=-1) # Filled red rectangle

# Draw a Circle
[Link](image, (250, 400), 50, (255, 255, 0), thickness=3) # Cyan circle
[Link](image, (400, 400), 30, (0, 255, 255), thickness=-1) # Filled yellow circle

# Draw an Ellipse
[Link](image, (250, 250), (100, 50), 30, 0, 360, (128, 0, 128), thickness=2) # Purple ellipse

# Add Text

8
[Link](image, 'OpenCV Drawing', (120, 480), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0),
thickness=2)

# Save the image to verify if the window does not display


[Link]('annotated_image.jpg', image)

# Display the image


[Link]('Annotated Image', image)

[Link](0)
[Link]()

OUTPUT

RESULT

Thus, the basic Image Annotation including drawing lines, text circle, rectangle and ellipse on
images using OpenCV, have been executed, and the output was verified.

9
Exp. No: 4
IMAGE ENHANCEMENT

AIM

To perform Image Enhancement including color space conversion, Histogram equialization,


Convolution, Image smoothing, Gradients, Edge Detection using OpenCV.

HAEDWARE REQUIRED

Personal Computer

SOFTWAREBREQUIRED

Pycharm

PROCEDURE

1. Open PyCharm.
2. Click on File → New Project.
3. Set the project name.
4. Choose the base interpreter (Python) and click Create.
5. Replace 'your_image_path.jpg' with the path to your image file.
6. Type the program
7. Run the program by clicking the Run button in PyCharm.

PROGRAM CODE

import cv2
import numpy as np
from matplotlib import pyplot as plt

# Load an image
image = [Link]('C:/Users/SINDHU/Pictures/[Link]') # Replace with your image path

# Resize the image for consistency (optional)


image = [Link](image, (500, 500))

# Display the original image


[Link]('Original Image', image)

# 1. Understanding Color Spaces


gray_image = [Link](image, cv2.COLOR_BGR2GRAY)
hsv_image = [Link](image, cv2.COLOR_BGR2HSV)

[Link]('Grayscale Image', gray_image)


[Link]('HSV Image', hsv_image)

# 2. Histogram Equalization (Grayscale)


equalized_image = [Link](gray_image)

10
[Link]('Histogram Equalized Image', equalized_image)

# 3. Convolution (Custom Kernel)


# Define a sharpening kernel
kernel = [Link]([[0, -1, 0], [-1, 5, -1],0, -1, 0]])
sharpened_image = cv2.filter2D(image, -1, kernel)
[Link]('Sharpened Image', sharpened_image)

# 4. Image Smoothing
# Apply Gaussian Blur
gaussian_blur = [Link](image, (5, 5), 0)
[Link]('Gaussian Blur', gaussian_blur)

# 5. Gradients
# Sobel Gradient (X and Y)
sobelx = [Link](gray_image, cv2.CV_64F, 1, 0, ksize=3)
sobely = [Link](gray_image, cv2.CV_64F, 0, 1, ksize=3)
sobelx = [Link](sobelx)
sobely = [Link](sobely)
[Link]('Sobel X', sobelx)
[Link]('Sobel Y', sobely)

# 6. Edge Detection
edges = [Link](gray_image, 100, 200)
[Link]('Canny Edge Detection', edges)

# Show Histograms (for visualization)


[Link](figsize=(10, 5))
[Link](1, 2, 1)
[Link](gray_image.ravel(), 256, [0, 256])
[Link]('Original Grayscale Histogram')
[Link](1, 2, 2)
[Link](equalized_image.ravel(), 256, [0, 256])
[Link]('Equalized Histogram')
[Link]()

# Wait for a key press and close all windows


[Link](0)
[Link]()

OUTPUT

11
Original Image Grayscale Image

HSV Image Histogram Equalized Image

Sharpened Image Gaussian Filtered Image

12
Sobel Image Canny Edge Detected Image

Histogram Image

RESULT

Thus, the Image Enhancement including color space conversion, Histogram equialization,
Convolution, Image smoothing, Gradients, Edge Detection using OpenCV, have been executed, and the
output was verified.

13
Exp. No: 5
IMAGE FEATURES AND IMAGE ALIGNMENT

AIM

To implement various image processing techniques including image transforms (Fourier


Transform and Hough Transform), extraction of ORB image features, feature matching, cloning, and
feature-matching-based image alignment using OpenCV.

HAEDWARE REQUIRED

Personal Computer

SOFTWARE REQUIRED

Pycharm

PROCEDURE

1. Open PyCharm.
2. Click on File → New Project.
3. Set the project name.
4. Choose the base interpreter (Python) and click Create.
5. Replace 'your_image_path.jpg' with the path to your image file.
6. Type the program
7. Run the program by clicking the Run button in PyCharm.

PROGRAM CODE

import cv2
import numpy as np
from matplotlib import pyplot as plt

# Load input images


image1 = [Link]('C:/Users/SINDHU/Pictures/[Link]') # Replace with your reference
image path
image2 = [Link]('C:/Users/SINDHU/Pictures/[Link]') # Replace with your image to align

# Resize for consistency (optional)


image1 = [Link](image1, (500, 500))
image2 = [Link](image2, (500, 500))

# 1. Fourier Transform
gray1 = [Link](image1, cv2.COLOR_BGR2GRAY)
dft = [Link](np.float32(gray1), flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = [Link](dft)
magnitude_spectrum = 20 * [Link]([Link](dft_shift[:, :, 0], dft_shift[:, :, 1]))

[Link](figsize=(10, 5))
[Link](1, 2, 1)

14
[Link](gray1, cmap='gray')
[Link]('Original Image (Gray)')
[Link](1, 2, 2)
[Link](magnitude_spectrum, cmap='gray')
[Link]('Magnitude Spectrum')
[Link]()

# 2. Hough Transform for Line Detection


edges = [Link](gray1, 50, 150, apertureSize=3)
lines = [Link](edges, 1, [Link] / 180, 200)

hough_image = [Link]()
if lines is not None:
for rho, theta in lines[:, 0]:
a = [Link](theta)
b = [Link](theta)
x0 = a * rho
y0 = b * rho
x1 = int(x0 + 1000 * (-b))
y1 = int(y0 + 1000 * (a))
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * (a))
[Link](hough_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
[Link]("Hough Transform Lines", hough_image)

# 3. ORB Feature Extraction


orb = cv2.ORB_create()
keypoints1, descriptors1 = [Link](image1, None)
keypoints2, descriptors2 = [Link](image2, None)

image1_with_keypoints = [Link](image1, keypoints1, None, color=(0, 255, 0))


[Link]('ORB Keypoints (Image 1)', image1_with_keypoints)

# 4. Feature Matching
bf = [Link](cv2.NORM_HAMMING, crossCheck=True)
matches = [Link](descriptors1, descriptors2)
matches = sorted(matches, key=lambda x: [Link])

# Draw matches
matched_image = [Link](image1, keypoints1, image2, keypoints2, matches[:20], None,
flags=2)
[Link]('Feature Matching', matched_image)

# 5. Image Alignment Using Feature Matching


# Extract points from matches
src_pts = np.float32([keypoints1[[Link]].pt for m in matches]).reshape(-1, 1, 2)
dst_pts = np.float32([keypoints2[[Link]].pt for m in matches]).reshape(-1, 1, 2)

15
# Compute the homography matrix
M, mask = [Link](dst_pts, src_pts, [Link], 5.0)

# Warp the second image to align with the first


aligned_image = [Link](image2, M, ([Link][1], [Link][0]))
[Link]('Aligned Image', aligned_image)

# Wait for a key press and close all windows


[Link](0)
[Link]()

OUTPUT

Hough Tranformed Image ORB Image

16
Feature Matching

Aligned Image

RESULT

Thus, the implementation of various image processing techniques like image feature, matching
and alignment using OpenCV was successfully completed.

17
Exp. No: 6
IMAGE SEGMENTATION USING GRABCUT

AIM

To implement image segmentation using the GrabCut algorithm in OpenCV to extract the
foreground object from the background in an image.

HAEDWARE REQUIRED

Personal Computer

SOFTWARE REQUIRED

Pycharm

PROCEDURE

1. Open PyCharm.
2. Click on File → New Project.
3. Set the project name.
4. Choose the base interpreter (Python) and click Create.
5. Replace 'your_image_path.jpg' with the path to your image file.
6. Type the program
7. Run the program by clicking the Run button in PyCharm.

PROGRAM CODE

import cv2
import numpy as np
from matplotlib import pyplot as plt

# Load the image


image = [Link]('C:/Users/SINDHU/Pictures/[Link]') # Replace with your image path

# Resize for consistency (optional)


image = [Link](image, (600, 400))
original_image = [Link]()

# Step 1: Define the region of interest (ROI)


# You can adjust this rectangle to include the desired foreground area
rect = (50, 50, 500, 500) # (x, y, width, height)

# Step 2: Initialize GrabCut parameters


mask = [Link]([Link][:2], dtype=np.uint8) # Mask initialized to 0's
bg_model = [Link]((1, 65), dtype=np.float64) # Background model
fg_model = [Link]((1, 65), dtype=np.float64) # Foreground model

# Step 3: Apply the GrabCut algorithm


[Link](image, mask, rect, bg_model, fg_model, 5, mode=cv2.GC_INIT_WITH_RECT)

18
# Step 4: Process the mask
# The mask has values: 0 (background), 1 (probably background), 2 (foreground), 3 (probably
foreground)
# We convert the mask to binary, where 0 and 1 are background, and 2 and 3 are foreground.
binary_mask = [Link]((mask == 2) | (mask == 0), 0,1).astype('uint8')

# Step 5: Extract the segmented foreground


segmented_image = cv2.bitwise_and(original_image, original_image, mask=binary_mask)

# Display results
[Link]("Original Image", original_image)
[Link]("Segmented Image", segmented_image)

# Step 6: Visualize the mask


[Link](figsize=(10, 5))
[Link](1, 2, 1)
[Link]("Mask")
[Link](mask, cmap='gray')
[Link](1, 2, 2)
[Link]("Segmented Image")
[Link]([Link](segmented_image, cv2.COLOR_BGR2RGB))
[Link]()

[Link](0)
[Link]()

OUTPUT

Original Image Segmented Image

19
RESULT

Thus, the GrabCut algorithm was successfully implemented for image segmentation and the
output was verified.

20
Exp. No: 7
CAMERA CALIBRATION WITH CIRCULAR GRID

AIM

To perform camera calibration using a circular grid pattern in OpenCV.

HAEDWARE REQUIRED

Personal Computer

SOFTWARE REQUIRED

Pycharm

PROCEDURE

1. Open PyCharm.
2. Click on File → New Project.
3. Set the project name.
4. Choose the base interpreter (Python) and click Create.
5. Replace 'your_image_path.jpg' with the path to your image file.
6. Type the program
7. Run the program by clicking the Run button in PyCharm.

PROGRAM CODE

import numpy as np
import cv2
import glob

# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)


objp = [Link]((6*7,3), np.float32)
objp[:,:2] = [Link][0:7,0:6].[Link](-1,2)

# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.

images = [Link]('C:/Users/SINDHU/Pictures/[Link]')

for fname in images:


img = [Link](fname)
gray = [Link](img, cv.COLOR_BGR2GRAY)

# Find the chess board corners


ret, corners = [Link](gray, (7,6), None)

21
# If found, add object points, image points (after refining them)
if ret == True:
[Link](objp)

corners2 = [Link](gray,corners, (11,11), (-1,-1), criteria)


[Link](corners2)

# Draw and display the corners


[Link](img, (7,6), corners2, ret)
[Link]('img', img)

[Link](0)
[Link]()

OUTPUT

RESULT

Thus, the camera calibration with circular grid operation using OpenCV has been
implemented and the output was verified.

22
Exp. No: 8
POSE ESTIMATION

AIM

To perform using OpenCV.

HAEDWARE REQUIRED

Personal Computer

SOFTWARE REQUIRED

Pycharm

PROCEDURE

1. Open PyCharm.
2. Click on File → New Project.
3. Set the project name.
4. Choose the base interpreter (Python) and click Create.
5. Replace 'your_image_path.jpg' with the path to your image file.
6. Type the program
7. Run the program by clicking the Run button in PyCharm.

PROGRAM CODE

import cv2
import numpy as np

# Define the 3D model points of the object (in the object's coordinate space)
# These correspond to key landmarks on the object (e.g., corners of a cube, facial landmarks, etc.)
object_points = [Link]([(0.0, 0.0, 0.0),(0.0, 1.0, 0.0),(1.0, 1.0, 0.0),(1.0, 0.0, 0.0)],
dtype=np.float32)

# Define the 2D image points (these are detected landmarks in the image)
image_points = [Link]([(320, 240),(320, 340),(420, 340),(420, 240)], dtype=np.float32)

# Define the camera matrix and distortion coefficients


# Camera matrix: intrinsic parameters of the camera
focal_length = 800 # Example focal length
center = (320, 240) # Example image center
camera_matrix = [Link]([[focal_length, 0, center[0]],[0, focal_length, center[1]],[0, 0, 1]],
dtype=np.float32)

# Assume no lens distortion for simplicity


dist_coeffs = [Link]((4, 1), dtype=np.float32)

# Solve for the rotation and translation vectors


success, rotation_vector, translation_vector = [Link](object_points, image_points,

23
camera_matrix, dist_coeffs)

if success:
print("Rotation Vector:\n", rotation_vector)
print("Translation Vector:\n", translation_vector)

# Project the 3D points onto the image plane for visualization


projected_points, _ = [Link](
object_points, rotation_vector, translation_vector, camera_matrix, dist_coeffs
)

# Example: Draw the projected points on an image


img = [Link]((480, 640, 3), dtype=np.uint8)
for point in projected_points:
x, y = int(point[0][0]), int(point[0][1])
[Link](img, (x, y), 5, (0, 255, 0), -1)

# Display the image


[Link]("Pose Estimation", img)
[Link](0)
[Link]()

OUTPUT

RESULT

Thus the camera calibration using a circular grid pattern in OpenCV has been implemented
and the output was verified.

24
Exp. No: 9
3D RECONSTRUCTION

AIM

To perform 3D reconstruction by creating a depth map from stereo image pairs, enabling the
estimation of scene depth and spatial structure using OpenCV.

HAEDWARE REQUIRED

Personal Computer

SOFTWARE REQUIRED

Pycharm

PROCEDURE

1. Open PyCharm.
2. Click on File → New Project.
3. Set the project name.
4. Choose the base interpreter (Python) and click Create.
5. Replace 'your_image_path.jpg' with the path to your image file.
6. Type the program
7. Run the program by clicking the Run button in PyCharm.

PROGRAM CODE

import cv2
import numpy as np

def create_depth_map(left_image_path, right_image_path):


# Load stereo images
left_image = [Link](left_image_path, cv2.IMREAD_GRAYSCALE)
right_image = [Link](right_image_path, cv2.IMREAD_GRAYSCALE)

if left_image is None or right_image is None:


print("Error: Unable to load one or both images. Check file paths.")
exit()

# Ensure images are the same size


if left_image.shape != right_image.shape:
print("Images are not the same size. Resizing...")
right_image = [Link](right_image, (left_image.shape[1], left_image.shape[0]))

# Check image depth


if left_image.dtype != np.uint8:
print("Converting images to 8-bit grayscale.")
left_image = left_image.astype(np.uint8)

25
right_image = right_image.astype(np.uint8)

# Initialize the StereoBM or StereoSGBM matcher


# Using StereoSGBM for better results
min_disp = 0
num_disp = 16 * 6 # Number of disparities (must be divisible by 16)
block_size = 9 # Block size (odd value)

stereo = cv2.StereoSGBM_create(minDisparity=min_disp,numDisparities=num_disp,blockSize=
block_size,P1=8 * 3 * block_size**2,P2=32 * 3 * block_size**2,disp12MaxDiff=1,
uniquenessRatio=15,speckleWindowSize=100,speckleRange=32,preFilterCap=63)

# Compute the disparity map


disparity = [Link](left_image, right_image).astype(np.float32) / 16.0

# Normalize the disparity map for visualization


disparity_normalized = [Link](disparity, None, alpha=0, beta=255, norm_type=
cv2.NORM_MINMAX)
disparity_normalized = np.uint8(disparity_normalized)

# Display the disparity map


[Link]('Disparity Map', disparity_normalized)
[Link](0)
[Link]()

# Optionally, convert disparity to depth map (requires camera parameters)


# depth_map = (baseline * focal_length) / disparity
# Baseline and focal length depend on your stereo camera setup

# Paths to stereo images


left_image_path = ('C:/Users/SINDHU/Pictures/[Link]') # Replace with your left image path
right_image_path = ('C:/Users/SINDHU/Pictures/[Link]') # Replace with your right image path

create_depth_map(left_image_path, right_image_path)

OUTPUT
image 1 image 2

26
Disparity Map

RESULT

Thus, the 3D reconstruction by creating a depth map from stereo image pairs, enabling the
estimation of scene depth and spatial structure using OpenCV, have been executed, and the output was
verified.

27
Exp. No: 10
OBJECT DETECTION USING KALMAN FILTER

AIM

To perform the object detection with the help of Kalman filtering technique using OpenCV.

HAEDWARE REQUIRED

Personal Computer

SOFTWARE REQUIRED

Pycharm

PROCEDURE

1. Open PyCharm.
2. Click on File → New Project.
3. Set the project name.
4. Choose the base interpreter (Python) and click Create.
5. Replace 'your_image_path.jpg' with the path to your image file.
6. Type the program
7. Run the program by clicking the Run button in PyCharm.

PROGRAM CODE

import cv2
import numpy as np

def object_detection_kalman():
# Initialize video capture
cap = [Link]('C:/Users/SINDHU/Pictures/objdete.mp4') # Change to video file
path if needed

# Initialize background subtractor for object detection


bg_subtractor = cv2.createBackgroundSubtractorMOG2()

# Initialize Kalman Filter


kalman = [Link](4, 2) # State (x, y, dx, dy), Measurement (x, y)
[Link] = [Link]([[1, 0, 0, 0], [0, 1, 0, 0]], np.float32)
[Link] = [Link]([[1, 0, 1, 0],[0, 1, 0, 1],[0, 0, 1, 0],[0, 0, 0, 1]], np.float32)
[Link] = [Link](4, dtype=np.float32) * 0.03

# Get video properties


width = int([Link](cv2.CAP_PROP_FRAME_WIDTH))
height = int([Link](cv2.CAP_PROP_FRAME_HEIGHT))
fps = [Link](cv2.CAP_PROP_FPS)

# Initialize VideoWriter

28
fourcc = cv2.VideoWriter_fourcc(*'XVID') # Codec
out = [Link]('C:/Users/SINDHU/Pictures/output_tracking.avi', fourcc, fps, (width,
height))

while [Link]():
ret, frame = [Link]()
if not ret:
break

# Preprocess the frame


gray = [Link](frame, cv2.COLOR_BGR2GRAY)
fg_mask = bg_subtractor.apply(gray)

# Find contours to detect objects


contours, _ = [Link](fg_mask, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
# Filter small detections
if [Link](cnt) < 500:
continue

# Get bounding box of the object


x, y, w, h = [Link](cnt)
center_x, center_y = x + w // 2, y + h // 2

# Kalman Filter Update (Measurement)


measured = [Link]([[np.float32(center_x)], [np.float32(center_y)]])
[Link](measured)

# Kalman Filter Prediction


predicted = [Link]()
pred_x, pred_y = int(predicted[0]), int(predicted[1])

# Draw detection and prediction


[Link](frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # Detected object
[Link](frame, (center_x, center_y), 5, (255, 0, 0), -1) # Measured position
[Link](frame, (pred_x, pred_y), 5, (0, 0, 255), -1) # Predicted position

last_measured = (center_x, center_y)


last_predicted = (pred_x, pred_y)

# Display the results


[Link]("Object Detection with Kalman Filter", frame)
if [Link](1) & 0xFF == ord('q'):
break

[Link]()
[Link]()

29
if __name__ == "__main__":
object_detection_kalman()

OUTPUT

RESULT

Thus, the object detection with the help of Kalman filter using OpenCV, has been executed,
and the output was verified.

30
Exp. No: 11
REAL-TIME EDGE DETECTION USING CANNY

AIM

To perform real-time edge detection using the Canny Edge Detection algorithm, enabling the
identification of edges using OpenCV.

HAEDWARE REQUIRED

Personal Computer

SOFTWARE REQUIRED

Pycharm

PROCEDURE

1. Open PyCharm.
2. Click on File → New Project.
3. Set the project name.
4. Choose the base interpreter (Python) and click Create.
5. Replace 'your_image_path.jpg' with the path to your image file.
6. Type the program
7. Run the program by clicking the Run button in PyCharm.

PROGRAM CODE

import cv2

def nothing(x):
pass

# Initialize webcam
cap = [Link](0)

# Create a window for the trackbars


[Link]('Edge Detection')
[Link]('Min Threshold', 'Edge Detection', 50, 255, nothing)
[Link]('Max Threshold', 'Edge Detection', 150, 255, nothing)

while True:
ret, frame = [Link]()
if not ret:
break

# Convert to grayscale
gray = [Link](frame, cv2.COLOR_BGR2GRAY)

31
# Get trackbar positions
min_thresh = [Link]('Min Threshold', 'Edge Detection')
max_thresh = [Link]('Max Threshold', 'Edge Detection')

# Apply Canny edge detection


edges = [Link](gray, min_thresh, max_thresh)

# Display results
[Link]('Edge Detection', edges)
if [Link](1) & 0xFF == ord('q'):
break

[Link]()
[Link]()

OUTPUT

RESULT

Thus, the real-time edge detection using the Canny Edge Detection algorithm, enabling the
identification of edges using OpenCV, has been executed, and the output was verified.

32
Exp. No: 12
OBJECT DETECTION USING HAAR CASCADES

AIM

To perform the object detection using Haar cascades, enabling the identification and
localization of specific objects, such as faces or eyes using OpenCV.

HAEDWARE REQUIRED

Personal Computer

SOFTWARE REQUIRED

Pycharm

PROCEDURE

1. Open PyCharm.
2. Click on File → New Project.
3. Set the project name.
4. Choose the base interpreter (Python) and click Create.
5. Replace 'your_image_path.jpg' with the path to your image file.
6. Type the program
7. Run the program by clicking the Run button in PyCharm.

PROGRAM CODE

import cv2

# Load pre-trained classifiers


face_cascade = [Link]([Link] +
'haarcascade_frontalface_default.xml')
eye_cascade = [Link]([Link] + 'haarcascade_eye.xml')

cap = [Link](0)

while True:
ret, frame = [Link]()
if not ret:
break

gray = [Link](frame, cv2.COLOR_BGR2GRAY)


faces = face_cascade.detectMultiScale(gray, 1.3, 5)

for (x, y, w, h) in faces:


[Link](frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
roi_gray = gray[y:y + h, x:x + w]
roi_color = frame[y:y + h, x:x + w]

33
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex, ey, ew, eh) in eyes:
[Link](roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)

[Link]('Face and Eye Detection', frame)


if [Link](1) & 0xFF == ord('q'):
break

[Link]()
[Link]()

OUTPUT

RESULT

Thus, the object detection using Haar cascades, enabling the identification and localization of
specific objects, such as faces or eyes using OpenCV, has been executed, and the output was verified.

34
Exp. No: 13
REAL-TIME HAND GESTURE RECOGNITION

AIM

To perform the object detection with the help of Kalman filtering technique using OpenCV.

HAEDWARE REQUIRED

Personal Computer

SOFTWARE REQUIRED

Pycharm

PROCEDURE

1. Open PyCharm.
2. Click on File → New Project.
3. Set the project name.
4. Choose the base interpreter (Python) and click Create.
5. Replace 'your_image_path.jpg' with the path to your image file.
6. Type the program
7. Run the program by clicking the Run button in PyCharm.

PROGRAM

import cv2
import numpy as np
import math

# Initialize video capture


cap = [Link](0)

while True:
ret, frame = [Link]()
if not ret:
break

# Flip the frame for mirror view


frame = [Link](frame, 1)

# Define the region of interest (ROI) for hand detection


roi = frame[100:400, 300:600]
[Link](frame, (300, 100), (600, 400), (0, 255, 0), 2)
roi_hsv = [Link](roi, cv2.COLOR_BGR2HSV)

# Define skin color range in HSV


lower_skin = [Link]([0, 30, 60]) # Adjusted for better detection
upper_skin = [Link]([20, 150, 255])

35
# Create a mask for skin detection
mask = [Link](roi_hsv, lower_skin, upper_skin)

# Apply morphological operations to clean the mask


mask = [Link](mask, None, iterations=2)
mask = [Link](mask, (5, 5), 100)

# Find contours in the mask


contours, _ = [Link](mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
if contours:
# Find the largest contour
max_contour = max(contours, key=[Link])

# Skip small contours


if [Link](max_contour) > 2000:
# Approximate the contour and find convex hull
epsilon = 0.01 * [Link](max_contour, True)
approx = [Link](max_contour, epsilon, True)
hull = [Link](max_contour, returnPoints=False)

# Detect convexity defects


if hull is not None and len(hull) > 3:
defects = [Link](max_contour, hull)

# Count the number of fingers


count_fingers = 0
if defects is not None:
for i in range([Link][0]):
s, e, f, d = defects[i, 0]
start = tuple(max_contour[s][0])
end = tuple(max_contour[e][0])
far = tuple(max_contour[f][0])

# Calculate the angles


a = [Link](start, end)
b = [Link](start, far)
c = [Link](end, far)
angle = [Link]((b**2 + c**2 - a**2) / (2 * b * c))

# Count fingers based on angle threshold


if angle <= [Link](90) and d > 10000: # Filter based on defect depth
count_fingers += 1
[Link](roi, far, 5, (0, 0, 255), -1)

# Display the gesture


[Link](frame,f"Fingers: {count_fingers + 1}",(50, 50),cv2.FONT_ HERSHEY
_SIMPLEX,1,(0, 0, 255),2,)

36
# Draw contours and hull
[Link](roi, [max_contour], -1, (255, 0, 0), 2)
[Link](roi, [[Link](max_contour)], -1, (0, 255, 0), 2)

# Display the frames


[Link]("Frame", frame)
[Link]("Mask", mask)

# Break the loop on 'q' key press


if [Link](1) & 0xFF == ord('q'):
break

# Release resources
[Link]()
[Link]()

OUTPUT

RESULT

Thus, the object detection with the help of Kalman filtering technique using OpenCV has
been implemented and the output was verified.

37
Exp. No: 14
IMAGE REGION SELECTOR

AIM

To perform the Image Region Selector using ROI technique using OpenCV.

HAEDWARE REQUIRED

Personal Computer

SOFTWARE REQUIRED

Pycharm

PROCEDURE

1. Open PyCharm.
2. Click on File → New Project.
3. Set the project name.
4. Choose the base interpreter (Python) and click Create.
5. Replace 'your_image_path.jpg' with the path to your image file.
6. Type the program
7. Run the program by clicking the Run button in PyCharm.

PROGRAM

import cv2

# Step 1: Load the image


image_path = "C:/Users/SINDHU/Pictures/[Link]" # Replace with your image path
image = [Link](image_path)

# Step 2: Define the rectangle coordinates


# Coordinates are (x_start, y_start) and (x_end, y_end)
x_start, y_start = 70, 70
x_end, y_end = 340, 340

# Draw the rectangle on the image


rect_color = (0, 255, 0) # Green color
rect_thickness = 2
[Link](image, (x_start, y_start), (x_end, y_end), rect_color, rect_thickness)

# Step 3: Extract the region of interest (ROI)


roi = image[y_start:y_end, x_start:x_end]

# Step 4: Display the images


[Link]("Original Image with Rectangle", image)
[Link]("Extracted ROI", roi)

38
# Save the extracted ROI
output_path = "extracted_object.jpg"
[Link](output_path, roi)

# Wait for a key press and close all windows


[Link](0)
[Link]()

OUTPUT

Original Image Extracted ROI

RESULT

Thus, the Image Region Selector using ROI technique in OpenCV has been implemented and
the output was verified.

39
Exp. No: 15
ACCESS IMAGE PROPERTIES

AIM

To understand how to access and analyze the fundamental properties of an image using
OpenCV, including dimensions, number of channels, size, and data type.

HAEDWARE REQUIRED

Personal Computer

SOFTWARE REQUIRED

Pycharm

PROCEDURE

1. Open PyCharm.
2. Click on File → New Project.
3. Set the project name.
4. Choose the base interpreter (Python) and click Create.
5. Replace 'your_image_path.jpg' with the path to your image file.
6. Type the program
7. Run the program by clicking the Run button in PyCharm.

PROGRAM

import cv2

def access_image_properties(image_path):
# Load the image
image = [Link](image_path)
if image is None:
print("Image not found. Please check the file path.")
return

# Display the image


[Link]("Loaded Image", image)

# Get image properties


height, width, channels = [Link] # Dimensions and number of channels
size = [Link] # Total number of pixels (height * width * channels)
data_type = [Link] # Data type of pixel values

# Print image properties


print(f"Image Properties:")
print(f" - Width: {width} pixels")
print(f" - Height: {height} pixels")
print(f" - Channels: {channels}")

40
print(f" - Size: {size} bytes")
print(f" - Data Type: {data_type}")

# Wait for a key press and close the image window


[Link](0)
[Link]()

# Path to the input image


image_path = "C:/Users/SINDHU/Pictures/[Link]"

# Call the function


access_image_properties(image_path)

OUTPUT

RESULT

Thus, the fundamental properties of an image using OpenCV has been implemented and the
output was verified.

41
ANNEXURE
Expected Viva-Voce Questions

1. How do you install OpenCV for Python?


o Use the command: pip install opencv-python. For full functionality, install additional
modules with: pip install opencv-contrib-python.
2. How do you verify if OpenCV is installed in Python?
o Run:
o import cv2
o print(cv2.__version__)
3. What is the role of cv2 in OpenCV?
o cv2 is the Python module used to interact with OpenCV functions.
4. How do you load an image in OpenCV?
o Use [Link](). Example:
o img = [Link]('[Link]')
5. What is the purpose of image thresholding?
o It converts grayscale images to binary by setting a threshold value.
6. What is contour analysis?
o It's the process of detecting and analyzing object boundaries in an image.
7. How is blob detection performed in OpenCV?
o Using [Link].
8. How do you draw a line on an image in OpenCV?
o Use [Link](img, pt1, pt2, color, thickness).
9. What function is used to add text to an image?
o [Link](img, text, org, font, fontScale, color, thickness).
10. How do you draw a rectangle on an image?
o Use [Link](img, pt1, pt2, color, thickness).
11. What are color spaces, and why are they important?
o Color spaces (e.g., RGB, HSV) define how colors are represented and are used for
color-based operations like filtering.
12. What is histogram equalization?
o A method to enhance the contrast of an image by redistributing pixel intensities.
13. How do you perform edge detection in OpenCV?
o Using [Link]().
14. What is the purpose of Fourier transform in image processing?
o To analyze the frequency content of an image.
15. What are ORB features?
o ORB (Oriented FAST and Rotated BRIEF) is a feature detection algorithm used for
keypoint detection and matching.
16. How is feature-based image alignment done?
o Extract features, match them, compute homography, and warp the image.
17. What is GrabCut in OpenCV?
o A segmentation technique that uses graph cuts for foreground and background
segmentation.
18. What is the key difference between GraphCut and GrabCut?
o GrabCut is an iterative version of GraphCut and is optimized for better user interaction.
19. What is camera calibration?
o The process of determining the intrinsic and extrinsic parameters of a camera.

42
20. Why is a circular grid used in camera calibration?
o It provides better accuracy for detecting grid points compared to a square grid.
21. What is pose estimation?
o Determining the position and orientation of an object or camera in 3D space.
22. What role does the PnP algorithm play in pose estimation?
o It estimates the pose from 2D-3D correspondences.
23. What is a depth map?
o A grayscale image where pixel intensity represents the depth of an object from the
camera.
24. How do you compute a depth map from stereo images?
o Using [Link] or [Link].
25. What is the Kalman filter?
o A predictive model used to estimate the state of a moving object from noisy data.
26. What is Camshift?
o Camshift (Continuously Adaptive Mean Shift) is a tracking algorithm used for object
tracking based on histograms.
27. What is the difference between detection and tracking?
o Detection identifies an object, while tracking follows its movement across frames.

43

You might also like