Computer Vision Laboratory Manual
Computer Vision Laboratory Manual
REGULATION R-2021
LABORATORY MANUAL
Prepared by
[Link]
Assistant Professor
Rajapalayam
CCS338 COMPUTER VISION LT P C
202 3
COURSE OBJECTIVES:
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:
2
Ex. No 1
OpenCV Installation and working with Python
Date:
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:
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:
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]')
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
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]()
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.
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
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:
25
Keypoints", img1)
[Link](0)
[Link]()
Input:
Output:
c) Feature matching
Code:
# Importing the libraries
import cv2
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:
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)
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
[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
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
40
[180],
[0, 180])
[Link](roi_hist, roi_hist,
0, 255, cv.NORM_MINMAX)
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
track_window,
term_crit)
# Draw it on image
pts =
[Link](ret2) #
convert from floating #
to integer
pts = np.int0(pts)
[pts],
True,
(0, 255, 255),
2)
[Link]('Camshift',
Result) # set ESC key as the
# exit button.
k = [Link](30) &
0xff if k == 27:
break
42
# close all opened windows
[Link]()
Output:
Result:
Thus, implementation of Object detection and Tracking using Camshift is verified
successfully.
43