[go: up one dir, main page]

0% found this document useful (0 votes)
142 views43 pages

Computer Vision Laboratory Manual

Uploaded by

raji
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)
142 views43 pages

Computer Vision Laboratory Manual

Uploaded by

raji
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

Department of Artificial Intelligence and Data Science

[Link] ARTIFICIAL INTELLIGENCE AND DATA SCIENCE


VII SEMESTER

REGULATION R-2021

CCS338 COMPUTER VISION LABORATORY

LABORATORY MANUAL

Prepared by

[Link]

Assistant Professor

Department of Artificial Intelligence and Data science

Ramco Institute of Technology

Rajapalayam
CCS338 COMPUTER VISION LT P C
202 3
COURSE OBJECTIVES:

 To understand the fundamental concepts related to Image formation and processing.


 To learn feature detection, matching and detection
 To become familiar with feature based alignment and motion estimation
 To develop skills on 3D reconstruction
 To understand image based rendering and recognition

LABORATORY EXPERIMENTS:

Software needed:

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

At the end of this course, the students will be able to:


CO1:To understand basic knowledge, theories and methods in image processing and computer vision.
CO2:To implement basic and some advanced image processing techniques in OpenCV.
CO3:To apply 2D a feature-based based image alignment, segmentation and motion estimations.
CO4:To apply 3D image reconstruction techniques
CO5:To design and develop innovative image processing and computer vision applications.

2
Ex. No 1
OpenCV Installation and working with Python
Date:

Aim: To install Open CV and working with python

Procedure:

1) To install OpenCV, one must have Python and PIP, preinstalled on their system. To check if your system
already contains Python, go through the following instructions: Open the Command line (search for cmd in
the Run dialog (+ R). Now run the following command:

python --version

2) If Python is already installed, it will generate a message with the Python version available.

3) To install OpenCV, just go to the command-line 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:

3
7) Finished Installation:

8) To check if OpenCV is correctly installed, just run the following commands to perform a
version check:
python
>>>import cv2
>>>print(cv2.__version__)

Result:

Thus, the Installation of Open CV and working with python is successfully verified.

4
Ex. No 1
Basic Image Processing-Loading
Date: images,cropping,Resizing,Thresholding,Contour analysis,Bolb detetion

Aim: To implement Basic Image Processing like loading images, cropping, resizing,
thresholding, contour analysis and bolb detection.
a) Loading Images:

The steps to Load and display an image in OpenCV are:

1. Read an image using imread() function.


2. Create a GUI window and display image using imshow() function.
3. Use function waitkey(0) to hold the image window on the screen by the specified number
of seconds, o means till the user closes it, it will hold GUI window on the screen.
4. Delete image window from the memory after displaying using destroyAllWindows()
function.
Program
import cv2
img = [Link]("[Link]", cv2.IMREAD_COLOR)
[Link]("image", img)
[Link](0)
[Link]()
Output Image

5
b) Cropping Image
Steps to Crop the image
Step 1: Read the image
Step 2: Get the image
Dimensions
Step 3: Slice the image

Program
import cv2
img =
[Link]("[Link]")
print(type(img))
# Shape of the image
print("Shape of the image",
[Link]) # [rows, columns]
crop = img[50:180, 100:300]
[Link]('original',
img)
[Link]('cropped',
crop) [Link](0)
[Link]()

Output Image:

6
c)Resizing
Steps for Resizing Image
source: Input Image array (Single-channel, 8-bit or floating-
point) dsize: Size of the output array
dest: Output array (Similar to the dimensions and type of Input image array)
[optional] fx: Scale factor along the horizontal axis [optional]
fy: Scale factor along the vertical axis [optional]
interpolation: One of the above interpolation methods [optional]

Coding:
image = [Link](r"D:\sims\eb\sim21\EB-ML-06-10-2022-Test-Output 15\
PERFORATION\Overkill\Fail\Blister 1 2022-03-12 12-59-43.859 T0 M0 G0 3
PERFORATION [Link]", 1)
# Loading the image
half = [Link](image, (0, 0), fx = 0.1, fy = 0.1)
bigger = [Link](image, (1050, 1610))
stretch_near = [Link](image, (780, 540),interpolation = cv2.INTER_LINEAR)
Titles =["Original", "Half", "Bigger", "Interpolation Nearest"] images
=[image, half, bigger, stretch_near]
count = 4

for i in range(count):
[Link](2, 2, i + 1)
[Link](Titles[i])
[Link](images[i])
[Link]()
Output:

7
d)Thresholding
Steps for Thresholding

cv2.THRESH_BINARY: If pixel intensity is greater than the set threshold, value set to 255,
else set to 0 (black).
cv2.THRESH_BINARY_INV: Inverted or Opposite case of cv2.THRESH_BINARY.
cv.THRESH_TRUNC: If pixel intensity value is greater than threshold, it is truncated to the
threshold. The pixel values are set to be the same as the threshold. All other values remain the
same.
cv.THRESH_TOZERO: Pixel intensity is set to 0, for all the pixels intensity, less than the
threshold value.
cv.THRESH_TOZERO_INV: Inverted or Opposite case of cv2.THRESH_TOZERO.

Coding:
import cv2
import numpy as np
# path to input image is specified and
# image is loaded with imread command
image1 = [Link]('[Link]')

# [Link] is applied over the


# image input with applied parameters #
to convert the image in grayscale
img = [Link](image1, cv2.COLOR_BGR2GRAY)

# applying different thresholding #


techniques on the input image #
all pixels value above 120 will #
be set to 255
ret, thresh1 = [Link](img, 120, 255, cv2.THRESH_BINARY)
ret, thresh2 = [Link](img, 120, 255, cv2.THRESH_BINARY_INV)
ret, thresh3 = [Link](img, 120, 255, cv2.THRESH_TRUNC)
ret, thresh4 = [Link](img, 120, 255, cv2.THRESH_TOZERO)
ret, thresh5 = [Link](img, 120, 255, cv2.THRESH_TOZERO_INV)
# the window showing output images #
with the corresponding thresholding

8
# techniques applied to the input images [Link]('Binary
Threshold', thresh1) [Link]('Binary Threshold Inverted',
thresh2) [Link]('Truncated Threshold', thresh3)
[Link]('Set to 0', thresh4)
[Link]('Set to 0 Inverted', thresh5)
# De-allocate any associated memory usage if
[Link](0) & 0xff == 27:
[Link]()
Output:

Contour Analysis
Steps for Contour
Analysis
Convert the image to a binary image, it is a common practice for the input image to be a binary
image (which should be a result of a thresholded image or edge detection).
Finding the contours using findContours() OpenCV function.
Draw these contours and show the image.

Code:
import cv2
import [Link] as plt #
read the image
image = [Link]("thumbs_up_down.jpg") #
convert to RGB
image = [Link](image, cv2.COLOR_BGR2RGB) #
convert to grayscale
gray = [Link](image, cv2.COLOR_RGB2GRAY) #
create a binary thresholded image
_, binary = [Link](gray, 225, 255, cv2.THRESH_BINARY_INV) #
show it
[Link](binary, cmap="gray")
[Link]()
# find the contours from the thresholded image
contours, hierarchy = [Link](binary, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)

9
# draw all contours
image = [Link](image, contours, -1, (0, 255, 0), 2) #
show the image with the drawn contours [Link](image)
[Link]()

Input:

Output:

Bolb detection
Code:
import cv2
import numpy as np
# Load image
image = [Link]('C://gfg//images//[Link]', 0)
# Set our filtering parameters
# Initialize parameter setting using [Link]
params = cv2.SimpleBlobDetector_Params()
#S et Area filtering parameters [Link] = True
[Link] = 100

10
# Set Circularity filtering parameters
[Link] = True
[Link] = 0.9
# Set Convexity filtering parameters
[Link] = True
[Link] = 0.2

# Set inertia filtering parameters


[Link] = True
[Link] = 0.01
# Create a detector with the parameters
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs
keypoints = [Link](image)
# Draw blobs on our image as red circles blank
= [Link]((1, 1))
blobs = [Link](image, keypoints, blank, (0, 0, 255),
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

number_of_blobs = len(keypoints)
text = "Number of Circular Blobs: " + str(len(keypoints))
[Link](blobs, text, (20, 550),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 100, 255), 2)
# Show blobs
[Link]("Filtering Circular Blobs Only", blobs)
[Link](0)
[Link]()

11
Output:

Result:
Thus, the Basic image processing techniques were implemented successfully.

12
Ex no3 Image Annotation – Drawing lines, text circle, rectangle, ellipse on
images

Aim: To implement Image Annotation for lines, Text Circle, rectangle, ellipse on images.
Code:
# Import dependencies import
cv2
# Read Images
img = [Link]('[Link]') #
Display Image
[Link]('Original Image',img)
[Link](0)
# Print error message if image is null if
img is None:
print('Could not read image') #
Draw line on image imageLine =
[Link]()
Draw the image from point A to B
pointA = (200,80)
pointB = (450,80)
[Link](imageLine, pointA, pointB, (255, 255, 0), thickness=3, lineType=cv2.LINE_AA)
[Link]('Image Line', imageLine)
[Link](0)
# Make a copy of image
imageCircle = [Link]() #
define the center of circle
circle_center = (415,190)
# define the radius of the circle radius
=100
# Draw a circle using the circle() Function
[Link](imageCircle, circle_center, radius, (0, 0, 255), thickness=3,
lineType=cv2.LINE_AA)
# Display the result
[Link]("Image Circle",imageCircle)
[Link](0)

13
# make a copy of the original image
imageFilledCircle = [Link]()
# define center of the circle
circle_center = (415,190)
# define the radius of the circle radius
=100
# draw the filled circle on input image
[Link](imageFilledCircle, circle_center, radius, (255, 0, 0), thickness=-1,
lineType=cv2.LINE_AA)
# display the output image
[Link]('Image with Filled Circle',imageFilledCircle) [Link](0)
# make a copy of the original image
imageRectangle = [Link]()

# define the starting and end points of the rectangle start_point


=(300,115)
end_point =(475,225)
# draw the rectangle
[Link](imageRectangle, start_point, end_point, (0, 0, 255), thickness= 3,
lineType=cv2.LINE_8)
# display the output [Link]('imageRectangle',
imageRectangle) [Link](0)
# make a copy of the original image
imageEllipse = [Link]()
# 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)
# draw the ellipse #Horizontal
[Link](imageEllipse, center, axis1, 0, 0, 360, (255, 0, 0), thickness=3) #Vertical
[Link](imageEllipse, center, axis2, 90, 0, 360, (0, 0, 255), thickness=3) #
display the output
[Link]('ellipse Image',imageEllipse)
[Link](0)

14
# make a copy of the original image
imageText = [Link]()
#let's write the text you want to put on the image text
= 'I am a Happy dog!'
#org: Where you want to put the text org
= (50,350)
# write the text on the input image
[Link](imageText, text, org, fontFace = cv2.FONT_HERSHEY_COMPLEX, fontScale =
1.5, color = (250,225,100))))
# display the output image with text over it
[Link]("Image Text",imageText)
[Link](0)
[Link]()

Output:

Result:
Thus, the Image Annotation techniques were implemented successfully.

15
Image Enhancement - Understanding Color spaces, color space
conversion, Histogram equalization, Convolution, Image smoothing,
Ex No4 Gradients, Edge Detection

Date:

Aim: To implement Image Enhancement techniques for Color spaces, Histogram, image
Smoothing and Edge detection.

a) Understanding Color Spaces


Code:
import cv2

image = [Link]('C://Users//Gfg//[Link]') B,
G, R = [Link](image)
# Corresponding channels are separated

[Link]("original", image)
[Link](0)

[Link]("blue", B)
[Link](0)

[Link]("Green", G)
[Link](0)

[Link]("red", R)
[Link](0)

[Link]()
Output:

16
b) Color Space Conversion
Code:
import cv2
# path
path = r'C:\Users\Administrator\Desktop\[Link]' #
Reading an image in default mode

src = [Link](path)
# Window name in which image is displayed
window_name = 'Image'
# Using [Link]() method
# Using cv2.COLOR_BGR2GRAY color space #
conversion code
image = [Link](src, cv2.COLOR_BGR2GRAY ) #
Displaying the image
[Link](window_name, image)
Input Image:

Output Image:

c) Histogram Equalization
Code:
# import Opencv
import cv2
# import Numpy

17
import numpy as np
# read a image using imread
img = [Link](\'F:\\do_nawab.png\', 0) #
creating a Histograms Equalization
# of a image using [Link]() equ
= [Link](img)
# stacking images side-by-side
res = [Link]((img, equ))
# show image input vs output
[Link](\'image\', res)
[Link](0)
[Link]()

Input Image:

Output Image:

d) Convolution
Code:
import cv2
import numpy as np
import [Link] as plt
# Reading the image from the disk using [Link]() function

18
# Showing the original image using matplotlib library function [Link]() img =
[Link]('[Link]')
[Link](img)
[Link]()
# Kernel for box blur filter
# It is a unity matrix which is divided by 9
box_blur_ker = [Link]([[0.1111111, 0.1111111, 0.1111111],
[0.1111111, 0.1111111, 0.1111111],
[0.1111111, 0.1111111, 0.1111111]])
# Applying Box Blur effect
# Using the cv2.filter2D() function
# src is the source of image(here, img)
# ddepth is destination depth. -1 will mean output image will have same depth as input image
# kernel is used for specifying the kernel operation (here, box_blur_ker) Box_blur =
cv2.filter2D(src=img, ddepth=-1, kernel=box_blur_ker)
# Showing the box blur image using matplotlib library function [Link]()
[Link](Box_blur)
[Link]()

Output:

e) Gradients
Code:
import numpy as np
import cv2 as cv

19
from matplotlib import pyplot as plt
img = [Link]('[Link]', cv.IMREAD_GRAYSCALE)
assert img is not None, "file could not be read, check with [Link]()" laplacian
= [Link](img,cv.CV_64F)
sobelx = [Link](img,cv.CV_64F,1,0,ksize=5)
sobely = [Link](img,cv.CV_64F,0,1,ksize=5)
[Link](2,2,1),[Link](img,cmap = 'gray')
[Link]('Original'), [Link]([]), [Link]([])
[Link](2,2,2),[Link](laplacian,cmap = 'gray')
[Link]('Laplacian'), [Link]([]), [Link]([])
[Link](2,2,3),[Link](sobelx,cmap = 'gray')
[Link]('Sobel X'), [Link]([]), [Link]([])
[Link](2,2,4),[Link](sobely,cmap = 'gray')
[Link]('Sobel Y'), [Link]([]), [Link]([]) [Link]()

Output:

f) Edge Detection
Code:
import cv2
import numpy as np
import [Link] as plt #
read the image
image = [Link]("little_flower.jpg") #
convert it to grayscale
gray = [Link](image, cv2.COLOR_BGR2GRAY) #
show the grayscale image

20
[Link](gray, cmap="gray")
[Link]()
# perform the canny edge detector to detect image edges edges
= [Link](gray, threshold1=30, threshold2=100)
Input Image:

Output Image:

Result:
Thus, implementation of image enhancement for various techniques was successfully
verified.

21
Image Features and Image Alignment – Image transforms – Fourier, Hough,
Ex. No 5 Extract ORB Image features, Feature matching, cloning,
Date: Feature matching-based image alignment

Aim: To implement Image Features and Image Alignment for various techniques.

a) Image Transforms

Code: Translation
import numpy as np
import cv2 as cv
img = [Link]('[Link]', 0)
rows, cols = [Link]
M = np.float32([[1, 0, 100], [0, 1, 50]])
dst = [Link](img, M, (cols, rows))
[Link]('img', dst)
[Link](0)
[Link]()

Output:

Code: Reflection
import numpy as np
import cv2 as cv
img = [Link]('[Link]', 0)
rows, cols = [Link]
M = np.float32([[1, 0, 0],
[0, -1, rows],
[0, 0, 1]])
reflected_img = [Link](img, M,

22
(int(cols),
int(rows)))
[Link]('img', reflected_img)
[Link]('reflection_out.jpg', reflected_img)
[Link](0)
[Link]()

Output

Code: Rotation
import numpy as np
import cv2 as cv
img = [Link]('[Link]', 0)
rows, cols = [Link]
M = np.float32([[1, 0, 0], [0, -1, rows], [0, 0, 1]])
img_rotation = [Link](img,
cv.getRotationMatrix2D((cols/2, rows/2),
30, 0.6),
(cols, rows))
[Link]('img', img_rotation)
[Link]('rotation_out.jpg', img_rotation)
[Link](0)
[Link]()
Output:

Code: Scaling
import numpy as np
import cv2 as cv

23
img = [Link]('[Link]', 0)
rows, cols = [Link]
img_shrinked = [Link](img, (250, 200),
interpolation=cv.INTER_AREA)
[Link]('img', img_shrinked)
img_enlarged = [Link](img_shrinked, None,
fx=1.5, fy=1.5,
interpolation=cv.INTER_CUBIC)
[Link]('img', img_enlarged)
[Link](0)
[Link]()

Output

Code: Shearing in X axis


import numpy as np import
cv2 as cv
img = [Link]('[Link]', 0)
rows, cols = [Link]
M = np.float32([[1, 0.5, 0], [0, 1, 0], [0, 0, 1]])
sheared_img = [Link](img, M, (int(cols*1.5), int(rows*1.5)))
[Link]('img', sheared_img)
[Link](0)
[Link]()
Output:

24
Code: Shearing in Y axis
import numpy as np import
cv2 as cv
img = [Link]('[Link]', 0)
rows, cols = [Link]
M = np.float32([[1, 0, 0], [0.5, 1, 0], [0, 0, 1]])
sheared_img = [Link](img, M, (int(cols*1.5), int(rows*1.5)))
[Link]('sheared_y-axis_out.jpg', sheared_img)
[Link](0)
[Link]()

Output:

b) Extract ORB Image features


Code:
# import required libraries
import cv2
# read input image
img = [Link]('[Link]')
# convert the image to grayscale
gray = [Link](img, cv2.COLOR_BGR2GRAY) #
Initiate ORB object with default values
orb = cv2.ORB_create(nfeatures=2000)
# detect and compute the keypoints on image (grayscale) kp =
[Link](gray, None)
kp, des = [Link](gray, kp) #
draw keypoints in image
img1 = [Link](gray, kp, None, (0,0,255), flags=0) #
display the image with keypoints drawn on it [Link]("ORB

25
Keypoints", img1)
[Link](0)
[Link]()

Input:

Output:

c) Feature matching
Code:
# Importing the libraries
import cv2

# Reading the image and converting into B/W image


= [Link]('[Link]')
gray_image = [Link](image, cv2.COLOR_BGR2GRAY) #
Applying the function
fast = cv2.FastFeatureDetector_create()
[Link](False)
# Drawing the keypoints
kp = [Link](gray_image, None)
kp_image = [Link](image, kp, None, color=(0, 255, 0))

26
[Link]('FAST', kp_image)
[Link]()

Input:

Output:

d) Cloning
Code:
# Standard imports import
cv2
import numpy as np #
Read images
src = [Link]("images/[Link]") dst
= [Link]("images/[Link]")
# Create a rough mask around the airplane.

27
src_mask = [Link]([Link], [Link])
poly = [Link]([ [4,80], [30,54], [151,63], [254,37], [298,90], [272,134], [43,122] ],
np.int32)
[Link](src_mask, [poly], (255, 255, 255))
# This is where the CENTER of the airplane will be placed
center = (800,100)
# Clone seamlessly.
output = [Link](src, dst, src_mask, center, cv2.NORMAL_CLONE) #
Save result
[Link]("images/[Link]", output);
Output:

Result:
Thus, implementation of image features and alignment for various techniques was
successfully verified.

28
Ex. No 6
Image segmentation using Graphcut / Grabcut
Date:

Aim: To implement Image Segmentation using Graphcut technique.


Procedure:
image: Input 8-bit 3-channel image.
mask: Input/output 8-bit single-channel mask. The mask is initialized by the function when
mode is set to GC_INIT_WITH_RECT. Its elements may have one of following values:
GC_BGD defines an obvious background pixels.
GC_FGD defines an obvious foreground (object) pixel.
GC_PR_BGD defines a possible background pixel.
GC_PR_FGD defines a possible foreground pixel.
rectangle: It is the region of interest containing a segmented object. The pixels outside of the
ROI are marked as obvious background. The parameter is only used when
mode==GC_INIT_WITH_RECT.
backgroundModel: Temporary array for the background model.
foregroundModel: Temporary array for the foreground model.
iterationCount: Number of iterations the algorithm should make before returning the result.
Note that the result can be refined with further calls with mode==GC_INIT_WITH_MASK or
mode==GC_EVAL.
mode: It defines the Operation mode. It can be one of the following:
GC_INIT_WITH_RECT: The function initializes the state and the mask using the provided
rectangle. After that it runs iterCount iterations of the algorithm.
GC_INIT_WITH_MASK: The function initializes the state using the provided mask. Note that
GC_INIT_WITH_RECT and GC_INIT_WITH_MASK can be combined. Then, all the pixels
outside of the ROI are automatically initialized with GC_BGD.
GC_EVAL: The value means that the algorithm should just resume.
Code:
# Python program to illustrate #
foreground extraction using #
GrabCut algorithm
# organize imports
import numpy as np
import cv2

29
from matplotlib import pyplot as plt #
path to input image specified and
# image is loaded with imread command
image = [Link]('[Link]')
# create a simple mask image similar #
to the loaded image, with the
# shape and return type
mask = [Link]([Link][:2], np.uint8)
# specify the background and foreground model #
using numpy the array is constructed of 1 row #
and 65 columns, and all array elements are 0 #
Data type for the array is np.float64 (default)

backgroundModel = [Link]((1, 65), np.float64)


foregroundModel = [Link]((1, 65), np.float64) #
define the Region of Interest (ROI)
# as the coordinates of the rectangle #
where the values are entered as
# (startingPoint_x, startingPoint_y, width, height)
# these coordinates are according to the input image # it
may vary for different images
rectangle = (20, 100, 150, 150)
# apply the grabcut algorithm with appropriate #
values as parameters, number of iterations = 3 #
cv2.GC_INIT_WITH_RECT is used because # of
the rectangle mode is used [Link](image,
mask, rectangle,
backgroundModel, foregroundModel, 3,
cv2.GC_INIT_WITH_RECT)

# In the new mask image, pixels will #


be marked with four flags
# four flags denote the background / foreground #
mask is changed, all the 0 and 2 pixels
# are converted to the background

30
# mask is changed, all the 1 and 3 pixels #
are now the part of the foreground
# the return type is also mentioned, #
this gives us the final mask
mask2 = [Link]((mask == 2)l(mask == 0), 0,
1).astype('uint8') # The final mask is multiplied with #
the input image to give the segmented image. image =
image * mask2[:, :, [Link]]
# output segmented image with colorbar
[Link](image)
[Link]()
[Link]()

Input:

Output:

Result:
Thus, implementation of image segmentation using Graphcut Algorithm is verified
successfully.

31
Ex. No 7
Camera Calibration with circular grid
Date:

Aim: To implement Camera Calibration with Circular grid using Open CV in python.
Procedure:
Step 1: First define real world coordinates of 3D points using known size of checkerboard
pattern.
Step 2: Different viewpoints of check-board image is captured.
Step 3: findChessboardCorners() is a method in OpenCV and used to find pixel coordinates (u,
v) for each 3D point in different images
Step 4: Then calibrateCamera() method is used to find camera parameters.
Code:
# Import required modules
import cv2
import numpy as np
import os
import glob
# Define the dimensions of checkerboard CHECKERBOARD =
(6, 9)
# stop the iteration when specified #
accuracy, epsilon, is reached or
# specified number of iterations are completed. criteria =
(cv2.TERM_CRITERIA_EPS +
cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# Vector for 3D points
threedpoints = []
# Vector for 2D points
twodpoints = []
# 3D points real world coordinates
objectp3d = [Link]((1, CHECKERBOARD[0]
* CHECKERBOARD[1],
3), np.float32)
objectp3d[0, :, :2] = [Link][0:CHECKERBOARD[0],
0:CHECKERBOARD[1]].[Link](-1, 2)

32
prev_img_shape = None
# Extracting path of individual image stored #
in a given directory. Since no path is
# specified, it will take current directory #
jpg files alone
images = [Link]('*.jpg')
for filename in images:
image = [Link](filename)
grayColor = [Link](image, cv2.COLOR_BGR2GRAY) #
Find the chess board corners
# If desired number of corners are

# found in the image then ret = true ret,


corners =
[Link]( grayColor,
CHECKERBOARD,
cv2.CALIB_CB_ADAPTIVE_THRESH
+ cv2.CALIB_CB_FAST_CHECK +
cv2.CALIB_CB_NORMALIZE_IMAGE)
# If desired number of corners can be detected then, #
refine the pixel coordinates and display
# them on the images of checker board if
ret == True:
[Link](objectp3d) #
Refining pixel coordinates
# for given 2d points. corners2
= [Link](
grayColor, corners, (11, 11), (-1, -1), criteria)
[Link](corners2)
# Draw and display the corners
image = [Link](image,
CHECKERBOARD,
corners2, ret)

[Link]('img', image)
[Link](0)

33
[Link]() h,
w = [Link][:2]
# Perform camera calibration by
# passing the value of above found out 3D points (threedpoints) #
and its corresponding pixel coordinates of the
# detected corners (twodpoints)
ret, matrix, distortion, r_vecs, t_vecs =
[Link]( threedpoints, twodpoints,
[Link][::-1], None, None)
# Displaying required output print("
Camera matrix:") print(matrix)
print("\n Distortion coefficient:")
print(distortion)
print("\n Rotation Vectors:")
print(r_vecs)
print("\n Translation Vectors:")
print(t_vecs)

Input:

Output:
Camera matrix:
[[ 36.26378216 0. 125.68539168]
[ 0. 36.76607372 142.49821147]
[ 0. 0. 1. ]]

34
Distortion coefficient:
[[-1.25491812e-03 9.89269357e-05 -2.89077718e-03 4.52760939e-04
-3.29964245e-06]]

Rotation Vectors:
[array([[-0.05767492],
[ 0.03549497],
[ 1.50906953]]), array([[-0.09301982],
[-0.01034321],
[ 3.07733805]]), array([[-0.02175332],[ 0.05611105],
[-0.07308161]])]

Translation Vectors:
[array([[ 4.63047351],
[-3.74281386],
[ 1.64238108]]), array([[2.31648737],
[3.98801521],
[1.64584622]]), array([[-3.17548808],
[-3.46022466],
[ 1.68200157]])]

Result:
Thus, implementation of Camera Calibration with Circular Grid is verified successfully.

35
Ex. No 8 Pose
Date: Estimation

Aim: To Implement Pose estimation for the Video.


Code:
!pip install opencv-python
!pip install mediapipe import
cv2
import mediapipe as mp ##
initialize pose estimator
mp_drawing = [Link].drawing_utils
mp_pose = [Link]
pose = mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) cap =
[Link]('test_video.mp4')
while [Link](): #
read frame
_, frame = [Link]()
try:
# resize the frame for portrait video
frame = [Link](frame, (350, 600)) #
convert to RGB
frame_rgb = [Link](frame, cv2.COLOR_BGR2RGB)

# process the frame for pose detection


pose_results = [Link](frame_rgb) #
print(pose_results.pose_landmarks)

# draw skeleton on the frame


mp_drawing.draw_landmarks(frame, pose_results.pose_landmarks,
mp_pose.POSE_CONNECTIONS)
# display the frame
[Link]('Output', frame)
except:
break

36
if [Link](1) == ord('q'):
break

[Link]()
[Link]()
# get landmark for a specific point
pose_results.pose_landmarks.landmark[32]

Output:

Result:
Thus, implementation of Pose Estimation is verified successfully.

37
Ex. No 9
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 OpenCV and matplotlib libraries.
Read both left and right images.
Calculate disparity using [Link].
Code:
# import OpenCV and pyplot import
cv2 as cv
from matplotlib import pyplot as plt #
read left and right images
imgR = [Link]('[Link]', 0)
imgL = [Link]('[Link]', 0) #
creates StereoBm object
stereo = cv.StereoBM_create(numDisparities = 16, blockSize = 15) #
computes disparity
disparity = [Link](imgL, imgR) #
displays image as grayscale and plotted
[Link](disparity, 'gray')
[Link]()
Input:

Left Right

38
Output:

Result:
Thus, implementation of 3D Reconstruction creating depth map from stereo images is
verified successfully.

39
Ex. No 10
Object Detection and Tracking using Kalman Filter, Camshift
Date:

Aim: To implement Object detection and tracking using Kalman filter, Camshift using Open CV.
Code:

import numpy as np
import cv2 as cv
# Read the input video

cap =
[Link]('sample.mp4') #
take first frame of the
# video

ret, frame = [Link]()


# setup initial region of
# tracker
x, y, width, height = 400, 440, 150,
150 track_window = (x, y,width,
height) # set up the Region of
# Interest for tracking
roi = frame[y:y + height,
x : x + width]

# convert ROI from BGR to


# HSV format
hsv_roi = [Link](roi,
cv.COLOR_BGR2HSV) # perform masking
operation
mask = [Link](hsv_roi,

[Link]((0., 60., 32.)),

[Link]((180., 255., 255)))

roi_hist = [Link]([hsv_roi], [0], mask,

40
[180],

[0, 180])
[Link](roi_hist, roi_hist,

0, 255, cv.NORM_MINMAX)

# Setup the termination criteria,


# either 15 iteration or move by
# atleast 2 pt
term_crit = ( cv.TERM_CRITERIA_EPS l
cv.TERM_CRITERIA_COUNT, 15, 2)
while(1):

ret, frame = [Link]()

# Resize the video


frames. frame =
[Link](frame,
(720, 720),

fx = 0, fy = 0,

interpolation = cv.INTER_CUBIC)
[Link]('Original', frame)
# perform thresholding on
# the video frames
ret1, frame1 = [Link](frame,

180, 155,
cv.THRESH_TOZERO_INV)
# convert from BGR to
HSV # format.
hsv = [Link](frame1,

cv.COLOR_BGR2HSV)

41
dst = [Link]([hsv],
[0],
roi_hist,
[0, 180],
1)
# apply Camshift to get the new location

ret2, track_window = [Link](dst,

track_window,
term_crit)
# Draw it on image

pts =
[Link](ret2) #
convert from floating #
to integer
pts = np.int0(pts)

# Draw Tracking window on the


# video frame.
Result = [Link](frame,

[pts],
True,
(0, 255, 255),

2)

[Link]('Camshift',
Result) # set ESC key as the
# exit button.

k = [Link](30) &
0xff if k == 27:
break

# Release the cap object


[Link]()

42
# close all opened windows
[Link]()

Output:

Result:
Thus, implementation of Object detection and Tracking using Camshift is verified
successfully.

43

You might also like