简介
本篇博文将实现基于python的运动物体检测。
依赖库
opencv-python
基本动作检测
在计算机视觉中,我们把运动看作是环境的变化。为了计算转换,我们必须有一个背景图像来比较。所以,我们在程序的开头保存第一个图像。
# Converting the image to GrayScale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray,(21,21),0)
# Saving the First Frame
if first_frame is None:
first_frame = gray
continue
然后,我们将后续帧与保存的第一帧进行比较,以观察差异。计算完差异后,我们可以应用阈值将其转换为黑白图像。
#Calculates difference to detect motion
delta_frame = cv2.absdiff(first_frame, gray)
#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
完整代码
import cv2
import numpy as np
import time
class VideoReader(object):
def __init__(self, file_name):
self.file_name = file_name
try: # OpenCV needs int to read from webcam
self.file_name = int(file_name)
except ValueError:
pass
def __iter__(self):
self.cap = cv2.VideoCapture(self.file_name)
if not self.cap.isOpened():
raise IOError('Video {} cannot be opened'.format(self.file_name))
return self
def __next__(self):
was_read, img = self.cap.read()
if not was_read:
raise StopIteration
return img
if __name__ == '__main__':
frame_provider = VideoReader(0)
FRAMES_TO_PERSIST = 50
MIN_SIZE_FOR_MOVEMENT = 200
delay_counter = 0
first_frame = None
delay = 1
for frame in frame_provider:
start = time.time()
# Converting the image to GrayScale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
# Saving the First Frame
if first_frame is None:
first_frame = gray
continue
# Calculates difference to detect motion
delta_frame = cv2.absdiff(first_frame, gray)
# 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)
if np.sum(np.where(thresh_delta > 0, 1, 0)) < MIN_SIZE_FOR_MOVEMENT:
thresh_delta = np.zeros_like(gray)
end = time.time()
seconds = end - start
# Calculate frames per second
print(f"seconds: {seconds}")
fps = 1 / seconds
imgVis = np.dstack([thresh_delta for _ in range(3)])
cv2.putText(imgVis, f"FPS: {int(fps)}", (50, 50),
cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255))
cv2.imshow('Lightweight Human Pose Estimation Python Demo', imgVis)
key = cv2.waitKey(delay)
if key == 27: # esc
continue
elif key == 112: # 'p'
if delay == 1:
delay = 0
else:
delay = 1
# 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 = gray