Answered step by step
Verified Expert Solution
Link Copied!

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 cv2
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 = cv2. VideoCapture ( path_to_video .mp4)
# Read two c ons e cut i v e frames to i n i t i a l i z e background
ret , frame1= cap . read ()
ret , frame2= cap . read ()
whi l e True :
# Perform frame d i f f e r e n c i n g
d i f f = cv2. a b s d i f f ( frame1, frame2)
gray = cv2. cvtColor ( d i f f , cv2.COLOR_BGR2GRAY)
blur = cv2. GaussianBlur ( gray ,(5,5),0)
_, thr e sh = cv2. thr e sho ld ( blur ,20,255, cv2.THRESH_BINARY)
d i l a t e d = cv2. d i l a t e ( thresh , None , i t e r a t i o n s =3)
contours ,_= cv2. findContours ( di l a t ed , cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
)
# 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)= cv2. boundingRect ( contour )
i f cv2. contourArea ( contour )<900: # Adjust area thr e sho ld as needed
cont inue
cv2. r e c t a n g l e ( frame1,(x , y ),( x + w, y + h),(0,255,0),2)
# Display r e s u l t
cv2. imshow(Motion De t e c t ion , frame1)
# Update frames
frame1= frame2
ret , frame2= cap . read ()
# Exit on q pr e s s
i f cv2. waitKey (1) & 0xFF == ord ( q ) :
break
# Re l eas e capture and c l o s e windows
cap . r e l e a s e ()
cv2. destroyAllWindows ()
2.1 Lucas-Kanade method for motion estimation
import cv2
import numpy as np
# Capture video from a f i l e or a devi c e
cap = cv2. VideoCapture ( path_to_video .mp4)
# Read the f i r s t frame
ret , prev_frame = cap . read ()
# Convert frame to g r a y s c a l e
prev_gray = cv2. cvtColor ( prev_frame , cv2.COLOR_BGR2GRAY)
# Create LucasKanade parameters
lk_params = d i c t ( winSize=(15,15),
maxLevel=2,
c r i t e r i a =(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,
10,0.03))
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
frame_gray = cv2. cvtColor ( frame , cv2.COLOR_BGR2GRAY)
# 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
p1, p2,_= cv2. calcOpticalFlowPyrLK ( prev_gray , frame_gray , None , None ,**
lk_params )
# S e l e c t good po int s
good_new = p2[ p1 i s not None ]
good_old = p1[ p1 i s not None ]
# Draw motion v e c t o r s
f o r i ,(new , old ) in enumerate ( z ip ( good_new , good_old )) :
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 = cv2. l i n e ( frame ,( a , b),( c , d),(0,255,0),2)
frame = cv2. c i r c l e ( frame ,( a , b),5,(0,0,255),1)
# Display the r e s u l t i n g frame with motion v e c t o r s
cv2. imshow( Opt i cal Flow , frame )
# Exit on q pr e s s
i f cv2. waitKey (1) & 0xFF == ord ( q ) :
break
# Update pr evi ous frame and pr evi ous po int s
prev_gray = frame_gray . copy ()
# Re l eas e capture and c l o s e windows
cap . r e l e a s e ()
cv2. destroyAllWindows ()
Video Segmentation:import cv2
# Capture the video f e ed
cap = cv2. VideoCapture ( s u r v e i l l a n c e_v i d e o .mp4)
# I n i t i a l i z e the background subt r a c t o r
fgbg = cv2. createBackgroundSubtractorMOG2()
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 = cv2. thr e sho ld ( fgmask ,127,255, cv2.THRESH_BINARY)
# Display the o r i g i n a l frame and the r e s u l t
cv2. imshow( Or i g ina l Frame , frame )
cv2. imshow( Foreground Mask , thr e sh )
i f cv2. waitKey (30) & 0xFF ==27 :
break
cap . r e l e a s e ()
cv2. destroyAllWindows ()
Instructions:
You are free to use Tensorflow/Keras for the implementation.
Use one video file which is 1min 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 2 Motion Detection and Motion Estimation code
Module 4 Video Segmentation code
write code for implementing Module 5 Motion Tracking in Video code
Module 6 Video Indexing code
Module 7 Video Summarization code

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Compare levels of resolution in conflict outcomes?

Answered: 1 week ago

Question

Strategies for Managing Conflict Conflict Outcomes?

Answered: 1 week ago