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

Computer Vision- Lab Manual

The document is a laboratory record for the Computer Vision Laboratory course (CCS338) for B.Tech Information Technology at Anna University. It outlines the syllabus, experiments, and procedures for various image processing tasks using OpenCV, including installation, basic image processing, image annotation, and object detection. The record includes sections for student details, experiment dates, marks, and signatures from internal and external examiners.

Uploaded by

nainudigitals
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)
3 views43 pages

Computer Vision- Lab Manual

The document is a laboratory record for the Computer Vision Laboratory course (CCS338) for B.Tech Information Technology at Anna University. It outlines the syllabus, experiments, and procedures for various image processing tasks using OpenCV, including installation, basic image processing, image annotation, and object detection. The record includes sections for student details, experiment dates, marks, and signatures from internal and external examiners.

Uploaded by

nainudigitals
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
You are on page 1/ 43

lOMoAR cPSD| 40491420

LAB RECORD

Name of the Student :


Register Number :
Subject code & Name :
Year / Sem :
Department :
Academic Year :
lOMoAR cPSD| 40491420

This is to certify that Mr./Ms…………………………………………………..


Of …………year……….Semester B.E./ B.Tech (……………………..............
……………………………………………………………………….) is a Bonafide
student bearing the Reg.No…………………………………………
record of Work done in the …………………………………………………….
………………………………………………………………… lab during the
Academic year 20 -20

Faculty in-charge Head of the Department

Submitted for the Practical Examination held on .....................

Internal Examiner External Examiner


lOMoAR cPSD| 40491420

LABORATORY RECORD
For

CCS338– COMPUTER VISION LABORATORY


Of

BTECH INFORMATION TECHNOLOGY

(Anna University Regulation 2021)

SEMESTER: V

ACADEMIC YEAR: 2024-25

DEPARTMENT OF INFORMATION TECHNOLOGY


CHENNAI – 603103
lOMoAR cPSD| 40491420

SYLLABUS

CCS338 – COMPUTER VISION

LABORATORY LIST OF EXPERIMENTS

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

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

Basic Image Processing - loading images, Cropping,


2
Resizing, Thresholding, Contour analysis, Bolb

Image Annotation – Drawing lines, text circle,


3 rectangle, ellipse on images

4 Image Enhancement - Understanding Color spaces,


color space conversion, Histogram equalization,
Convolution, Image smoothing, Gradients, Edge

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

Object Detection and Tracking using Kalman Filter,


10
Camshift
lOMoAR cPSD| 40491420

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

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:

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

Ex. No 2 Basic Image Processing - loading images, Cropping, Resizing,


Date: Thresholding, Contour analysis, Bolb detection

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.

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

Steps to Crop the image


Step 1: Read the image
Step 2: Get the image Dimensions
Step 3: Slice the image

3
lOMoAR cPSD| 40491420

Coding:
import cv2

img = cv2.imread("test.jpeg")
print(type(img))

# Shape of the image


print("Shape of the image", img.shape)

# [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

half = cv2.resize(image, (0, 0), fx = 0.1, fy = 0.1)


bigger = cv2.resize(image, (1050, 1610))

stretch_near = cv2.resize(image, (780, 540),

4
lOMoAR cPSD| 40491420

interpolation = cv2.INTER_LINEAR)

Titles =["Original", "Half", "Bigger", "Interpolation Nearest"]


images =[image, half, bigger, stretch_near]
count = 4

for i in range(count):
plt.subplot(2, 2, i + 1)
plt.title(Titles[i])
plt.imshow(images[i])

plt.show()

Output:

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.

5
lOMoAR cPSD| 40491420

Coding:
import cv2
import numpy as np

# path to input image is specified and


# image is loaded with imread command
image1 = cv2.imread('input1.jpg')

# cv2.cvtColor is applied over the


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

# applying different thresholding


# techniques on the input image
# all pixels value above 120 will
# be set to 255
ret, thresh1 = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY_INV)
ret, thresh3 = cv2.threshold(img, 120, 255, cv2.THRESH_TRUNC)
ret, thresh4 = cv2.threshold(img, 120, 255, cv2.THRESH_TOZERO)
ret, thresh5 = cv2.threshold(img, 120, 255, cv2.THRESH_TOZERO_INV)

# the window showing output images


# with the corresponding thresholding
# techniques applied to the input images
cv2.imshow('Binary Threshold', thresh1)
cv2.imshow('Binary Threshold Inverted', thresh2)
cv2.imshow('Truncated Threshold', thresh3)
cv2.imshow('Set to 0', thresh4)
cv2.imshow('Set to 0 Inverted', thresh5)

# De-allocate any associated memory usage


if cv2.waitKey(0) & 0xff == 27:
cv2.destroyAllWindows()

Input:

6
lOMoAR cPSD| 40491420

Output:

e) 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 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)

# Set our filtering parameters


# Initialize parameter setting using cv2.SimpleBlobDetector
params = cv2.SimpleBlobDetector_Params()

# Set Area filtering parameters


params.filterByArea = True
params.minArea = 100

# Set Circularity filtering parameters


params.filterByCircularity = True
params.minCircularity = 0.9

# Set Convexity filtering parameters


params.filterByConvexity = True
params.minConvexity = 0.2

8
lOMoAR cPSD| 40491420

# Set inertia filtering parameters


params.filterByInertia = True
params.minInertiaRatio = 0.01

# Create a detector with the parameters


detector = cv2.SimpleBlobDetector_create(params)

# Detect blobs
keypoints = detector.detect(image)

# Draw blobs on our image as red circles


blank = np.zeros((1, 1))
blobs = cv2.drawKeypoints(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))
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

Ex. No 3 Image Annotation – Drawing lines, text circle, rectangle, ellipse on


Date: images

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

# define the starting and end points of the rectangle


start_point =(300,115)
end_point =(475,225)
# draw the rectangle
cv2.rectangle(imageRectangle, start_point, end_point, (0, 0, 255), thickness= 3,
lineType=cv2.LINE_8)
# display the output
cv2.imshow('imageRectangle', imageRectangle)
cv2.waitKey(0)
# make a copy of the original image
imageEllipse = img.copy()
# define the center point of ellipse
ellipse_center = (415,190)
# define the major and minor axes of the ellipse
axis1 = (100,50)
axis2 = (125,50)
# draw the ellipse
#Horizontal
cv2.ellipse(imageEllipse, center, axis1, 0, 0, 360, (255, 0, 0), thickness=3)
#Vertical
cv2.ellipse(imageEllipse, center, axis2, 90, 0, 360, (0, 0, 255), thickness=3)
# display the output
cv2.imshow('ellipse Image',imageEllipse)
cv2.waitKey(0)
# make a copy of the original image
imageText = img.copy()
#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
cv2.putText(imageText, text, org, fontFace = cv2.FONT_HERSHEY_COMPLEX,
fontScale = 1.5, color = (250,225,100))))
# display the output image with text over it
cv2.imshow("Image Text",imageText)
cv2.waitKey(0)
cv2.destroyAllWindows()

11
lOMoAR cPSD| 40491420

Output:

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

12
lOMoAR cPSD| 40491420

Ex. No 4 Image Enhancement - Understanding Color spaces, color space


Date: conversion, Histogram equalization, Convolution, Image smoothing,
Gradients, Edge Detection

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

a) Understanding Color Spaces

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:

b) Color Space Conversion

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

Ex. No 5 Image Features and Image Alignment – Image transforms – Fourier,


Date: Hough, Extract ORB Image features, Feature matching, cloning,
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 = 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

Downloaded by Vimal MSAJCE (msajcevimal@gmail.com)


lOMoAR cPSD| 40491420

Output:

Code: Shearing in X axis


import numpy as np
import cv2 as cv
img = cv.imread('girlImage.jpg', 0)
rows, cols = img.shape
M = np.float32([[1, 0.5, 0], [0, 1, 0], [0, 0, 1]])
sheared_img = cv.warpPerspective(img, M, (int(cols*1.5), int(rows*1.5)))
cv.imshow('img', sheared_img)
cv.waitKey(0)
cv.destroyAllWindows()

Output:

Code: Shearing in Y axis


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.5, 1, 0], [0, 0, 1]])
sheared_img = cv.warpPerspective(img, M, (int(cols*1.5), int(rows*1.5)))
cv.imshow('sheared_y-axis_out.jpg', sheared_img)
cv.waitKey(0)
cv.destroyAllWindows()

Output:

20
lOMoAR cPSD| 40491420

b) Extract ORB Image features

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

# Reading the image and converting into B/W


image = cv2.imread('book.png')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Applying the function
fast = cv2.FastFeatureDetector_create()
fast.setNonmaxSuppression(False)
# Drawing the keypoints
kp = fast.detect(gray_image, None)
kp_image = cv2.drawKeypoints(image, kp, None, color=(0, 255, 0))
cv2.imshow('FAST', kp_image)
cv2.waitKey()

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

src_mask = np.zeros(src.shape, src.dtype)


poly = np.array([ [4,80], [30,54], [151,63], [254,37], [298,90], [272,134], [43,122] ],
np.int32)
cv2.fillPoly(src_mask, [poly], (255, 255, 255))
# This is where the CENTER of the airplane will be placed
center = (800,100)
# Clone seamlessly.
output = cv2.seamlessClone(src, dst, src_mask, center, cv2.NORMAL_CLONE)
# Save result
cv2.imwrite("images/opencv-seamless-cloning-example.jpg", output);

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

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
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

backgroundModel = np.zeros((1, 65), np.float64)


foregroundModel = np.zeros((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
cv2.grabCut(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
# 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 = np.where((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[:, :, np.newaxis]
# output segmented image with colorbar
plt.imshow(image)
plt.colorbar()
plt.show()

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')

for filename in images:


image = cv2.imread(filename)
grayColor = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Find the chess board corners
# If desired number of corners are

27
lOMoAR cPSD| 40491420

# found in the image then ret = true


ret, corners = cv2.findChessboardCorners(
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:
threedpoints.append(objectp3d)
# Refining pixel coordinates
# for given 2d points.
corners2 = cv2.cornerSubPix(
grayColor, corners, (11, 11), (-1, -1), criteria)

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

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 = 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)

# process the frame for pose detection


pose_results = pose.process(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
cv2.imshow('Output', frame)
except:
break

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

# Release the cap object


cap.release()
# close all opened windows
cv.destroyAllWindows()

37
lOMoAR cPSD| 40491420

Output:

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

38

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy