python+opencv 運動檢測技術 基礎入門(Motion Detection Techniques with pyrhon and OpenCV)
python+opencv 運動檢測技術 基礎入門(Motion Detection Techniques with pyrhon and OpenCV)
資料來源: https://mp.weixin.qq.com/s/_eEn9hN5CDZjhlSfmPoqNg
https://github.com/safaabbes/Motion-Detection-Techniques-using-OpenCV
GITHUB: https://github.com/jash-git/Motion-Detection-Techniques-with-pyrhon-and-OpenCV
文字重點摘要
01. 幀差分(畫面相減)
02. 背景減法
03. 自適應背景減法
04. 高斯混合(MoG)
code
import cv2 frames=[] MAX_FRAMES = 1000 N = 2 THRESH = 60 ASSIGN_VALUE = 255 #Value to assign the pixel if the threshold is met cap = cv2.VideoCapture(0) #Capture using Computer's Webcam for t in range(MAX_FRAMES): #Capture frame by frame ret, frame = cap.read() #Convert frame to grayscale frame_gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) #Append to list of frames frames.append(frame_gray) if t >= N: #D(N) = || I(t) - I(t+N) || = || I(t-N) - I(t) || diff = cv2.absdiff(frames[t-N], frames[t]) #Mask Thresholding threshold_method = cv2.THRESH_BINARY ret, motion_mask = cv2.threshold(diff, THRESH, ASSIGN_VALUE, threshold_method) #Display the Motion Mask cv2.imshow('Motion Mask', motion_mask) #Exit if cv2.waitKey(1) == ord('e') or not ret: break #When everything is finished, we release the capture cap.release() cv2.destroyAllWindows()
import cv2 background = None MAX_FRAMES = 1000 THRESH = 60 ASSIGN_VALUE = 255 cap = cv2.VideoCapture(0) for t in range(MAX_FRAMES): # Capture frame-by-frame ret, frame = cap.read() # Convert frame to grayscale frame_gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) if t == 0: # Train background with first frame background = frame_gray else: # Background subtraction diff = cv2.absdiff(background, frame_gray) # Mask thresholding ret, motion_mask = cv2.threshold(diff, THRESH, ASSIGN_VALUE, cv2.THRESH_BINARY) # Display the motion mask and background cv2.imshow('Motion mask', motion_mask) cv2.imshow('Background', background) # Exit if cv2.waitKey(1) == ord('e') or not ret: break # When everything done, release the capture cap.release() cv2.destroyAllWindows()
import numpy as np import cv2 background = None MAX_FRAMES = 1000 THRESH = 60 ASSIGN_VALUE = 255 ALPHA = 0.1 def update_background(current_frame, prev_bg, alpha): bg = alpha * current_frame + (1 - alpha) * prev_bg bg = np.uint8(bg) return bg cap = cv2.VideoCapture(0) for t in range(MAX_FRAMES): # Capture frame-by-frame ret, frame = cap.read() # Convert frame to grayscale frame_gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) if t == 0: # Train background with first frame background = frame_gray else: # Background subtraction diff = cv2.absdiff(background, frame_gray) # Mask thresholding ret, motion_mask = cv2.threshold(diff, THRESH, ASSIGN_VALUE, cv2.THRESH_BINARY) # Update background background = update_background(frame_gray, background, alpha = ALPHA) # Display the motion mask and background cv2.imshow('Motion mask', motion_mask) cv2.imshow('Background', background) # Exit if cv2.waitKey(1) == ord('e') or not ret: break # When everything done, release the capture cap.release() cv2.destroyAllWindows()
import cv2 MAX_FRAMES = 1000 LEARNING_RATE = -1 fgbg = cv2.createBackgroundSubtractorMOG2() cap = cv2.VideoCapture(0) for t in range(MAX_FRAMES): # Capture frame-by-frame ret, frame = cap.read() #Apply MOG motion_mask = fgbg.apply(frame, LEARNING_RATE) #Get background background = fgbg.getBackgroundImage() # Display the motion mask and background cv2.imshow('background', background) cv2.imshow('Motion Mask', motion_mask) # Exit if cv2.waitKey(1) == ord('e') or not ret: break # When everything done, release the capture cap.release() cv2.destroyAllWindows()
圖文完整教學