資源簡介
car_detected.py
代碼片段和文件信息
#coding=utf-8
import?time
import?cv2
import?numpy?as?np
bs?=?cv2.createBackgroundSubtractorKNN(detectShadows?=?True)
#camera?=?cv2.VideoCapture(0)?#獲取攝像頭
camera?=?cv2.VideoCapture(“video.avi“)
frame_num?=?200
start_time?=?time.time()
#while?True:
for?_?in?range(frame_num):
ret?frame?=?camera.read()
frame?=?cv2.resize(frame?(int(700*frame.shape[1]/frame.shape[0])?700))
fgmask?=?bs.apply(frame)
fg2?=?fgmask.copy()
#?threshold?固定閾值二值化
th?=?cv2.threshold(fg2150255cv2.THRESH_BINARY)[1]
#?膨脹(dilate)和腐蝕(erode)
dilated?=?cv2.dilate(thcv2.getStructuringElement(cv2.MORPH_ELLIPSE(33))iterations?=?2)
#findContours繪制輪廓輪廓
image?contours?hier?=?cv2.findContours(dilatedc
評論
共有 條評論