[go: up one dir, main page]

0% found this document useful (0 votes)
15 views56 pages

CV Lab Manual PDF

The document outlines the installation and usage of the OpenCV library with Python, detailing the necessary tools and step-by-step installation procedures. It includes practical examples of basic image processing operations such as loading, cropping, resizing, thresholding, contour analysis, and blob detection. Additionally, it covers advanced topics like image features extraction, image alignment, and segmentation using Graph cut and Grab cut methods.

Uploaded by

dygaming38
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views56 pages

CV Lab Manual PDF

The document outlines the installation and usage of the OpenCV library with Python, detailing the necessary tools and step-by-step installation procedures. It includes practical examples of basic image processing operations such as loading, cropping, resizing, thresholding, contour analysis, and blob detection. Additionally, it covers advanced topics like image features extraction, image alignment, and segmentation using Graph cut and Grab cut methods.

Uploaded by

dygaming38
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 56

AIM:

EXP.NO 01 OPENCV INSTALLATION AND WORKING WITH PYTHON

DATE

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.

I
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

I
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 -
contribpython:

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

# Load the image

image = cv2.imread(r'C:\Users\samsh\Downloads\ffa.jpg')

if image is None:

print("Error: Image not loaded. Check the path.")

exit()

# Save and display original image

cv2.imwrite('Input_Image.png', image)

cv2.imshow('Original Image', image)

# Crop and display the image

cropped_image = image[200:400, 700:900]

cv2.imshow('Cropped Image', cropped_image)

cv2.imwrite('Cropped_Image.jpg', cropped_image)

# Resize image using nearest-neighbor interpolation

cv2.imshow('Resized Image', cv2.resize(image, (400, 300),


interpolation=cv2.INTER_NEAREST))

# Apply grayscale and binary thresholding

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

_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

cv2.imshow('Binary Threshold', binary)

# Find and draw contours

contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

cv2.imshow('Contours', cv2.drawContours(np.zeros_like(image), contours, -1, (0, 255, 0), 2))

# Blob detection

params = cv2.SimpleBlobDetector_Params()

detector = cv2.SimpleBlobDetector_create(params)

keypoints = detector.detect(gray)

cv2.imshow('Blob Detection', cv2.drawKeypoints(image, keypoints, np.array([]), (0, 0, 255),

cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS))

# Wait and close

cv2.waitKey(0)

cv2.destroyAllWindows()
0utput
Output:
RESULT:

Thus the python program to implement the Drawing Lines,Test circle , Rectangle
and Ellipse is
executed suscessfully.
# showing the image
res= np.hstack (( img , im g2))
# 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 Equalization, Convolution, Image Smoothing, Gradients and Edge Detection is executed successf
EXP.NO 05 IMAGE FEATURES, IMAGE ALIGNMENT AND IMAGE
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:

c)
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.
XP.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 numpy as np
import matplotlib.pyplot as plt
import maxflow
from skimage import io, color

# Load and preprocess the image


image = color.rgb2gray(io.imread(r'C:\Users\User\Pictures\WALLPAPER\ANIME\luffy.png'))

# Create graph and nodes


g = maxflow.Graph[float]()
nodes = g.add_grid_nodes(image.shape)

# Add grid edges (pairwise energy)


g.add_grid_edges(nodes, weights=np.abs(image - 0.5))

# Define source and sink capacities


source_capacity = (image > 0.2).astype(float) * 100
sink_capacity = (image <= 0.2).astype(float) * 100

# Add terminal edges (unary energy)


g.add_grid_tedges(nodes, source_capacity, sink_capacity)

# Compute maxflow and get segmentation


g.maxflow()
segmentation = np.logical_not(g.get_grid_segments(nodes))

# Display results
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
axes[0].imshow(image, cmap='gray')
axes[0].set_title("Original Grayscale Image")
axes[0].axis('off')

axes[1].imshow(segmentation, cmap='gray')
axes[1].set_title("Segmentation Result")
axes[1].axis('off')

plt.tight_layout()
plt.show()
b) GRABCUT:

# Python program to illustrate foreground extraction using GrabCut algorithm

import numpy as np

import cv2

from matplotlib import pyplot as plt

# Load the image (raw string to avoid backslash issues)

image = cv2.imread(r'C:\Users\samsh\Downloads\VK1.jpg')

if image is None:

raise FileNotFoundError("Image not found. Check the path.")


# Create initial mask

mask = np.zeros(image.shape[:2], np.uint8)

# Define background and foreground models

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

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

# Define rectangle region of interest (ROI)

rectangle = (20, 100, 150, 150)

# Apply GrabCut algorithm

cv2.grabCut(image, mask, rectangle, backgroundModel, foregroundModel, 3, cv2.GC_INIT_WITH_RECT)

# Modify the mask

mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8')

# Apply the mask to extract the foreground

result = image * mask2[:, :, np.newaxis]

# Show result

plt.imshow(cv2.cvtColor(result, cv2.COLOR_BGR2RGB))

plt.title("Segmented Image")

plt.axis('off')

plt.show()
OUTPUT:

GRAPH CUT:

F ig u re 1

Original Grayscale Image Segmentation Result


Grab cut:

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 cv2

import numpy as np # Import numpy to avoid the NameError

# Setup Blob Detector with parameters

params = cv2.SimpleBlobDetector_Params()

params.minThreshold, params.maxThreshold = 8, 255

params.filterByArea, params.minArea, params.maxArea = True, 64, 2500

params.filterByCircularity, params.minCircularity = True, 0.1

params.filterByConvexity, params.minConvexity = True, 0.87

params.filterByInertia, params.minInertiaRatio = True, 0.01

detector = cv2.SimpleBlobDetector_create(params)
# Video capture from external webcam (index 1 instead of 0)

cap = cv2.VideoCapture(1) # Change 0 to 1 to use the external webcam

while cap.isOpened():

ret, img = cap.read()

if not ret: break

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

keypoints = detector.detect(gray)

img_with_keypoints = cv2.drawKeypoints(img, keypoints, np.array([]), (0, 255, 0),

cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

cv2.imshow("Live Video with Blob Detection", img_with_keypoints)

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

cap.release()

cv2.destroyAllWindow
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

# Load left and right images (use stereo pair!)


imgL = cv.imread(r'C:\Users\samsh\Downloads\img.jpg', cv.IMREAD_GRAYSCALE)
imgR = cv.imread(r'C:\Users\samsh\Downloads\img.jpg', cv.IMREAD_GRAYSCALE)

# Check if images loaded properly


if imgL is None or imgR is None:
print("Error: One or both images not found!")
exit()

# Create StereoBM object


stereo = cv.StereoBM_create(numDisparities=16, blockSize=15)
# Compute disparity
disparity = stereo.compute(imgL, imgR)

# Normalize for display


disparity = cv.normalize(disparity, None, 0, 255, cv.NORM_MINMAX, cv.CV_8U)

# Show result
plt.imshow(disparity, 'gray')
plt.title('Disparity Map')
plt.axis('off')
plt.show()
Figure 1

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-inpython/?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 s

You might also like