Computer Vision- Lab Manual
Computer Vision- Lab Manual
LAB RECORD
LABORATORY RECORD
For
SEMESTER: V
SYLLABUS
SOFTWARE
OpenCV computer vision Library for OpenCV in Python / PyCharm or C++ / Visual Studio
or equivalent
HARDWARE
Standalone desktops – 30 Nos. (or) Server supporting 30 terminals or more
lOMoAR cPSD| 40491420
INDEX
SL.
NAME OF THE PROGRAM DATE MARKS SIGNATURE
NO.
1 OpenCV Installation and working with Python
Ex. No 1
Date:
OpenCV Installation 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:
6) Installing Packages:
1
lOMoAR cPSD| 40491420
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.
2
lOMoAR cPSD| 40491420
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:
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.
Coding:
import cv2
img = cv2.imread("geeksforgeeks.png", cv2.IMREAD_COLOR)
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output Image
b) Cropping Image
3
lOMoAR cPSD| 40491420
Coding:
import cv2
img = cv2.imread("test.jpeg")
print(type(img))
# [rows, columns]
crop = img[50:180, 100:300]
cv2.imshow('original', img)
cv2.imshow('cropped', crop)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output Image:
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 = cv2.imread(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 Mono.bmp", 1)
# Loading the image
4
lOMoAR cPSD| 40491420
interpolation = cv2.INTER_LINEAR)
for i in range(count):
plt.subplot(2, 2, i + 1)
plt.title(Titles[i])
plt.imshow(images[i])
plt.show()
Output:
d) Thresholding
5
lOMoAR cPSD| 40491420
Coding:
import cv2
import numpy as np
Input:
6
lOMoAR cPSD| 40491420
Output:
e) Contour Analysis
Code:
import cv2
import matplotlib.pyplot as plt
# read the image
image = cv2.imread("thumbs_up_down.jpg")
# convert to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# create a binary thresholded image
_, binary = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)
# show it
plt.imshow(binary, cmap="gray")
plt.show()
# find the contours from the thresholded image
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
# draw all contours
image = cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
# show the image with the drawn contours
plt.imshow(image)
plt.show()
7
lOMoAR cPSD| 40491420
Input:
Output:
f) Bolb detection
Code:
import cv2
import numpy as np
# Load image
image = cv2.imread('C://gfg//images//blobs.jpg', 0)
8
lOMoAR cPSD| 40491420
# Detect blobs
keypoints = detector.detect(image)
number_of_blobs = len(keypoints)
text = "Number of Circular Blobs: " + str(len(keypoints))
cv2.putText(blobs, text, (20, 550),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 100, 255), 2)
# Show blobs
cv2.imshow("Filtering Circular Blobs Only", blobs)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
Result:
Thus, the Basic image processing techniques were implemented successfully.
9
lOMoAR cPSD| 40491420
Aim: To implement Image Annotation for lines, Text Circle, rectangle, ellipse on images.
Code:
# Import dependencies
import cv2
# Read Images
img = cv2.imread('sample.jpg')
# Display Image
cv2.imshow('Original Image',img)
cv2.waitKey(0)
# Print error message if image is null
if img is None:
print('Could not read image')
# Draw line on image
imageLine = img.copy()
Draw the image from point A to B
pointA = (200,80)
pointB = (450,80)
cv2.line(imageLine, pointA, pointB, (255, 255, 0), thickness=3, lineType=cv2.LINE_AA)
cv2.imshow('Image Line', imageLine)
cv2.waitKey(0)
# Make a copy of image
imageCircle = img.copy()
# define the center of circle
circle_center = (415,190)
# define the radius of the circle
radius =100
# Draw a circle using the circle() Function
cv2.circle(imageCircle, circle_center, radius, (0, 0, 255), thickness=3,
lineType=cv2.LINE_AA)
# Display the result
cv2.imshow("Image Circle",imageCircle)
cv2.waitKey(0)
# make a copy of the original image
imageFilledCircle = img.copy()
# define center of the circle
circle_center = (415,190)
# define the radius of the circle
radius =100
# draw the filled circle on input image
cv2.circle(imageFilledCircle, circle_center, radius, (255, 0, 0), thickness=-1,
lineType=cv2.LINE_AA)
# display the output image
cv2.imshow('Image with Filled Circle',imageFilledCircle)
cv2.waitKey(0)
# make a copy of the original image
imageRectangle = img.copy()
10
lOMoAR cPSD| 40491420
11
lOMoAR cPSD| 40491420
Output:
Result:
Thus, the Image Annotation techniques were implemented successfully.
12
lOMoAR cPSD| 40491420
Aim: To implement Image Enhancement techniques for Color spaces, Histogram, image
Smoothing and Edge detection.
Code:
import cv2
image = cv2.imread('C://Users//Gfg//rgb.png')
B, G, R = cv2.split(image)
# Corresponding channels are separated
cv2.imshow("original", image)
cv2.waitKey(0)
cv2.imshow("blue", B)
cv2.waitKey(0)
cv2.imshow("Green", G)
cv2.waitKey(0)
cv2.imshow("red", R)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
Code:
import cv2
# path
path = r'C:\Users\Administrator\Desktop\geeks.png'
# Reading an image in default mode
13
lOMoAR cPSD| 40491420
src = cv2.imread(path)
# Window name in which image is displayed
window_name = 'Image'
# Using cv2.cvtColor() method
# Using cv2.COLOR_BGR2GRAY color space
# conversion code
image = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY )
# Displaying the image
cv2.imshow(window_name, image)
Input Image:
Output Image:
c) Histogram Equalization
Code:
# import Opencv
import cv2
# import Numpy
import numpy as np
# read a image using imread
img = cv2.imread(\'F:\\do_nawab.png\', 0)
# creating a Histograms Equalization
# of a image using cv2.equalizeHist()
equ = cv2.equalizeHist(img)
# stacking images side-by-side
res = np.hstack((img, equ))
# show image input vs output
cv2.imshow(\'image\', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
14
lOMoAR cPSD| 40491420
Input Image:
Output Image:
d) Convolution
Code:
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Reading the image from the disk using cv2.imread() function
# Showing the original image using matplotlib library function plt.imshow()
img = cv2.imread('geeksforgeeks.png')
plt.imshow(img)
plt.show()
# Kernel for box blur filter
# It is a unity matrix which is divided by 9
box_blur_ker = np.array([[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 plt.imshow()
plt.imshow(Box_blur)
plt.show()
15
lOMoAR cPSD| 40491420
Output:
e) Gradients
Code:
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('dave.jpg', cv.IMREAD_GRAYSCALE)
assert img is not None, "file could not be read, check with os.path.exists()"
laplacian = cv.Laplacian(img,cv.CV_64F)
sobelx = cv.Sobel(img,cv.CV_64F,1,0,ksize=5)
sobely = cv.Sobel(img,cv.CV_64F,0,1,ksize=5)
plt.subplot(2,2,1),plt.imshow(img,cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,2),plt.imshow(laplacian,cmap = 'gray')
plt.title('Laplacian'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,3),plt.imshow(sobelx,cmap = 'gray')
plt.title('Sobel X'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,4),plt.imshow(sobely,cmap = 'gray')
plt.title('Sobel Y'), plt.xticks([]), plt.yticks([])
plt.show()
Output:
16
lOMoAR cPSD| 40491420
f) Edge Detection
Code:
import cv2
import numpy as np
import matplotlib.pyplot as plt
# read the image
image = cv2.imread("little_flower.jpg")
# convert it to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# show the grayscale image
plt.imshow(gray, cmap="gray")
plt.show()
# perform the canny edge detector to detect image edges
edges = cv2.Canny(gray, threshold1=30, threshold2=100)
Input Image:
Output Image:
Result:
Thus, implementation of image enhancement for various techniques was successfully
verified.
17
lOMoAR cPSD| 40491420
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 = cv.imread('girlImage.jpg', 0)
rows, cols = img.shape
M = np.float32([[1, 0, 100], [0, 1, 50]])
dst = cv.warpAffine(img, M, (cols, rows))
cv.imshow('img', dst)
cv.waitKey(0)
cv.destroyAllWindows()
Output:
Code: Reflection
import numpy as np
import cv2 as cv
img = cv.imread('girlImage.jpg', 0)
rows, cols = img.shape
M = np.float32([[1, 0, 0],
[0, -1, rows],
[0, 0, 1]])
reflected_img = cv.warpPerspective(img, M,
(int(cols),
int(rows)))
cv.imshow('img', reflected_img)
cv.imwrite('reflection_out.jpg', reflected_img)
cv.waitKey(0)
cv.destroyAllWindows()
18
lOMoAR cPSD| 40491420
Output:
Code: Rotation
import numpy as np
import cv2 as cv
img = cv.imread('girlImage.jpg', 0)
rows, cols = img.shape
M = np.float32([[1, 0, 0], [0, -1, rows], [0, 0, 1]])
img_rotation = cv.warpAffine(img,
cv.getRotationMatrix2D((cols/2, rows/2),
30, 0.6),
(cols, rows))
cv.imshow('img', img_rotation)
cv.imwrite('rotation_out.jpg', img_rotation)
cv.waitKey(0)
cv.destroyAllWindows()
Output:
Code: Scaling
import numpy as np
import cv2 as cv
img = cv.imread('girlImage.jpg', 0)
rows, cols = img.shape
img_shrinked = cv.resize(img, (250, 200),
interpolation=cv.INTER_AREA)
cv.imshow('img', img_shrinked)
img_enlarged = cv.resize(img_shrinked, None,
fx=1.5, fy=1.5,
interpolation=cv.INTER_CUBIC)
cv.imshow('img', img_enlarged)
cv.waitKey(0)
cv.destroyAllWindows()
19
Output:
Output:
Output:
20
lOMoAR cPSD| 40491420
Code:
# import required libraries
import cv2
# read input image
img = cv2.imread('house.jpg')
# convert the image to grayscale
gray = cv2.cvtColor(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 = orb.detect(gray, None)
kp, des = orb.compute(gray, kp)
# draw keypoints in image
img1 = cv2.drawKeypoints(gray, kp, None, (0,0,255), flags=0)
# display the image with keypoints drawn on it
cv2.imshow("ORB Keypoints", img1)
cv2.waitKey(0)
cv2.destroyAllWindows()
Input:
Output:
c) Feature matching
Code:
# Importing the libraries
import cv2
21
lOMoAR cPSD| 40491420
Input:
Output:
d) Cloning
Code:
# Standard imports
import cv2
import numpy as np
# Read images
src = cv2.imread("images/airplane.jpg")
dst = cv2.imread("images/sky.jpg")
# Create a rough mask around the airplane.
22
lOMoAR cPSD| 40491420
Output:
Result:
Thus, implementation of image features and alignment for various techniques was
successfully verified.
23
lOMoAR cPSD| 40491420
Ex. No 6
Date:
Image segmentation using Graphcut / Grabcut
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
from matplotlib import pyplot as plt
# path to input image specified and
# image is loaded with imread command
image = cv2.imread('image.jpg')
# create a simple mask image similar
# to the loaded image, with the
# shape and return type
mask = np.zeros(image.shape[: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)
24
lOMoAR cPSD| 40491420
Input:
25
lOMoAR cPSD| 40491420
Output:
Result:
Thus, implementation of image segmentation using Graphcut Algorithm is verified
successfully.
26
lOMoAR cPSD| 40491420
Ex. No 7
Date:
Camera Calibration with circular grid
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 = np.zeros((1, CHECKERBOARD[0]
* CHECKERBOARD[1],
3), np.float32)
objectp3d[0, :, :2] = np.mgrid[0:CHECKERBOARD[0],
0:CHECKERBOARD[1]].T.reshape(-1, 2)
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 = glob.glob('*.jpg')
27
lOMoAR cPSD| 40491420
twodpoints.append(corners2)
# Draw and display the corners
image = cv2.drawChessboardCorners(image,
CHECKERBOARD,
corners2, ret)
cv2.imshow('img', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
h, w = image.shape[: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 = cv2.calibrateCamera(
threedpoints, twodpoints, grayColor.shape[::-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)
28
lOMoAR cPSD| 40491420
Input:
Output:
Camera matrix:
[[ 36.26378216 0. 125.68539168]
[ 0. 36.76607372 142.49821147]
[ 0. 0. 1. ]]
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],
29
lOMoAR cPSD| 40491420
[ 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.
30
lOMoAR cPSD| 40491420
Ex. No 8
Date:
Pose Estimation
Code:
!pip install opencv-python
!pip install mediapipe
import cv2
import mediapipe as mp
## initialize pose estimator
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
pose = mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5)
cap = cv2.VideoCapture('test_video.mp4')
while cap.isOpened():
# read frame
_, frame = cap.read()
try:
# resize the frame for portrait video
frame = cv2.resize(frame, (350, 600))
# convert to RGB
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# get landmark for a specific point
pose_results.pose_landmarks.landmark[32]
31
lOMoAR cPSD| 40491420
Output:
Result:
Thus, implementation of Pose Estimation is verified successfully.
32
lOMoAR cPSD| 40491420
Ex. No 9
Date:
3D Reconstruction – Creating Depth map from stereo images
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 stereo.compute.
Code:
# import OpenCV and pyplot
import cv2 as cv
from matplotlib import pyplot as plt
# read left and right images
imgR = cv.imread('right.png', 0)
imgL = cv.imread('left.png', 0)
# creates StereoBm object
stereo = cv.StereoBM_create(numDisparities = 16, blockSize = 15)
# computes disparity
disparity = stereo.compute(imgL, imgR)
# displays image as grayscale and plotted
plt.imshow(disparity, 'gray')
plt.show()
Input:
Left Right
33
lOMoAR cPSD| 40491420
Output:
Result:
Thus, implementation of 3D Reconstruction creating depth map from stereo images is
verified successfully.
34
lOMoAR cPSD| 40491420
Ex. No 10
Date:
Object Detection and Tracking using Kalman Filter, Camshift
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 = cv.VideoCapture('sample.mp4')
# take first frame of the
# video
ret, frame = cap.read()
# 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 = cv.cvtColor(roi,
cv.COLOR_BGR2HSV)
# perform masking operation
mask = cv.inRange(hsv_roi,
np.array((0., 60., 32.)),
np.array((180., 255., 255)))
roi_hist = cv.calcHist([hsv_roi],
[0], mask,
35
lOMoAR cPSD| 40491420
[180],
[0, 180])
cv.normalize(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 = cap.read()
# Resize the video frames.
frame = cv.resize(frame,
(720, 720),
fx = 0, fy = 0,
interpolation = cv.INTER_CUBIC)
cv.imshow('Original', frame)
# perform thresholding on
# the video frames
ret1, frame1 = cv.threshold(frame,
180, 155,
cv.THRESH_TOZERO_INV)
# convert from BGR to HSV
# format.
hsv = cv.cvtColor(frame1,
cv.COLOR_BGR2HSV)
dst = cv.calcBackProject([hsv],
[0],
roi_hist,
[0, 180], 1)
# apply Camshift to get the
36
lOMoAR cPSD| 40491420
# new location
ret2, track_window = cv.CamShift(dst,
track_window,
term_crit)
# Draw it on image
pts = cv.boxPoints(ret2)
# convert from floating
# to integer
pts = np.int0(pts)
# Draw Tracking window on the
# video frame.
Result = cv.polylines(frame,
[pts],
True,
(0, 255, 255),
2)
cv.imshow('Camshift', Result)
# set ESC key as the
# exit button.
k = cv.waitKey(30) & 0xff
if k == 27:
break
37
lOMoAR cPSD| 40491420
Output:
Result:
Thus, implementation of Object detection and Tracking using Camshift sssis verified
successfully.
38