Get超炫技能:如何使用 Python 执行运动检测?(1)

  1. 然后我们将使用无限循环来捕获视频中的每一帧。
  2. 我们将使用 read() 方法读取每一帧并将它们存储到各自的变量中。
  3. 我们定义了一个可变运动并将其初始化为零。
  4. 我们使用 cv2 函数 cvtColor 和 GaussianBlur 创建了另外两个变量 grayImage 和 grayFrame 来查找运动的变化。
  5. 如果我们的 initialState 是 None 那么我们将当前的 grayFrame 分配给 initialState 否则并使用“continue”关键字停止下一个进程。
  6. 在下一节中,我们计算了我们在当前迭代中创建的初始帧和灰度帧之间的差异。
  7. 然后我们将使用 cv2 阈值和扩张函数突出显示初始帧和当前帧之间的变化。
  8. 我们将从当前图像或帧中的移动对象中找到轮廓,并通过使用矩形函数在其周围创建绿色边界来指示移动对象。
  9. 在此之后,我们将通过添加当前检测到的元素来附加我们的motionTrackList。
  10. 我们使用imshow方法显示了所有的帧,如灰度和原始帧等。
  11. 此外,我们使用 cv2 模块的 witkey() 方法创建了一个键来结束进程,我们可以使用“m”键来结束我们的进程。
# starting the webCam to capture the video using cv2 module 
video = cv2.VideoCapture(0)  

# using infinite loop to capture the frames from the video 
while True:  

   # Reading each image or frame from the video using read function 

   check, cur_frame = video.read()  

   

   # Defining 'motion' variable equal to zero as initial frame 

   var_motion = 0  

   

   # From colour images creating a gray frame 

   gray_image = cv2.cvtColor(cur_frame, cv2.COLOR_BGR2GRAY)  

   

   # To find the changes creating a GaussianBlur from the gray scale image 

   gray_frame = cv2.GaussianBlur(gray_image, (21, 21), 0)  

   

   # For the first iteration checking the condition

   # we will assign grayFrame to initalState if is none 

   if initialState is None:  

       initialState = gray_frame  

       continue  

       

   # Calculation of difference between static or initial and gray frame we created 

   differ_frame = cv2.absdiff(initialState, gray_frame)  

   

   # the change between static or initial background and current gray frame are highlighted 

   

   thresh_frame = cv2.threshold(differ_frame, 30, 255, cv2.THRESH_BINARY)[1]  

   thresh_frame = cv2.dilate(thresh_frame, None, iterations = 2)  

   

   # For the moving object in the frame finding the coutours 

   cont,_ = cv2.findContours(thresh_frame.copy(),   

     
  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值