基于OpenCV实战:动态物体检测
#Applies Threshold and converts it to black & white image
thresh_delta = cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1]
thresh_delta = cv2.dilate(thresh_delta, None, iterations=0)
#finding contours on the white portion(made by the threshold)
cnts,_ = cv2.findContours(thresh_delta.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
最后一个命令在该黑白图像中查找轮廓,并给出用于创建边界框的坐标,如上面的视频所示。使用运动检测的好处:
- 它不会保存无用的空闲镜头。因此,减少了其他算法的工作量,因为不会保存空闲帧进行处理。
- 它需要较少的计算,并且适合实时实施。
阻碍与解决方案
给定的因素导致轮廓检测不理想,运动检测的幼稚方法会在执行开始时为所有比较保存第一帧。不好有几个原因:
- 白天的照明条件可能会改变。
- 天气变化。
- 执行时相机被遮挡。
解决方案:在没有运动的情况下,可以通过定期定期更新保存的帧来轻松解决此问题。
# Number of idle frames to pass before changing the saved frame
# for further comparisions
FRAMES_TO_PERSIST = 1000
然后将其放在while循环中:
#increment delay counter for every idle frame
delay_counter += 1
#Update the saved first frame
if delay_counter > FRAMES_TO_PERSIST:
delay_counter = 0
first_frame = next_frame
当检测到运动时,将delay_counter设置为零,微小的物体(例如蜜蜂和昆虫)和通常不必要的轻微不必要的运动被存储起来。解决方案:如片段所示,我们应该在该区域设置一个阈值。
# Minimum boxed area(in pixels) for a detected motion to count as actual motion
# Use to filter out noise or small objects
MIN_SIZE_FOR_MOVEMENT = 2000
然后在while循环中放置一个if语句:
#Checks if the area is big enough to be considered as motion.
if cv2.contourArea(c) > MIN_SIZE_FOR_MOVEMENT:
#Your code
各种平台上的基准:
所有这些都是针对同一视频(30-fps,1280x720)计算的。
Raspberry Pi 2:
- 规格
- 1.5 GHz处理器
- 1 GB内存
- 没有GPU
- FPS:每秒8.08帧
Jetson Nano:
- 规格
- 四核ARM处理器1.43Ghz
- 2 Gb内存
- GPU:128核心Nvidia Maxwell
- FPS:每秒33帧
个人电脑 :
- 规格
- i7第八代处理器
- 16 GB内存
- GTX 1060 6 GB GPU
- FPS:每秒37帧
潜在应用:
智能铃:
如果有人站在您家门口,它将自动触发铃声,并向您发送提示。
潜在威胁警报:
如果有人站在您家门前的时间长于正常时间,它将提醒您。
结论
在本文中,我们实现了一个非常基本但重要的算法,可用于有效运行所有其他算法。可以对该运动检测算法进行更多修改,以使其更加健壮。