0% found this document useful (0 votes)
20 views67 pages

CV Lab Manual PDF

The document outlines a series of experiments related to OpenCV installation and image processing using Python. It includes detailed procedures for installing OpenCV, performing basic image processing tasks, image annotation, image enhancement, and more. Each experiment aims to familiarize users with various functionalities of OpenCV, such as loading images, drawing shapes, and applying image filters and transformations.

Uploaded by

swethashervin003
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)
20 views67 pages

CV Lab Manual PDF

The document outlines a series of experiments related to OpenCV installation and image processing using Python. It includes detailed procedures for installing OpenCV, performing basic image processing tasks, image annotation, image enhancement, and more. Each experiment aims to familiarize users with various functionalities of OpenCV, such as loading images, drawing shapes, and applying image filters and transformations.

Uploaded by

swethashervin003
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/ 67

INDEX

Page Faculty
SI.NO Date Name of the Experiment Marks
No Signature

10

11

12

13

14
15

16

17

18

19

20

21

22

23

24

25
EXP.NO 01
OPENCV INSTALLATION AND WORKING WITH PYTHON
DATE

AIM:

To study the installation and working procedure of OPENCV package on python.

TOOLS REQUIRED:

1. Computer with 32 bit or 64 bit Windows Operating system and 4GB RAM
2. Python3
3. OpenCV computer vision Library for Open CV in Python

OPENCV INSTALLATION PROCEDURE:

OpenCV Installation steps for Windows


1. Ensuring availability of necessary modules/packages
The first step to installing OpenCV is to ensure that your system has Python and pip
preinstalled. You can check if your system already contains Python through the following
command:
python --version
If Python is already installed in your system, the command prompt will output a message
displaying the current version of Python installed in your local device.

2. Downloading/checking version of pip


Pip is a package manager for Python used to install and manage software packages. It's a
tool that lets us install additional libraries and dependencies not distributed along with the standard
library. It connects to an online repository of public packages, Python Package Index.
To check if pip is already installed on your device, execute the following command on
the command prompt
pip -version

3. Updating the version of pip


We need to ensure that the pip version that we are using is up-to-date (19.3 is the
minimum supported version for OpenCV) by executing the following command:
pip install --upgrade pip

By default, pip installs Python packages to a system directory. This requires root access
and hence you may not be able to upgrade the version of pip using the above command. To
overcome that problem, we need to execute the following command:

pip install -upgrade pip -user

4. Downloading and Installing OpenCV

We can directly download and install OpenCV-python by using pip. If you have previously
installed a version of OpenCV, remove it before installation to avoid conflicts using the command:

pip uninstall opencv-python

To install OpenCV-python, execute the following code via the command prompt:

pip install opencv-python


I

Ensure that you select the correct package for your environment. Installing multiple packages
has several demerits and can cause conflicts. If we have installed multiple different packages in the
same environment, we need to uninstall them all with the command given above and reinstall only
one package.

5. Installing OpenCV-contrib

Contrib modules are additional modules that constantly under development and are often
used alongside the latest releases of OpenCV. Usually, some functions get transferred to and from
OpenCV-Python and OpenCV-contrib-python.
The following command is executed on the command prompt to install OpenCV -contrib-
python:

pip install opencv-contrib-python


I

By executing the above steps, we have successfully installed the latest version of OpenCV -python in
our machine for the Windows operating system.

RESULT:

Thus, the installation procedure of openCV on python was studied and installation process done
successfully.
EXP.NO 02 BASIC IMAGE PROCESSING - LOADING IMAGES,
CROPPING, RESIZING, THRESHOLDING, CONTOUR
DATE ANALYSIS, BOLB DETECTION

AIM:

To write a python program to implement the following Basic Image Processing operations

1. Loading images.
2. Cropping.
3. Resizing.
4. Thresholding.
5. Contour analysis.
6. Bolb detection.

TOOLS REQUIRED:

1. Computer with 32 bit or 64 bit Windows Operating system and 4GB RAM
2. Python3
3. OpenCV computer vision Library for Open CV in Python

ALGORITHM:

Step 1: Open a python Script file.

Step 2: Load the image.

Step 3: Perform Cropping, Resizing, Thresholding, Contour analysis and Bolb detection over the input image.

Step 4: visualize the image using open CV.

PROGRAM:

import cv2
import numpy as np
# Read an image
image = cv2.imread('image.jpg')

# Display the image


cv2.imshow('Image', image)
Outputl = cv2.imwrite('Input Image.png', image)
# Define the cropping region coordinates
x, y, width, height= 100, 100, 200, 200

# Crop the image


cropped_image = image[y:y+height, x:x+width]
# Display the original and cropped images
cv2.imshow('Cropped Image', cropped_image)
Output2 = cv2.imwrite('Cropped Image.jpg', image)
# Resize the image using different interpolation methods
resized_nearest = cv2.resize(image, (400, 300), interpolation=cv2.INTER_NEAREST)
resized_linear = cv2.resize(image, (400, 300), interpolation=cv2.INTER_LINEAR)
resized_cubic = cv2.resize(image, (400, 300), interpolation=cv2.INTER_CUBIC)

# Display the original and resized images


cv2.imshow('Original Image', image)
cv2.imshow('Resized Nearest', resized_nearest)
Output3 = cv2.imwrite('Resized Nearest.jpg', image)
cv2.imshow('Resized Linear', resized_linear)
Output4 = cv2.imwrite('Resized Linear.jpg', image)
cv2.imshow('Resized Cubic', resized_cubic)
Outputs = cv2.imwrite('Resized Cubic.jpg', image)

imagel = cv2.imread('image.jpg',0)

_, binary_threshold = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)


_, binary_inverse_threshold = cv2.threshold(image, 127,255, cv2.THRESH_BINARY_INV)
#_, adaptive_threshold = cv2.adaptiveThreshold(image, 255,
cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)

# Display the original and thresholded images


cv2.imshow('Original Image', image)
cv2.imshow('Binary Threshold', binary_threshold)
Output6 = cv2.imwrite('Binary Threshold.jpg', image)
cv2.imshow('Binary Inverse Threshold', binary_inverse_threshold)
Output7 = cv2.imwrite('Binary Inverse Threshold.jpg', image)
#cv2.imshow('Adaptive Threshold', adaptive_threshold)
# Apply a threshold to obtain a binary image
_, binary_image = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)

cv2.imshow('Binary image', binary_image)


Output7 = cv2.imwrite('Binary image.jpg', image)
binary_image1=cv2.cvtColor(binary_image, cv2.COLOR_BGR2GRA Y)
# Find contours in the binary image
contours,_= cv2.findContours(binary_imagel, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
# Draw the contours on a blank image
contour_image = cv2.drawContours(np.zeros_like(image), contours, -1, (0, 255, 0), 2)

# Display the original image and the contour image


cv2.imshow('Original Image', image)
cv2.imshow('Contour Image', contour_image)

# Read image
im = cv2.imread("image.jpg", cv2.IMREAD_GRAYSCALE)

# Set up the detector with default parameters.


detector = cv2.SimpleBlobDetector_create()

# Detect blobs.
keypoints = detector.detect(im)

# Draw detected blobs as red circles.


# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the
circle corresponds to the size of blob
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255),
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Show keypoints
cv2.imshow("Keypoints", im_with_keypoints)

cv2.waitKey(0)
cv2.destroy AllWindows()
# Resize the image
resized_image = cv2.resize(image, (400, 300))
RESULT:

Thus, the python program to implement the Cropping, Resizing, Thresholding, Contour analysis and Bolb
detection is executed successfully.
EXP.NO 03
IMAGE ANNOTATION - DRAWING LINES, TEXT CIRCLE,
RECTANGLE, ELLIPSE ON IMAGES
DATE

AIM:

To write a python program to implement the following Image annotation operations

1. Drawing Lines.
2. Text Circle.
3. Rectangle.
4. Ellipse.

TOOLS REQUIRED:

1. Computer with 32 bit or 64 bit Windows Operating system and 4GB RAM
2. Python3
3. OpenCV computer vision Library for Open CV in Python

ALGORITHM:

Step 1: Open a python Script file.

Step 2: Load the image.

Step 3: Perform Drawing Lines, Text Circle, Rectangle and Ellipse over the input image.

Step 4: visualize the image using open CV.

PROGRAM:

# Import dependencies
import cv2
# Read Images
img = cv2.imread('image.jpg')
# Display Image
cv2.imshow('Original Image',img)
# 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)
# 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)
# make a copy of the original image
imageRectangle = img.copy()
# 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)
# 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
axisl = (100,50)
axis2 = (125,50)
# draw the ellipse
#Horizontal
cv2.ellipse(imageEllipse, ellipse_center, axisl, 0, 0, 360, (255, 0, 0), thickness=3)
#Vertical
cv2.ellipse(imageEllipse, ellipse_center, axis2, 90, 0, 360, (0, 0, 255), thickness=3)
# display the output
cv2.imshow('ellipse Image',imageEllipse)
# 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= (150,225,100))
# display the output image with text over it
cv2.imshow("Image Text",imageText)
cv2.waitKey(0)
RESULT:

Thus, the python program to implement the Drawing Lines, Text Circle, Rectangle and Ellipse is
executed successfully.
IMAGE ENHANCEMENT - UNDERSTANDING COLOR
EXP.NO 04
SPACES, COLOR SPACE CONVERSION, HISTOGRAM
EQUIALIZATION, CONVOLUTION, IMAGE SMOOTHING,
DATE
GRADIENTS, EDGE DETECTION

To write a python program to implement the following Image Enhancement operations

1. Understanding Color Spaces.


2. Color Space Conversion.
3. Histogram Equialization.
4. Convolution.
5. Image Smoothing.
6. Gradients.
7. Edge Detection.

TOOLS REQUIRED:

1. Computer with 32 bit or 64 bit Windows Operating system and 4GB RAM
2. Python3
3. OpenCV computer vision Library for Open CV in Python

ALGORITHM:

Step 1: Open a python Script file.

Step 2: Load the image.

Step 3: Perform Understanding Color Spaces, Color Space Conversion, Histogram Equialization,
Convolution, Image Smoothing, Gradients and Edge Detection over the input image.

Step 4: visualize the image using open CV.

PROGRAM:

import cv2
import numpy as np
img = cv2.imread('image.jpg')
imgl = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Displaying the image color space conversion
cv2.imshow('original', img)
cv2.imshow('Gray', imgl)
cv2.imshow('HSV', img2)
# creating a Histograms Equalization
# of a image using cv2.equalizeHist()
R, G, B = cv2.split(img)
outputl_R = cv2.equalizeHist(R)
outputl_G = cv2.equalizeHist(G)
outputl_B = cv2.equalizeHist(B)

equ = cv2.merge((outputl_R, outputl_G, outputl_B))


# stacking images side-by-side
res= np.hstack((img, equ))
# show image input vs output
cv2.imshow('image', res)
# Convert to graycsale
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Blur the image for better edge detection
img_blur = cv2.GaussianBlur(img_gray, (3,3), 0)

# Sobel Edge Detection


sobelx = cv2.Sobel(src=img_blur, ddepth=cv2.CV _64F, dx=l, dy=0, ksize=5) # Sobel Edge
Detection on the X axis
sobely = cv2.Sobel(src=img_blur, ddepth=cv2.CV _64F, dx=0, dy=l, ksize=5) # Sobel Edge
Detection on the Y axis
sobelxy = cv2.Sobel(src=img_blur, ddepth=cv2.CV _64F, dx=l, dy=l, ksize=5) # Combined
X and Y Sobel Edge Detection
# Display Sobel Edge Detection Images
cv2.imshow('Sobel X', sobelx)

cv2.imshow('Sobel Y', sobely)

cv2.imshow('Sobel X Y using Sobel() function', sobelxy)

# Canny Edge Detection


edges= cv2.Canny(image=img_blur, thresholdl=lO0, threshold2=200) # Canny Edge
Detection
# Display Canny Edge Detection Image
cv2.imshow('Canny Edge Detection', edges)
# Image Smoothing
# Creating the kernel with numpy
kernel2 = np.ones((5, 5), np.float32)/25

# Applying the filter


img2 = cv2.filter2D(src=img, ddepth=-1, kernel=kernel2)
# showing the image
res= np.hstack((img, img2))
# show image input vs output
cv2.imshow('Convolution', res)
cv2.waitKey(O)

OUTPUT:
RESULT:

Thus, the python program to implement the Understanding Color Spaces, Color Space Conversion,
Histogram Equialization, Convolution, Image Smoothing, Gradients and Edge Detection is executed
successfully.
IMAGE FEATURES, IMAGE ALIGNMENT AND IMAGE
EXP.NO 05
TRANSFORMS - FOURIER, HOUGH, EXTRACT ORB IMAGE
FEATURES, FEATURE MATCHING, CLONING, FEATURE
DATE
MATCHING BASED IMAGE ALIGNMENT

AIM:

To write a python program to implement the following Image Features, Image Alignment and Image
Transforms operations.

1. Extract ORB image features.


2. Feature Matching.
3. Cloning.
4. Feature matching based image alignment.
5. Fourier Transforms.
6. Hough Transforms.

TOOLS REQUIRED:

1. Computer with 32 bit or 64 bit Windows Operating system and 4GB RAM
2. Python3
3. OpenCV computer vision Library for Open CV in Python

ALGORITHM:

Step 1: Open a python Script file.

Step 2: Load the image.

Step 3: Perform Extraction of ORB image features, Feature Matching, Cloning, Feature matching based
image alignment, Fourier Transforms, Hough Transforms over the input image.

Step 4: visualize the image using open CV.

PROGRAM:

a) Extract ORB image features.

import numpy as np
import cv2
# Read the query image as query_img
# and train image This query image
# is what you need to find in train image
# Save it in the same directory
# with the name image.jpg
query_img = cv2.imread('query.jpg')
train_img = cv2.irnread('train.jpg')

# Convert it to grayscale
query_img_bw = cv2.cvtColor(query_img,cv2.COLOR_BGR2GRAY)
train_img_bw = cv2.cvtColor(train_img, cv2.COLOR_BGR2GRAY)

# Initialize the ORB detector algorithm


orb= cv2.ORB_create()

# Now detect the keypoints and compute


# the descriptors for the query image
# and train image
queryKeypoints, queryDescriptors = orb.detectAndCompute(query_img_bw,None)
trainKeypoints, trainDescriptors = orb.detectAndCompute(train_img_bw ,None)

# Initialize the Matcher for matching


# the keypoints and then match the
# keypoints
matcher = cv2.BFMatcher()
matches = matcher.match(queryDescriptors,trainDescri ptors)

# draw the matches to the final image


# containing both the images the drawMatches()
# function takes both images and keypoints
# and outputs the matched query image with
# its train image
final_img = cv2.drawMatches(query_img, queryKeypoints,
train_img, trainKeypoints, matches[:20],None)

final_img = cv2.resize(final_img, (650,650))

# Show the final image


cv2.imshow("Matches", final_img)

# Read images : src image will be cloned into dst


im = cv2.imread("wood-texture.jpg")
obj= cv2.imread("message.jpg")

#Create a rough mask around the airplane.


src_mask = np.zeros(src.shape, src.dtype)
poly= op.array([ [4,80], [30,54], [151,63], [254,37], [298,90], [272,134], [43,122]],
np.int32)
cv2.fil1Poly(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)
cv2.imshow(" opencv-normal-clone.jpg", output)
#cv2.imshow("opencv-mixed-clone-example.jpg", mixed_clone)
cv2.waitKey(0)

b) Feature Matching, Cloning, Feature matching based image alignment, Fourier Transforms,
Hough Transforms:
# Python program to illustrate HoughLine
# method for line detection
import cv2
import numpy as np

# Reading the required image in


# which operations are to be done.
# Make sure that the image is in the same
# directory in which this python program is
img = cv2.imread('image.jpg')
image=img
# Convert the img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Apply edge detection method on the image


edges= cv2.Canny(gray, 50, 150, apertureSize=3)

# This returns an array of r and theta values


lines= cv2.HoughLines(edges, 1, np.pi/180, 200)

# The below for loop runs till r and theta values


# are in the range of the 2d array
for r_theta in lines:
arr= np.array(r_theta[0], dtype=np.float64)
r, theta = arr
# Stores the value of cos(theta) in a
a= np.cos(theta)

# Stores the value of sin(theta) in b


b = np.sin(theta)

# x0 stores the value rcos(theta)


x0 = a*r

# y0 stores the value rsin(theta)


y0 = b*r

# xl stores the rounded off value of (rcos(theta)-lO00sin(theta))


xl = int(x0 + 1000*(-b))
# yl stores the rounded off value of (rsin(theta)+ lO00cos(theta))
yl = int(y0 + lO00*(a))

# x2 stores the rounded off value of (rcos(theta)+1000sin(theta))


x2 = int(x0 - 1000*(-b))

# y2 stores the rounded off value of (rsin(theta)-lO00cos(theta))


y2 = int(y0 - lO00*(a))

# cv2.line draws a line in img from the point(xl,yl) to (x2,y2).


# (0,0,255) denotes the colour of the line to be
# drawn. In this case, it is red.
cv2.line(img, (xl, yl), (x2, y2), (0, 0, 255), 2)

# All the changes made in the input image are finally


# written on a new image houghlines.jpg
cv2.imshow('linesDetected.jpg', img)
# Convert to grayscale.
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Blur using 3 * 3 kernel.


gray_blurred = cv2.blur(gray, (3, 3))

# Apply Hough transform on the blurred image.


detected_circles = cv2.HoughCircles(gray_blurred,
cv2.HOUGH_GRADIENT, 1, 20, paraml = 50,
param2 = 30, minRadius = 1, maxRadius = 40)

# Draw circles that are detected.


if detected_circles is not None:

# Convert the circle parameters a, band r to integers.


detected_circles = np.uintl 6(np.around( detected_circles))

for pt in detected_circles[0, :]:


a, b, r = pt[0], pt[1], pt[2]

# Draw the circumference of the circle.


cv2.circle(img, (a, b), r, (0, 255, 0), 2)

# Draw a small circle (of radius 1) to show the center.


cv2.circle(img, (a, b), 1, (0, 0, 255), 3)
cv2.imshow("Detected Circle", img)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)


# Compute the discrete Fourier Transform of the image
fourier = cv2.dft(np.float32(gray), flags=cv2.DFT_COMPLEX_OUTPUT)

# Shift the zero-frequency component to the center of the spectrum


fourier_shift = np.fft.fftshift(fourier)

# calculate the magnitude of the Fourier Transform


magnitude = 20*np.log(cv2.magnitude(fourier_shift[:,:,O],fourier_shift[:,:,1]))

# Scale the magnitude for display


magnitude = cv2.normalize(magnitude, None, 0, 255, cv2.NORM_MINMAX,
cv2.CV_8UC1)

# Display the magnitude of the Fourier Transform


cv2.imshow('Fourier Transform', magnitude)

OUTPUT:

a) Extract ORB image features:


b) Feature Matching, Cloning, Feature matching based image alignment, Fourier Transforms, Hough
Transforms:
RESULT:

Thus, the python program to implement the Extraction of ORB image features, Feature Matching,
Cloning, Feature matching based image alignment, Fourier Transforms and Hough Transforms is executed
successfully.
EXP.NO 06
IMAGE SEGMENTATION USING GRAPHCUT I GRABCUT
DATE

To write a python program to implement the Image segmentation process using Graph cut and Grab cut
method.

TOOLS REQUIRED:

1. Computer with 32 bit or 64 bit Windows Operating system and 4GB RAM
2. Python3
3. OpenCV computer vision Library for Open CV in Python

ALGORITHM:

Step 1: Open a python Script file.

Step 2: Load the image.

Step 3: Perform Image segmentation process using Graph cut and Grab cut method over the input image.

Step 4: visualize the image using open CV.

PROGRAM:

a) GRAPH CUT:
#import cv2
import numpy as np
import matplotlib.pyplot as plt
import maxflow
from skimage import data, color

# Load an example image (you can replace this with your own image loading code)
image= data.astronaut()
#image=cv2.imread('image.jpg')
image_gray = color.rgb2gray(image)

# Create a graph using PyMaxflow


g = maxflow.Graph[float]()

# Add nodes with capacities


nodes= g.add_grid_nodes(image_gray.shape)
unary_energy= np.abs(image_gray - 0.5) # Unary term based on pixel intensity difference
g.add_grid_edges(nodes, weights=unary_energy, structure=np.array([[0, 1, OJ, [1, 0, lJ, [0, 1,
OJ]))

# Add terminal edges (source and sink)


source_mask = image_gray > 0.2 # Example source mask based on intensity threshold
sink_mask = ~source_mask

#Setup the capacities in the unary term


capacities = np.zeros_like(image_gray)
capacities[source_maskJ = 100 # Set source capacities
capacities[sink_maskJ = 100 # Set sink capacities

g.add_grid_tedges(nodes, capacities, capacities)

# Compute the maxflow


g.maxflow()

# Get the segmentation result


segmentation= g.get_grid_segments(nodes)

# Plot the results


plt.figure(figsize=(lO, 5))
plt.subplot(l, 2, 1)
plt.imshow(image_gray, cmap='gray')
plt.title("Original Grayscale Image")
plt.subplot(l, 2, 2)
plt.imshow(segmentation, cmap='gray')
pit.title("Segmentation Result")
pit.show()

b) GRABCUT:
# Python program to illustrate
# foreground extraction using
# GrabCut algorithm

# organize imports
import numpy as np
import cv2

from matplotlib import pyplot as pit


# path to input image specified and
# image is loaded with imread command
image = cv2.imread('grrrp.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)
backgroundModel = np.zeros((l, 65), np.float64)
foregroundModel = np.zeros((l, 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)1(mask == 0), 0, l).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()

OUTPUT:

GRAPH CUT:

Figure 1

Original Grayscale Image Segmentation Result

100 100

200 200

300 300

400 400

500 500
100 200 300 400 500 100 200 300 400 500

Ln: 1 Col:O
GRABCUT:
• I Figure 1

250

50 200

100
150

150

100

200

50

250

100 200 300 400

Activdte Wi'ldows

A++l+O. l[gi

RESULT:

Thus, the python program to implement the Image segmentation process using Graph cut and Grab
cut method is executed successfully.
EXP.NO 07
CAMERA CALIBRATION WITH CIRCULAR GRID
DATE

AIM:

To write a python program to implement the concept of camera calibration with circular grid.

APPARATUS:

1. Computer with
2. Python3
3. OpenCV computer vision Library for Open CV in Python

ALGORITHM:

Step 1: Create a python notebook.

Step 2: Make the camera live.

Step 3: Apply the above mentioned concept of camera calibration with circular grid in the live video.

Step 4: visualize the image using open CV.

PROGRAM:

import numpy as np
import cv2
import yaml
# termination criteria
criteria= (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30,
0.001)

########################################Blob
Detector##############################################

# Setup SimpleBlobDetector parameters.


blobParams = cv2.SimpleBlobDetector_Params()

# Change thresholds
blobParams.minThreshold = 8
blobParams.maxThreshold = 255

# Filter by Area.
blobParams.filterByArea = True
blobParams.minArea = 64 # minArea may be adjusted to suit for your experiment
blobParams.maxArea = 2500 # maxArea may be adjusted to suit for your
experiment
# Filter by Circularity
blobParams.filterByCircularity = True
blobParams.minCircularity = 0.1

# Filter by Convexity
blobParams.filterByConvexity = True
blobParams.minConvexity = 0.87

# Filter by Inertia
blobParams.filterBylnertia = True
blobParams.minlnertiaRatio = 0.01

# Create a detector with the parameters


blobDetector = cv2.SimpleBlobDetector_create(blobParams)

#####################################################################
##############################

#####################################################################
##############################

# Original blob coordinates, supposing all blobs are of z-coordinates 0


# And, the distance between every two neighbour blob circle centers is 72 centimetres
# In fact, any number can be used to replace 72.
# Namely, the real size of the circle is pointless while calculating camera calibration
parameters.
objp = np.zeros((44, 3), np.float32)
objp[0] = (0 , 0 , 0)
objp[l] = (0 , 72, 0)
objp[2] = (0 , 144, 0)
objp[3] = (0 , 216, 0)
objp[4] = (36 , 36 , 0)
objp[5] = (36 , 108, 0)
objp[6] = (36 , 180, 0)
objp[7] = (36 , 252, 0)
objp[8] = (72 , 0 , 0)
objp[9] = (72 , 72 , 0)
objp[lO] = (72, 144, 0)
objp[l l] = (72 , 216, 0)
objp[l2] = (108, 36, 0)
objp[13] = (108, 108, 0)
objp[14] = (108, 180, 0)
objp[15] = (108, 252, 0)
objp[16] = (144, 0 , 0)
objp[l 7] = (144, 72, 0)
objp[18] = (144, 144, 0)
objp[l9] = (144, 216, 0)
objp[20] = (180, 36, 0)
objp[21] = (180, 108, 0)
objp[22] = (180, 180, 0)
objp[23] = (180, 252, 0)
objp[24] = (216, 0 , 0)
objp[25] = (216, 72, 0)
objp[26] = (216, 144, 0)
objp[27] = (216, 216, 0)
objp[28] = (252, 36 , 0)
objp[29] = (252, 108, 0)
objp[30] = (252, 180, 0)
objp[31] = (252, 252, 0)
objp[32] = (288, 0 , 0)
objp[33] = (288, 72 , 0)
objp[34] = (288, 144, 0)
objp[35] = (288, 216, 0)
objp[36] = (324, 36 , 0)
objp[37] = (324, 108, 0)
objp[38] = (324, 180, 0)
objp[39] = (324, 252, 0)
objp[40] = (360, 0 , 0)
objp[41] = (360, 72, 0)
objp[42] = (360, 144, 0)
objp[43] = (360, 216, 0)
#####################################################################
##############################

# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
cap = cv2.VideoCapture(0)
found= 0
while(found < 10): # Here, 10 can be changed to whatever number you like to choose
ret, img =cap.read()# Capture frame-by-frame
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

keypoints = blobDetector.detect(gray) # Detect blobs.

# Draw detected blobs as red circles. This helps cv2.findCirclesGrid() .


im_with_keypoints = cv2.drawKeypoints(img, keypoints, op.array([]), (0,255,0),
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
im_with_keypoints_gray = cv2.cvtColor(im_with_keypoints,
cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findCirclesGrid(im_with_keypoints, (4,11), None, flags =
cv2.CALIB_CB_ASYMMETRIC_GRID) # Find the circle grid

if ret == True:
objpoints.append(objp) # Certainly, every loop objp is the same, in 3D.

corners2 = cv2.cornerSubPix(im_with_keypoints_gray, corners, (11,11), (-1,-1),


criteria) # Refines the corner locations.
imgpoints.append(corners2)

# Draw and display the corners.


im_with_keypoints = cv2.drawChessboardCorners(img, (4,11), corners2, ret)
found+= 1

cv2.imshow("img", im_with_keypoints) # display


cv2.waitKey(2)

# When everything done, release the capture


cap.release()
cv2.destroyAllWindows()

ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-


1], None, None)
# It's very important to transform the matrix to list.
data= { 'camera_matrix': np.asarray(mtx).tolist(), 'dist_coeff': np.asarray(dist).tolist()}
with open("calibration.yaml", "w") as f:yaml.dump(data, f)
with open('calibration.yaml') as f:loadeddict = yaml.load(f)
mtxloaded = loadeddict. get('camera_matrix ')
distloaded = loadeddict.get('dist_coeff)
OUTPUT:

RESULT:

Thus, the python program to implement the concept of camera calibration with circular grid is
executed successfully.
EXP.NO 08
CREATING DEPTH MAP FROM STEREO IMAGES
DATE

AIM:

To write a python program to implement the concept of creating depth map from stereo images.

APPARATUS:

1. Computer with
2. Python3
3. OpenCV computer vision Library for Open CV in Python

ALGORITHM:

Step 1: Create a python notebook.

Step 2: Load the image.

Step 3: Apply the above mentioned concept of creating depth map from stereo image to the image.

Step 4: visualize the image using open CV.

PROGRAM:

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
imgL = cv.imread('tsukuba_l.png',O)
imgR = cv.imread('tsukuba_r.png',O)
#cv.imshow('lmgL',imgL)
stereo= cv.StereoBM.create(numDisparities=l6, blockSize=15)
disparity = stereo.compute(imgL,imgR)
plt.imshow(disparity,'gray')
pit.show()
OUTPUT:
Figure 1

50

100

150

200

250

50 100 150 200 250 300 350

n: 11 Col· O

RESULT:

Thus, the python program to implement the concept of creating depth map from stereo images is
executed successfully.
EXP.NO 09
OBJECT DETECTION AND TRACKING USING KALMAN
FILTER, CAMSHIFT
DATE

AIM:

To write a python program to implement the following object detection and tracking operations

1. Object detection and tracking operations using Camshift algorithm.


2. Object detection and tracking operations using Kalman filter algorithm.

APPARATUS:

1. Computer with
2. Python3
3. OpenCV computer vision Library for Open CV in Python

ALGORITHM:

Step 1: Create a python notebook.

Step 2: Load the prerecorded video or live the camera.

Step 3: Apply the above mentioned object detection and tracking operations on the prerecorded video or live
video from the camera.

Step 4: visualize the image using open CV.

PROGRAM:

OBJECT DETECTION AND TRACKING USING CAMSHIFT:

# DataFlair Object Tracker

# import necessary packages


import cv2
import numpy as np

# Naming the Output window


windowname = 'Result'
cv2.namedWindow(windowname)

cap = cv2.VideoCapture(0)

output= None

X, y, w, h = 0, 0, 0, 0

first_point_saved = False
second_point_saved = False
track_window = (x, y, w, h)
can_track = False

def click_event(event, px, py, flags, param):


global x, y, w, h, first_point_saved,second_point_saved, track_window, can_track,
output

# Left mouse button release event


if event== cv2.EVENT_LBUTTONUP:
if first_point_saved:
w = px-x
h = py-y
track_window = (x, y, w, h)
print(x, y, w, h)
first_point_saved = False
second_point_saved = True
else:
x=px
y=py
first_point_saved = True
can_track = False

# Right mouse button press event


if event== cv2.EVENT_RBUTTONDOWN:
can_track = False
cv2.setMouseCallback(windowname, click_event) # Start the mouse event

# initialize tracker

def initialize(frame, track_window):


x, y, w, h = track_window
# set up the ROI for tracking
roi = frame[y:y+h, x:x+w]
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
roi_hist = cv2.calcHist([hsv_roi],[0],None,[180],[0,180])
roi_hist = cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)

return roi_hist, roi

# Setup the termination criteria


term_crit = ( cv2.TERM_CRITERIA_EPS I cv2.TERM_CRITERIA_COUNT, 10, 1 )
while True:
ret, frame= cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

# Check if 2nd point is also saved then initialize the tracker


if second_point_saved:
roi_hist, roi = initialize(frame, track_window)
second_point_saved = False
can_track = True

# Start tracking
if can_track == True:
dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)
# apply camshift to get the new location
ret, track_window = cv2.CamShift( dst, track_window, term_crit)
# Draw it on image
pts = cv2.boxPoints(ret)
pts = np.int0(pts)
print(ret)
print(track_window)
cv2.imshow('roi', roi)
output= cv2.polylines(frame,[pts],True, 255,2)

else:
output = frame
if first_point_saved:
cv2.circle(output, (x, y), 5, (0, 0, 255), -1)
cv2.destroyWindow

# Show the output


cv2.imshow(windowname,output)
if cv2.waitKey(l) == ord('q'):
break

cap.release()
cv2.destroyAllWindows()

OBJECT DETECTION AND TRACKING USING KALMAN FILTER:

OBJECT TRACKING:

import cv2
from Detector import detect
from KalmanFilter import KalmanFilter

def main():

# Create opencv video capture object


VideoCap = cv2.VideoCapture('randomball.avi')

#Variable used to control the speed of reading the video


ControlSpeedVar = 100 #Lowest: 1 - Highest:100

HiSpeed = 100

#Create KalmanFilter object KF


#KalmanFilter(dt, u_x, u_y, std_acc, x_std_meas, y_std_meas)

KF = KalmanFilter(0.1, 1, 1, 1, 0.1,0.1)

debugMode=l

while(True):
# Read frame
ret, frame = VideoCap.read()

# Detect object
centers = detect(frame,debugMode)

# If centroids are detected then track them


if (len(centers) > 0):

# Draw the detected circle


cv2.circle(frame, (int(centers[0][0]), int(centers[0][l])), 10, (0, 191, 255), 2)

# Predict
(x, y) = KP.predict()
# Draw a rectangle as the predicted object position
cv2.rectangle(frame, (int(x - 15), int(y - 15)), (int(x + 15), int(y + 15)), (255, 0, 0), 2)

# Update
(xl, yl) = KF.update(centers[0])
# Draw a rectangle as the estimated object position
cv2.rectangle(frame, (int(xl - 15), int(yl - 15)), (int(xl + 15), int(yl + 15)), (0, 0,
255), 2)

cv2.putText(frame, "Estimated Position", (int(xl + 15), int(yl + 10)), 0, 0.5, (0, 0,


255), 2)
cv2.putText(frame, "Predicted Position", (int(x + 15), int(y)), 0, 0.5, (255, 0, 0), 2)
cv2.putText(frame, "Measured Position", (int(centers[0][0] + 15), int(centers[0][l] -
15)), 0, 0.5, (0,191,255), 2)

cv2.imshow('image', frame)

if cv2.waitKey(2) & 0xFF == ord('q'):


VideoCap.release()
cv2.destroyAllWindows()
break

cv2.waitKey(HiSpeed-ControlSpeedVar+1)

if_name_ "_main_"·
# execute main
main()

OBJECT DETECTOR:

# Import python libraries


import numpy as np
import cv2

def detect(frame,debugMode):
# Convert frame from BGR to GRAY
gray= cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

if (debugMode):
cv2.imshow('gray', gray)

# Edge detection using Canny function


img_edges = cv2.Canny(gray, 50, 190, 3)
if (debugMode):
cv2.imshow('img_edges', img_edges)

# Convert to black and white image


ret, img_thresh = cv2.threshold(img_edges, 254, 255,cv2.THRESH_BINARY)
if (debugMode):
cv2.imshow('img_thresh', img_thresh)

# Find contours
contours, _ = cv2.findContours(img_thresh, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)

# Set the accepted minimum & maximum radius of a detected object


min_radius_thresh= 3
max_radius_thresh= 30

centers=[]
for c in contours:
# ref: https://docs.opencv.org/trunk/dd/d49/tutorial_py_contour_features.html
(x, y), radius= cv2.minEnclosingCircle(c)
radius = int(radius)

#Take only the valid circle(s)


if (radius > min_radius_thresh) and (radius < max_radius_thresh):
centers.append(np.array([[x], [y]]))
cv2.imshow('contours', img_thresh)
return centers
KALMAN FILTER:

import numpy as np
import matplotlib.pyplot as plt

class KalmanFilter( object):


def _init_(self, dt, u_x,u_y, std_acc, x_std_meas, y_std_meas):

:param dt: sampling time (time for 1 cycle)


:param u_x: acceleration in x-direction
:param u_y: acceleration in y-direction
:param std_acc: process noise magnitude
:param x_std_meas: standard deviation of the measurement in x-direction
:param y_std_meas: standard deviation of the measurement in y-direction

# Define sampling time


self.dt = dt

# Define the control input variables


self.u = np.matrix([[u_x],[u_y]])

# Intial State
self.x = np.matrix([[O], [0], [0], [0]])

# Define the State Transition Matrix A


self.A = np.matrix([[ 1, 0, self.dt, O],
[O, 1, 0, self.dt],
[O, 0, 1, 0],
[O, 0, 0, 1]])

# Define the Control Input Matrix B


self.B = np.matrix([[(self.dt**2)/2, O],
[O,(self.dt**2)/2],
[self.dt,O],
[O,self.dt]])

# Define Measurement Mapping Matrix


self.H = np.matrix([[ 1, 0, 0, 0],
[O, 1, 0, OJ])

#Initial Process Noise Covariance


self.Q = np.matrix([[(self.dt**4)/4, 0, (self.dt**3)/2, 0],
[0, (self.dt**4)/4, 0, (self.dt**3)/2],
[(self.dt**3)/2, 0, self.dt**2, 0],
[O, (self.dt**3)/2, 0, self.dt**2]]) * std_acc**2

#Initial Measurement Noise Covariance


self.R = np.matrix([[x_std_meas**2,0],
[O, y_std_meas**2]])

#Initial Covariance Matrix


self.P = np.eye(self.A.shape[l])

def predict(self):
# Refer to :Eq.(9) and Eq.(10) in https://machinelearningspace.corn/object-tracking-
simple-implementation-of-kalman-filter-in-
python/?preview_id= l 364&preview _nonce=52f6fl 262e&preview=true&_thumbnail_id=1
795

# Update time state


#x_k =Ax_(k-1) + Bu_(k-1) Eq.(9)
self.x = np.dot(self.A, self.x) + np.dot(self.B, self.u)

# Calculate error covariance


# P= A*P*A'+ Q Eq.(10)
self.P = np.dot(np.dot(self.A, self.P), self.A.T) + self.Q
return self.x[0:2]

def update(self, z):

# Refer to :Eq.(11), Eq.(12) and Eq.(13) in https://machinelearningspace.com/object-


tracking-simple-implementation-of-kalman-filter-in-
python/?preview_id=1364&preview _nonce=52f6fl 262e&preview=true&_thumbnail_id=1
795
# S = H*P*H'+R
S = np.dot(self.H, np.dot(self.P, self.H.T)) + self.R

# Calculate the Kalman Gain


# K = P * H'* inv(H*P*H'+R)
K = np.dot(np.dot(self.P, self.H.T), np.linalg.inv(S)) #Eq.(11)

self.x = np.round(self.x + np.dot(K, (z - np.dot(self.H, self.x)))) #Eq.(12)

I= np.eye(self.H.shape[l])

# Update error covariance matrix


self.P = (I - (K * self.H)) * self.P #Eq.(13)
return self.x[0:2]

OUTPUT:

OBJECT DETECTION AND TRACKING USING CAMSHIFT


OBJECT DETECTION AND TRACKING USING KALMAN FILTER
RESULT:

Thus, the python program to implement the concept of object detection and tracking operations is
executed successfully.
CONTENT BEYOND SYLLABUS
EXP.NO 01
CONTENT BASED VIDEO RETRIVAL
DATE

AIM:

To write a python program to implement the concept of content based video retrival.

APPARATUS:

1. Computer with
2. Python3
3. OpenCV computer vision Library for Open CV in Python

ALGORITHM:

Step 1: Create a python notebook.

Step 2: Load the video.

Step 3: Import the necessary python packages.

Step 4: import argparse for content video retrival.

Step 5: Using numpy for mathematical operation.

PROGRAM:

import argparse
from collections import Counter
import time
from pyspin.spin import make_spin, Spin2
import app.config as config
from app.helpers import display_results_histogram, get_number_of_frames,
get_video_filenames, get_video_fps, \
get_video_first_frame, print_finished_training_message, terminal_yes_no_question,
show_final_match, \
video_file_already_stabilised
from app.histogram import HistogramGenerator
from app.video_operations import VideoStabiliser
def main():
"""
Program entry point. Parses command line input to decide which phase of the system
to
run.
:return: None

parser = argparse.ArgumentParser()
parser.add_argument("-m", "--model",
help="The histogram model to use. Choose from the following options: 'rgb',
'hsv' or 'gray'. "
"Leave empty to train using all 3 histogram models.")
parser.add_argument("--mode",
required=True,
help="The mode to run the code in. Choose from the following options:
'train', 'test' or "
"'segment'.")
parser.add_argument("--showhists",
action=''store_true'',
help="Specify whether you want to display each generated histogram.")
parser.add_argument("-d", "--debug",
action='' store_true '',
help="Specify whether you want to print additional logs for debugging
purposes.")
args = parser.parse_args()
config.debug = args.debug
config.mode = args.mode
config.show _histograms = args.showhists
config.model = args.model
if config.mode == "train":
off_line_colour_based_feature_extraction_phase()
elif config.mode == "test":
on_line_retrieval_phase()
elif config.mode == "segment":
database_preprocessing_phase()
else:
print("Wrong mode chosen. Choose from the following options: 'train', 'test' or
'segment'.")
exit(O)
@make_spin(Spin2, "Generating histograms for database
videos...".format(config.model))
def off_line_colour_based_feature_extraction_phase():
"""
Generates and stores averaged greyscale, RGB and HSV histograms for all the videos
in
the directory-based database.
:return: None

directory = "../footage/"
files= get_video_filenames(directory)
# start measuring runtime
start_time = time.time()
for file in files:
if config.model == "gray":
histogram_generator = HistogramGenerator(directory, file)
histogram_generator.generate_video_greyscale_histogram()
elif config.model == "rgb":
histogram_generator = HistogramGenerator(directory, file)
histogram_generator.generate_video_rgb_histogram()
elif config.model == "hsv":
histogram_generator = HistogramGenerator(directory, file)
histogram_generator.generate_video_hsv_histogram()
else:
histogram_generator_gray = HistogramGenerator(directory, file)
histogram_generator_gray.generate_video_greyscale_histogram()
histogram_generator_rgb = HistogramGenerator(directory, file)
histogram_generator_rgb.generate_video_rgb_histogram()
histogram_generator_hsv = HistogramGenerator(directory, file)
histogram_generator_hsv.generate_video_hsv_histogram()
runtime = round(time.time() - start_time, 2)
print_finished_training_message(config.model, directory, runtime)
def on_line_retrieval_phase():
"""
Prompts the user to stabilise and crop the query video before generating the same
averaged greyscale, RGB and HSY
histograms to compare with the database videos' previously stored histograms using
distance metrics.
:return: None

directory = "../recordings/"
recordings= ["recordingl.mp4", "recording2.mp4", "recording3.mp4",
"recording4.mp4", "recording5.mp4",
"recording6.mp4", "recording7.mp4", "recording8.mp4"]
mismatches_directory = "../recordings/mismatches/"
mismatches= ["mismatchl.mp4", "mismatch2.mp4"]
# 0: cloudy-sky, 1: seal, 2: butterfly (skewed), 3: wind-turbine, 4: ice-hockey, 5:
jellyfish,
6: people-dancing,
# 7: jellyfish (skewed)
file = recordings [7]
# ask user to stabilise the input query video or not
is_stabilise_video = terminal_yes_no_question("Do you wish to stabilise the recorded
query video?")
stable_filename ="stable-"+ file[:-4] + ".avi" # the stable version of the video
# yes: stabilise the video and use the stable .avi version
if is_stabilise_ video:
if not video_file_already_stabilised(directory + stable_filename):
VideoStabiliser(directory, "{}".format(file))
print("\nStabilised version of query already found: '{}"'.format(stable_filename))
file = stable_filename
# no: check if a version of the stabilised video doesn't already exist - use it if it does
else:
if video_file_already_stabilised(directory + stable_filename):
file = stable_filename
print("\nUsing query: '{}"'.format(file))
print("\nPlease crop the recorded query video for the signature to be generated.")
if config.model == "gray":
histogram_generator = HistogramGenerator(directory, file)
histogram_generator.generate_video_greyscale_histogram(is_query=True)
histogram_generator.match_histograms()
elif config.model == "rgb":
histogram_generator = HistogramGenerator(directory, file)
histogram_generator.generate_video_rgb_histogram(is_query=True)
histogram_generator.match_histograms()
elif config.model == "hsv":
histogram_generator = HistogramGenerator(directory, file)
histogram_generator.generate_video_hsv_histogram(is_query=True)
histogram_generator.match_histograms()
else:
# calculate query histogram
# greyscale
histogram_generator_gray = HistogramGenerator(directory, file)
histogram_generator_gray.generate_video_greyscale_histogram(is_query=True)
cur_reference_points = histogram_generator_gray.get_current_reference_points()
# start measuring runtime (after manual cropping)
start_time = time.time()
#RGB
histogram_generator_rgb = HistogramGenerator(directory, file)
histogram_generator_rgb.generate_video_rgb_histogram(is_query=True,
cur_ref_points=cur_reference_points)
#HSY
histogram_generator_hsv = HistogramGenerator(directory, file)
histogram_generator_hsv.generate_video_hsv_histogram(is_query=True,
cur_ref_points=cur_reference_points)
# calculate distances between query and DB histograms
histogram_generator_gray.match_histograms(cur_all_model='gray')
histogram_generator_rgb.match_histograms(cur_all_model='rgb')
histogram_generator_hsv.match_histograms(cur_all_model='hsv')
# Combine matches from all 3 histogram models to output one final result
all_results = histogram_generator_hsv.get_results_array() # array of all matches made
(using weights)
results_count = Counter( all_results) # count the number of matches made for each
video in all_results array
# transform from count to percentage of matches made
results_percentage = diet()
for match in results_count:
percentage= round((results_count[match] / len(all_results)) * 100.0, 2)
results_percentage[match] = percentage
display_results_histogram(results_percentage)
print("Matches made: {}".format(results_count))
print("% of matches made: {}".format(results_percentage))
# find best result
final_result_name = ""
final_result_count = 0
for i, r in enumerate(results_count):
if i == 0:
final_result_name = r
final_result_count = results_count[r]
else:
if results_count[r] > final_result_count:
final_result_name = r
final_result_count = results_count[r]
# print results
runtime= round(time.time() - start_time, 2)
accuracy= final_result_count / len(all_results)
get_video_first_frame(directory + file, "../results", is_query=True)
get_video_first_frame("../footage/{ }".format(final_result_name), "../results",
is_result=True)
show_final_match(final_result_name, "../results/query.pug", "../results/result.pug",
runtime, accuracy)
print_finished_training_message(final_result_name, config.model, runtime, accuracy)
def database_preprocessing_phase():
"""
Applies a shot boundary detection algorithm to a video for segmentation.
:return: None

directory = "../recordings/"
video = "scene-segmentation.mp4"
#directory= "/Volumes/ADAM2/"
#movies= ["Inception (2010).mp4"]
# video = movies[0]
shot_boundary _detector = HistogramGenerator(directory, video)
video_capture = shot_boundary_detector.get_video_capture()
frame_count = get_number_of_frames(vc=video_capture)
fps= get_video_fps(vc=video_capture)
print("Total Frames: {}".format(frame_count))
print("FPS: {}\n".format(fps))
# start measuring runtime
start_time = time.time()
# start processing video for shout boundary detection
print("Starting to process video for shot boundary detection...")
shot_boundary_detector.rgb_histogram_shot_boundary_detection(threshold=7)
# print final results
runtime = round(time.time() - start_time, 2)
print("--- Number of frames in video: {} ---".format(frame_count))
print("--- Runtime: { } seconds ---".format(runtime))
if name== " main ":
main()

OUTPUT:
Greyscale histogram for 'stable-recordingB.avi'

0.25

0.20

0.15

0.10

0.05

0.00

100 150 200 250


Bins

Original Query Video

.::-· • ff,

Ill ., ',..,,
I

.A
Match 'jellyfish.mp4' found in 8.88s with 75.0% accuracy
RESULT:

Thus, python program to implement content based video retrival is executed successfully.
EXP.NO 02
FACE DETECTION & RECOGNITION
DATE

AIM:

To write a python program to implement the face detection & recognition using OpenCV.

APPARATUS:

1. Computer with
2. Python3
3. OpenCV computer vision Library for Open CV in Python

ALGORITHM:

Step 1: Create a python notebook.

Step 2: Load the image.

Step 3: Import the necessary python packages.

Step 4: Use matplotlib.pyplot for visualize the image.

Step 5: Using numpy for mathematical operation.

Step 6: Recognise the face using .xml file.

PROGRAM:

import cv2
import numpy as np
f_casd = cv2.CascadeClassifier(cv2.data.haarcascades +
'haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret,frame = cap.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
face = f_casd.detectMultiScale(gray,1.3,5)
for (x,y,w,h) in face:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,255),2)
cv2.imshow('frame',frame)
if cv2.waitKey(l) & 0xFF == ord('q'):
break
cap.release()
cv2.destroy AllWindows()

You might also like