Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Implement the lab sheets given below Motion Detection using Frame Differencing import cv 2 import numpy as np # I n i t i a
Implement the lab sheets given below
Motion Detection using Frame Differencing
import cv
import numpy as np
# I n i t i a l i z e video capture
# Capture video from a f i l e or a devi c e
cap cv VideoCapture pathtovideo mp
# Read two c ons e cut i v e frames to i n i t i a l i z e background
ret frame cap read
ret frame cap read
whi l e True :
# Perform frame d i f f e r e n c i n g
d i f f cv a b s d i f f frame frame
gray cv cvtColor d i f f cvCOLORBGRGRAY
blur cv GaussianBlur gray
thr e sh cv thr e sho ld blur cvTHRESHBINARY
d i l a t e d cv d i l a t e thresh None i t e r a t i o n s
contours cv findContours di l a t ed cvRETRTREE, cvCHAINAPPROXSIMPLE
# Draw r e c t a n g l e s around moving o b j e c t s
f o r contour in contour s :
x y w h cv boundingRect contour
i f cv contourArea contour : # Adjust area thr e sho ld as needed
cont inue
cv r e c t a n g l e framex y x w y h
# Display r e s u l t
cv imshowMotion De t e c t ion frame
# Update frames
frame frame
ret frame cap read
# Exit on q pr e s s
i f cv waitKey & xFF ord q :
break
# Re l eas e capture and c l o s e windows
cap r e l e a s e
cv destroyAllWindows
LucasKanade method for motion estimation
import cv
import numpy as np
# Capture video from a f i l e or a devi c e
cap cv VideoCapture pathtovideo mp
# Read the f i r s t frame
ret prevframe cap read
# Convert frame to g r a y s c a l e
prevgray cv cvtColor prevframe cvCOLORBGRGRAY
# Create LucasKanade parameters
lkparams d i c t winSize
maxLevel
c r i t e r i a cvTERMCRITERIAEPS cvTERMCRITERIACOUNT,
whi l e True :
# Read the cur r ent frame
ret frame cap read
i f not r e t :
break
# Convert frame to g r a y s c a l e
framegray cv cvtColor frame cvCOLORBGRGRAY
# Ca l cul a t e o p t i c a l flow us ing LucasKanade method
# Find point cor r e spondenc e s between pr evi ous and cur r ent frames
p p cv calcOpticalFlowPyrLK prevgray framegray None None
lkparams
# S e l e c t good po int s
goodnew p p i s not None
goodold p p i s not None
# Draw motion v e c t o r s
f o r i new old in enumerate z ip goodnew goodold :
a b new. r a v e l astype i n t
c d old r a v e l astype i n t
frame cv l i n e frame a b c d
frame cv c i r c l e frame a b
# Display the r e s u l t i n g frame with motion v e c t o r s
cv imshow Opt i cal Flow frame
# Exit on q pr e s s
i f cv waitKey & xFF ord q :
break
# Update pr evi ous frame and pr evi ous po int s
prevgray framegray copy
# Re l eas e capture and c l o s e windows
cap r e l e a s e
cv destroyAllWindows
Video Segmentation:import cv
# Capture the video f e ed
cap cv VideoCapture s u r v e i l l a n c ev i d e o mp
# I n i t i a l i z e the background subt r a c t o r
fgbg cv createBackgroundSubtractorMOG
whi l e True :
ret frame cap read
i f not r e t :
break
# Apply background subt r a c t i on
fgmask fgbg apply frame
# Thresholding
thr e sh cv thr e sho ld fgmask cvTHRESHBINARY
# Display the o r i g i n a l frame and the r e s u l t
cv imshow Or i g ina l Frame frame
cv imshow Foreground Mask thr e sh
i f cv waitKey & xFF :
break
cap r e l e a s e
cv destroyAllWindows
Instructions:
You are free to use TensorflowKeras for the implementation.
Use one video file which is min or less.
Section A:
List all the dependencies necessary for running the code and specify their versions, in the very beginning of the notebook.
Module Motion Detection and Motion Estimation code
Module Video Segmentation code
write code for implementing Module Motion Tracking in Video code
Module Video Indexing code
Module Video Summarization code
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started