0% found this document useful (0 votes)
6 views29 pages

Ccs349 Image and Video Analysis Lab Manual1 2

The document outlines various laboratory exercises in image processing and computer vision, including creating T-pyramids, quadtree representation, geometric transformations, and object detection using TensorFlow. Each section provides aims, algorithms, and sample Python code using libraries like OpenCV and TensorFlow. The document serves as a lab record for students at A.J. College of Engineering, detailing practical applications of image processing techniques.

Uploaded by

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

Ccs349 Image and Video Analysis Lab Manual1 2

The document outlines various laboratory exercises in image processing and computer vision, including creating T-pyramids, quadtree representation, geometric transformations, and object detection using TensorFlow. Each section provides aims, algorithms, and sample Python code using libraries like OpenCV and TensorFlow. The document serves as a lab record for students at A.J. College of Engineering, detailing practical applications of image processing techniques.

Uploaded by

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

MOHAMED SATHAK

A.J. COLLEGE OF ENGINEERING


(
No:34, Rajiv Gandhi Road (OMR), IT Highway, Siruseri IT Park, Chennai - 603 103.
ISO 9001:2015 NABCB
Certified by IRQS QM 006

LAB RECORD

Name of the Student :

Register Number :

Subject code & Name :

Year / Sem :

Department :

Academic Year :
ISO 9001:2015 NABCB
Certified by IRQS QM 006

BONAFIDE CERTIFICATE

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


Index
Marks Awarded-Day to Day
activities
Sign of

ofRecordWork(5Mar

TechnicalKnowledge(
theexperiment(5M
the

Attendance(5Marks)
Performing

Completion
S.NoDate Name of the experiment Faculty

5Marks)
arks)
with

ks)
Date

10

Internal Calculation Max. Marks Sign of the


Marks Awarded Faculty
Average of Day to day Activity 20
Mini Project 15
Model Exam 15
Assignment/Simulation 10
Total 60

Sign of the Student Sign of Lab Incharge


lOMoARcPSD|40491420

1.Program that computes the T-pyramid of an image

Aim:

Creating a T-pyramid involves a process called image pyramiding, which


generates a series of progressively smaller representations of an original image.
Algorithm:

This series of images forms a pyramid structure, with the base being the original image
and each higher level being a smaller and lower-resolution version of the image.

1. Install OpenCV in your Python environment.


2. Replace 'path_to_your_image.jpg' with the actual path to your image file.
3. This function (build_t_pyramid) takes in the original image and the number of
desired levels for the pyramid. It uses cv2.pyrDown() to downsample the image,
reducing its size and creating the subsequent levels of the pyramid.
4. Adjust num_levels to determine the depth of the T-pyramid.

Program:
import cv2

defpyramidbuild_t_=pyramid(image,[image] levels):
for i in range(levels):

pyramid.append(image)image=cv2.pyrDown(image)
return pyramid
# Replace 'image_path' with the path to your
image image_path = 'path_to_your_image.jpg'
original_image = cv2.imread(image_path)
# Define the number of levels for the T-
pyramid num_levels = 5
# Build the T-pyramid
t_pyramid = build_t_pyramid(original_image, num_levels)
# Save the T-pyramid levels as images

forcv2.imwrite(f"leveli,imageinenumerate(t_{i}.jpg",_pyramid):image)
lOMoARcPSD|40491420

Output:

Result:

Thus the program that computes the T-pyramid of an image is successfully executed.
lOMoARcPSD|40491420

2.Quad tree representation of an image using the homogeneity criterion of equal intensity

Aim

To implement a quadtree representation algorithm based on the homogeneity


criterion of equal intensity in an image.
Algorithm Steps:

1. Initialize QuadNode and QuadTree Classes:

QuadNode Class: Represents a node in the quadtree. It contains an image region, a


threshold for determining homogeneity, and information about whether it's a leaf
node or not.
QuadTree Class: Manages the quadtree construction and visualization. It has a root
node and methods to build and visualize the quadtree.
2. QuadNode:

Initialization: A QuadNode is created with an image region and a threshold for


determining homogeneity.
compute_average_color(): Computes the average color of the image region.

split(): Divides the image region into four quadrants (sub-regions).

should_split(): Determines whether the region should be split based on the


homogeneity criterion. It checks if all pixels in the region have the same intensity. If
not, it suggests a split.
build_tree(): Recursively builds the quadtree by splitting nodes until the
homogeneity criterion is met or the node becomes a leaf node.
3. QuadTree:

Initialization: Loads the input image and creates a QuadNode as the root of the quadtree.

build(): Initiates the quadtree construction by calling build_tree() starting from the
root node.
visualize(): Generates an image representation of the quadtree structure for visualization.

create_image(): Creates an image based on the quadtree structure.


lOMoARcPSD|40491420

draw_quadtree(): Recursively fills in the colors of the image based on the quadtree
structure.
4. Main Script:

Replace Image Path: Replace 'path_to_your_image.jpg' with the path to the desired image.

Create QuadTree Instance: Create an instance of the QuadTree class using the image path.

Build QuadTree: Build the quadtree representation by calling the build() method.

Visualize QuadTree: Visualize the generated quadtree structure using the


visualize() method.
Program:
classfrom QuadNode:PILimport Image
def __init__(self, image, threshold):

self.thresholdself.image=image=threshold

self.isself.children_leaf= True=[None, None, None, None]

defself.colorcompute=_averageself.compute_color(self):_average_color()
width, height = self.image.size

r,pixelsg,b==0,list(self.image.getdata())0,0

forr pixel+=pixel[0]inpixels:

bg +=+= pixel[1]pixel[2]
total_pixels = len(pixels)

defreturnsplit(self):(r// total_pixels, g // total_pixels, b // total_pixels)


width, height = self.image.size

subsub__heightwidth == widthheight////22

regions#Divide=the[ image into quadrants


(0, 0, sub_width, sub_height),

(0,(subsub_width,_height,0, width,sub_width,sub_height),height),
lOMoARcPSD|40491420

] (sub_width, sub_height, width, height)

for i, region in enumerate(regions):


x1, y1, x2, y2 = region

self.children[i]sub_image=self.image=QuadNode(sub.crop((x1,_image,y1,x2, self.threshold)y2))

defself.isshould_leaf_split(self):=False

# Check if the image region should be split based on homogeneity

criterion width, height = self.image.size

firstpixels_pixel=list(self.image.getdata())=pixels[0]
for pixel in pixels:

if pixelreturn!=Truefirst_pixel:
return False

defif buildself.should_tree(self):_split():
self.split()

forifchildchild:in self.children:

class QuadTree:child.build_tree()
def __init__(self, image_path, threshold=10):

self.thresholdself.image=Image.open(image=threshold_path)

defself.rootbuild(self):=QuadNode(self. image, self.threshold)

defself.root.buildvisualize(self):_tree()

img.show()img=self.create_image()
def create_image(self):
img = Image.new("RGB", self.image.size)

returnself.drawimg_quadtree(img, self.root)

defif drawnode.is_quadtree(self,_leaf: img, node):


color = node.color
lOMoARcPSD|40491420

forforx iny inrange(img.size[0]):range(img.size[1]):


else: img.putpixel((x, y), color)

for child in node.children:

if child:self.draw_quadtree(img, child)

# Replace 'image_path' with the path to your


image image_path = 'path_to_your_image.jpg'
# Create the QuadTree representation

quadquad__tree.build()tree=QuadTree(image_path)

quad#Visualize_tree.visualize()theQuadTree
lOMoARcPSD|40491420

Output

Result
Thus the program is executed successfully.
lOMoARcPSD|40491420

3.Geometric transformations
Aim:
To understand and implement various geometric transformations such as rotation,
scaling, skewing, affine transformation, and bilinear transformation using Python
with the OpenCV library.

Algorithm:
1. Read the input image.
2. Define the rotation angle.
3. Calculate the rotation matrix using cv2.getRotationMatrix2D().
4. Apply the rotation using cv2.warpAffine() with the rotation matrix.
5. Display the original and rotated images.

Program
importimport numpycv2 as np

# Read the image


image = cv2.imread('input_image.jpg')

height,#Getimagewidthdimensions=image.shape[:2]

angle#Define=30rotation angle

# Calculate rotation matrix


rotation_matrix = cv2.getRotationMatrix2D((width / 2, height / 2), angle, 1)

# Apply rotation
rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))

# Display the original and rotated images

cv2.imshow('Rotatedcv2.imshow('Original Image',Image', rotatedimage)_image)

cv2.destroyAllWindows()cv2.waitKey(0)
lOMoARcPSD|40491420

2. Scaling:

Algorithm:
1. Read the input image.
2. Define scaling factors for x and y axes.
3. Apply scaling using cv2.resize() with the specified scaling factors.
4. Display the original and scaled images.
5. Expected Result:
6. The scaled image should be displayed, resized based on the specified scaling factors.

Programimport: cv2
# Read the image
image = cv2.imread('input_image.jpg')

scale#Define_x= scaling0.5 factors (resize by half)


scale_y = 0.5

# Apply scaling
scaled_image = cv2.resize(image, None, fx=scale_x,

fy=scale_y, interpolation=cv2.INTER_LINEAR)

cv2.imshow('Original#Displaytheoriginal Image',andscaledimage)images
lOMoARcPSD|40491420

cv2.imshow('Scaled Image', scaled_image)

cv2.destroyAllWindows()cv2.waitKey(0)

3. Skewing:

Algorithm:
1. Read the input image.
2. Define pairs of corresponding points for the original and skewed images.
3. Calculate the skew matrix using cv2.getAffineTransform().
4. Apply skewing using cv2.warpAffine() with the skew matrix.
5. Display the original and skewed images.

Program:
importimport numpycv2 as np

# Read the image


image = cv2.imread('input_image.jpg')

# Define perspective transform matrix for skewing

pts1 = np.float32([[50, 50], [200, 50], [50, 200]])


lOMoARcPSD|40491420

pts2 = np.float32([[10, 100], [200, 50], [100, 250]])

# Calculate skew matrix


skew_matrix = cv2.getAffineTransform(pts1, pts2)

# Apply skewing
skewed_image = cv2.warpAffine(image, skew_matrix,

(image.shape[1], image.shape[0]))

# Display the original and skewed images

cv2.imshow('Skewedcv2.imshow('Original Image',Image', skewedimage)_image)

cv2.destroyAllWindows()cv2.waitKey(0)
lOMoARcPSD|40491420

4. Affine Transformation:

Algorithm:
1. Read the input image.
2. Define three pairs of corresponding points for the original and transformed images.
3. Calculate the affine matrix using cv2.getAffineTransform().
4. Apply affine transformation using cv2.warpAffine() with the affine matrix.
5. Display the original and transformed images.

Program
import cv2
import numpy as np

# Read the image


image = cv2.imread('input_image.jpg')

# Define three pairs of corresponding points

ptspts__dstsrc == np.array([[50,np.array([[10, 50],100],[200,[200,50],50],[50,[100,200]],250]],dtype=np.float32)dtype=np.float32)

# Calculate affine transformation matrix


affine_matrix = cv2.getAffineTransform(pts_src, pts_dst)

# Apply affine transformation


affine_image = cv2.warpAffine(image, affine_matrix,

(image.shape[1], image.shape[0]))

# Display the original and transformed images


cv2.imshow('Original Image', image)
cv2.imshow('Affine Transformed Image',
affine_image) cv2.waitKey(0)
cv2.destroyAllWindows()
lOMoARcPSD|40491420

5. Bilinear Transformation:

Algorithm:
1. Read the input image.
2. Define four pairs of corresponding points for the original and transformed images.
3. Calculate the perspective transform matrix using cv2.getPerspectiveTransform().
4. Apply bilinear transformation using cv2.warpPerspective() with the perspective matrix.
5. Display the original and transformed images.
6. Expected Result:
7. The transformed image should be displayed based on the provided corresponding
points using bilinear interpolation.
Program:
importimport numpycv2 as np

# Read the image


image = cv2.imread('input_image.jpg')

# Define four pairs of corresponding points

ptspts__dstsrc == np.array([[50,np.array([[0,0],50],[300,[200,0],50],[0,300],[50,200],[300,[200,300]],200]],dtype=np.float32)dtype=np.float32)
lOMoARcPSD|40491420

# Calculate bilinear transformation matrix


bilinear_matrix = cv2.getPerspectiveTransform(pts_src, pts_dst)

# Apply bilinear transformation


bilinear_image = cv2.warpPerspective(image, bilinear_matrix, (300, 300))

# Display the original and transformed images


cv2.imshow('Original Image', image)
cv2.imshow('Bilinear Transformed Image',
bilinear_image) cv2.waitKey(0)
cv2.destroyAllWindows()

Result:
Thus the various geometric transformation is done and executed successfully.
lOMoARcPSD|40491420

4.Object detection and recognition

Aim:
The aim of this exercise is to implement object detection and recognition using a
pre-trained deep learning model provided by the TensorFlow Object Detection API

Procedure:

Object Detection Using TensorFlow Object Detection API


1. Setting up the Environment:
Install Required Libraries: Use pip to install TensorFlow and other necessary libraries.
2. Data Preparation:
Collect and Label Dataset: Gather a dataset containing images with the objects you
want to detect. Label the objects in these images using annotation tools like
LabelImg to create bounding boxes around the objects of interest.
3. Model Training (Optional):
Choose a Pre-trained Model: Select a pre-trained object detection model (e.g., SSD,
Faster R-CNN) available in the TensorFlow model zoo.
Fine-tune the Model: Optionally, fine-tune the pre-trained model using your labeled
dataset to improve its accuracy in detecting specific objects relevant to your application.

4. Object Detection Implementation:


• Load the Pre-trained Model:
• Use the path to the frozen detection graph (.pb file) obtained from the trained model
or the TensorFlow model zoo.
• Load the label map (.pbtxt file) containing class labels for the objects.
• Implement Object Detection Code:
• Load the frozen graph into memory using TensorFlow.
• Define functions to load images and perform object detection using the loaded model.
• Use TensorFlow sessions to perform inference on an input image and obtain
detected objects.
• Visualize Object Detection Results:
• Draw bounding boxes and labels around the detected objects on the input image.
• Display or save the modified image with detection results.
lOMoARcPSD|40491420

5. Testing and Observations:


Run Object Detection Code: Execute the implemented code with different input
images containing objects to observe the model's performance.

Evaluate Results: Analyze the accuracy and effectiveness of the model in


detecting and recognizing objects in various scenarios

Program:
importimport cv2os

importimport tensorflownumpyasnpas tf
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util

MODEL#Pathto_NAMEthepre= -'ssdtrained_mobilenetmodel _checkpointv2_coco_2018and_ 03configuration_29'

PATHPATH__TOTO__LABELSCKPT=MODEL=os.path.join('data',_NAME+'/frozen'mscoco_inference_label__map.pbtxt')graph.pb'

category#Loadthe_indexlabel=map
label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS,

use_display_name=True)

detection#Loadthe_graphTensorFlow=tf.Graph()model into memory

withod_detectiongraph_def_graph.as=tf.compat.v1.GraphDef()_default():

withserializedtf.io.gfile.GFile(PATH_graph=fid.read()_TO_CKPT, 'rb') as fid:

tf.importod_graph__graphdef.ParseFromString(serialized_def(od_graph_def,name='')_graph)

def#Functiondetect_objects(imagetoperformobject_np):detection
with detection_graph.as_default():
with tf.compat.v1.Session(graph=detection_graph) as sess:
lOMoARcPSD|40491420

ops#Get= handlesdetectionto_graph.getinputand_operations()outputtensors
all_tensor_names = {output.name for op in ops for output in
op.outputs} tensor_dict = {}
for key in ['num_detections', 'detection_boxes',
'detection_scores', 'detection_classes']:
tensor_name = key + ':0'
if tensor_name in all_tensor_n

Output :

Result:
The implementation will result in an object detection system capable of analyzing an
input image and identifying various objects within it. The output will display the
input image with bounding boxes drawn around detected objects, accompanied by
labels specifying the type of each recognized object.
lOMoARcPSD|40491420

5.Motion analysis

Aim:
The aim of this project is to perform motion analysis using frame differencing on an
image sequence. This technique helps identify changes between consecutive frames,
revealing moving edges or objects in the sequence.

Procedure :
1. Setting up the Environment:
• Install OpenCV: Ensure OpenCV is installed to leverage its functionalities
for image processing and video manipulation.
2. Accessing Image Sequence:
• Provide Image Sequence: Have a directory containing a sequence of images
that represent consecutive frames of the video or sequence to be analyzed.
• Read Image Files: Use functions from libraries like os to access and load the
image sequence files.
3. Implementing Frame Differencing:
• Frame Differencing Function: Write a function that computes the absolute
difference between consecutive frames.
• Convert Frames to Grayscale: Convert each frame to grayscale to simplify
the comparison between frames.
• Compute Frame Difference: Use the absolute difference between grayscale frames
to capture the changes between them, emphasizing the moving edges or objects.
4. Motion Analysis:
• Iterate Through Frames: Loop through the sequence of images, applying the
frame differencing function to consecutive frames.
• Display Motion Analysis: Visualize the frame differences obtained from each
pair of consecutive frames using OpenCV.
• Control Interaction: Allow for user interaction, such as displaying frame
differences and providing an option to exit.
5. Observations and Analysis:
• Observe Motion Analysis Results: Analyze the displayed frame
differences to identify moving edges or objects within the sequence.
• Adjustments and Refinement: Make adjustments or implement
additional techniques (e.g., thresholding, morphological operations) to
refine the analysis and enhance the detection of motion in the sequence.
lOMoARcPSD|40491420

Programimport: cv2

import os

# Function to perform frame differencing

def frame_difference(prev_frame, curr_frame):

# Convert frames to grayscale

prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)

curr_gray = cv2.cvtColor(curr_frame, cv2.COLOR_BGR2GRAY)

# Compute absolute difference between

frames frame_diff = cv2.absdiff(prev_gray,

curr_ gray) return frame_diff

# Path to the directory containing the image sequence

image_sequence_path = 'path/to/your/image_sequence_directory'

# Get the list of image file names in the sequence

image_files = sorted(os.listdir(image_sequence_path))

# Initialize variables for the first frame

prev_frame = cv2.imread(os.path.join(image_sequence_path, image_files[0]))

# Iterate through the image sequence starting from the second

image for i in range(1, len(image_files)):

current_frame = cv2.imread(os.path.join(image_sequence_path, image_files[i]))

# Perform frame differencing

diff = frame_difference(prev_frame, current_frame)

# Display the frame difference (you can modify this part to visualize the result)
lOMoARcPSD|40491420

cv2.imshow('Frame Difference', diff)

if cv2.waitKey(100) & 0xFF == ord('q'): # Press 'q' to

exit break

prev_frame = current_frame

cv2.destroyAllWindows()

Output:

Result:
Following these steps will enable the implementation of motion analysis using frame
differencing on an image sequence, allowing for the identification and visualization
of moving edges or objects across frames.
lOMoARcPSD|40491420

6.Facial detection and recognition


Aim:
The aim of this project is to create a facial detection and recognition system using
Python, OpenCV, and Dlib libraries. This system will detect faces in images or video
streams, recognize them if they match known faces in a database, and display the
recognized faces with bounding boxes.

Procedure:
1. Setting up the Environment:
• Install Required Libraries: Use pip to install OpenCV and Dlib libraries.
2. Accessing Pre-trained Models:
• Download Pre-trained Models: Acquire pre-trained models for face detection,
facial landmark prediction, and face recognition provided by Dlib.
3. Facial Detection and Recognition Code:
• Initialize Components: Initialize the face detector, facial landmarks
predictor, and face recognition model using Dlib.
• Function for Facial Detection and Recognition:
• Convert each frame to grayscale for face detection.
• Detect faces in the grayscale image using Dlib's face detector.
• Extract facial landmarks for each detected face.
• Compute face descriptors using the face recognition model.
• Compare recognized face descriptors with known face descriptors in a
database (not implemented in this code snippet).

4.Video Capture and Processing:


• Open a video capture stream (from a webcam or video file).
• Process each frame from the stream by calling the facial detection and
recognition function.
• Display the processed frames with detected and recognized faces in real-time.
• User Interaction and Termination:
• Allow the user to exit the program by pressing 'q'.

Program:
importimport dlibcv2

from dlib_models import download_model, load_dlib_models

download#Download_model()Dlibmodels
lOMoARcPSD|40491420

# Load Dlib models for face detection and face

recognition load_dlib_models()

faceface__recognizerdetector= dlib= .get_frontal_face_detector()


dlib.face_recognition_model_v1("dlib_models/dlib_face_recognition_resnet_model_v
1.dat")
# Load a pre-trained face recognition model (you can replace this with
your own known face embeddings)
# For example, you can store known face embeddings and corresponding
names in a dictionary.
known_face_encodings = [... ] # List of known face encodings
known_face_names = [...] # List of corresponding names
# Initialize the webcam or provide the path to the video file
video_capture = cv2.VideoCapture(0) # Change to desired camera index or
video file path
while True:
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

faces#Detect=facefaces_detector(frame)usingOpenCV

for face in faces:

shape#Getfacial= landmarks
dlib.shape_predictor("dlib_models/shape_predictor_68_face_landmarks.dat")(frame,

face)

# Recognize faces
face_encoding = face_recognizer.compute_face_descriptor(frame, shape)

# Compare with known faces

namematches="Unknown"=dlib.compare_faces(known_face_encodings, face_encoding)

if True in matches:

namematch=_indexknown=_matchesface_names[match.index(True)_index]

# Draw a box around the face and display the name


lOMoARcPSD|40491420

top, right, bottom, left = face.left(), face.right(), face.bottom(), face.top()


cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.putText(frame, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (255, 255, 255), 1)

cv2.imshow('Video', frame)

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

# Release the video capture object and close the OpenCV


windows video_capture.release()
cv2.destroyAllWindows()

Output :

Result:
Upon running the program, the system will open a video stream capturing frames
from the webcam or a video file. It will then perform facial detection and
recognition in real-time on each frame.
lOMoARcPSD|40491420

7.Detect motion in a video surveillance system

Aim:
The aim of this program is to detect motion in a video surveillance system. It will
identify and highlight areas where significant motion occurs within the video feed.

Procedure :
1. Setting up the Environment:
• Install Required Libraries: Use pip to install OpenCV, a powerful library for
computer vision tasks.
2. Accessing Video Stream:
• Open Video Stream: Use OpenCV to access the video stream from a camera
or a video file.
3. Motion Detection Algorithm:
• Background Subtraction: Apply background subtraction techniques (e.g.,
cv2.createBackgroundSubtractorMOG2) to extract the moving foreground
from the static background.
• Thresholding and Contour Detection: Use thresholding to create a binary image
highlighting motion. Detect contours to identify distinct areas of motion.
• Bounding Box or Highlight: Draw bounding boxes or highlight the areas
where motion is detected in the frames.
4. Event Detection and Visualization:
• Iterate Through Frames: Process each frame of the video stream.
• Detect Motion: Apply the motion detection algorithm to identify areas of
motion in each frame.
• Display Motion Areas: Show the processed frames with highlighted motion
areas or bounding boxes.
lOMoARcPSD|40491420

Program :
import cv2

background#InitializeBackground_subtractor =Subtractorcv2.createBackgroundSubtractorMOG2()(Changeparametersbasedonrequirements)

cap#Open=cv2.VideoCapture(0)videocapturestream (0 for webcam, or provide a path to a video file)

while True:

ifret,notframeret: = cap.read()
break

# Apply background subtraction to detect motion

fg_mask = background_subtractor.apply(frame)

# Thresholding to obtain binary image


_, thresh = cv2.threshold(fg_mask, 50, 255, cv2.THRESH_BINARY)

# Find contours of motion areas


contours, _ = cv2.findContours(thresh,

cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Draw bounding boxes around detected motion

areas for contour in contours:


cv2.rectangle(frame,x,y,w,h=cv2.boundingRect(contour)(x,y),(x+w,y+h), (0, 255, 0), 2)

cv2.imshow('Motion#Displayprocessed frameDetection', frame)

# Exit on 'q' press

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

# Release video capture and close OpenCV


windows cap.release()
cv2.destroyAllWindows()
lOMoARcPSD|40491420

Output:

Result:
The program will process the video stream, highlighting areas where motion is
detected in real-time or while analyzing a pre-recorded video. It will display the
video feed with the identified motion regions or bounding boxes.

You might also like