OpenCV Python背景消去

import cv2
import numpy as np


#读入图像
video = cv2.VideoCapture("E:\\video.avi")
videoIsOpen=video.isOpened
print(videoIsOpen)
width=int(video.get(cv2.CAP_PROP_FRAME_WIDTH))#宽度
height=int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))#高度
fps=video.get(cv2.CAP_PROP_FPS)#获取帧率
print(fps,width,height)
#创建窗口
cv2.namedWindow('MOG2')
cv2.namedWindow('MOG22')
cv2.namedWindow('input video')
#cv2.namedWindow('KNN')
bsmaskMOG2 = np.zeros([height,width],np.uint8)
bsmaskKnn = np.zeros([height,width],np.uint8)
#两种消除的方案
pMOG2 = cv2.createBackgroundSubtractorMOG2(detectShadows=True)
PKNN = cv2.createBackgroundSubtractorKNN(detectShadows=True)
#形态学处理
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3, 3))

while videoIsOpen:
    (flag,frame)=video.read()
    if not flag:
        break
    cv2.imshow('input video',frame)
    # bsmaskKnn= PKNN.apply(frame)
    # cv2.imshow('KNN',bsmaskKnn)
    bsmaskMOG2 = pMOG2.apply(frame)
    cv2.imshow('MOG22',bsmaskMOG2)
    OPEND=cv2.morphologyEx(bsmaskMOG2,cv2.MORPH_OPEN,kernel)
    cv2.imshow('MOG2',OPEND)
    
    c = cv2.waitKey(40)
    if c==27:
        break
video.release()

cv2.waitKey(0)

 

你可能感兴趣的:(跟踪算法)