通过获取背景应用,到实现图片
# import cv2 import numpy as np cap=cv2.VideoCapture(cv2.CAP_DSHOW) #fgbg = cv2.createBackgroundSubtractorMOG2() fgbg=cv2.createBackgroundSubtractorKNN() colorlist=[(255,0,0),(0,255,255),(0,255,0),(0,0,255),(255,255,0),(255,0,255)] kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(3,3)) #ret=cap.set(3,480) while cap.isOpened(): ret,frame =cap.read() if ret: frame=cv2.cvtColor(frame,cv2.COLOR_BGR2BGRA) gray=cv2.GaussianBlur(frame,(3,3),5) mask=fgbg.apply(gray) erode=cv2.erode(mask,kernel) dilate=cv2.dilate(erode,kernel,iterations=3) close=cv2.morphologyEx(dilate,cv2.MORPH_CLOSE,kernel) cnts,h=cv2.findContours(close,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) for (i,c) in enumerate(cnts): (x,y,w,h)=cv2.boundingRect(c) if w>60 or h>60: cv2.rectangle(frame,(x,y),(x+w,y+h),colorlist[i%6],1) cv2.namedWindow("frame", 0) # 0可调大小,注意:窗口名必须imshow里面的一窗口名一直 cv2.resizeWindow("frame", 800, 600) # 设置长和宽 cv2.imshow('frame',frame) cv2.imshow('close',close) key=cv2.waitKey(1) if key==27:break else: print("NO") break cap.release() cv2.destroyAllWindows()View Code
标签:kernel,frame,cap,cv2,监控,close,视觉,动态,255 From: https://www.cnblogs.com/seemenlee/p/16879565.html