# #
# # # -*- coding: utf-8 -*-
# # import numpy as np
# # import cv2
# #
# # #第三问加一个去除噪声的处理
# #
# # #BackgroundSubtractorMOG2
# # #opencv自带的一个视频
# # cap = cv2.VideoCapture('/Users/vicky/Desktop/附件2-典型视频/不带晃动-静态背景/hall/input.avi',0)
# # #创建一个3*3的椭圆核
# # kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)) #形态学运算,膨胀
# # #创建BackgroundSubtractorMOG2,
# # fgbg = cv2.createBackgroundSubtractorMOG2()#视频的背景分割,Foreground Segmentation Algorithm
# # #fgbg = cv2.createBackgroundSubtractorKNN()
# # # Define the codec and create VideoWriter object
# # size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# # fourcc=cv2.VideoWriter_fourcc('X','V','I','D')
# # out = cv2.VideoWriter('output_hall.avi',fourcc,20.0,size,False)
# #
# # #fourcc = cv2.VideoWriter_fourcc(*'DIVX') # 'x264' doesn't work
# # #out = cv2.VideoWriter('output.mp4',fourcc, 29.0, size, False) # 'False' for 1-ch instead of 3-ch for color
# # #fps = cap.get(cv2.CAP_PROP_FPS)
# # #size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# # #out = cv2.VideoWriter('output_escalator.avi', -1, fps, size)
# #
# # #Count = 0
# # #ZH = []
# # while(1):
# # ret, frame = cap.read()
# # if ret==True:
# # fgmask = fgbg.apply(frame)
# # #形态学开运算去噪点
# # fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)
# # #寻找视频中的轮廓
# # # im, contours, hierarchy = cv2.findContours(fgmask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# # #
# # # for c in contours:
# # # #计算各轮廓的周长
# # # perimeter = cv2.arcLength(c,True)
# # # if perimeter > 188:
# # # #找到一个直矩形(不会旋转)
# # # x,y,w,h = cv2.boundingRect(c)
# # # #画出这个矩形
# # # cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
# # # print(Count)
# # # ZH.append(Count)
# # out.write(frame)
# # cv2.imshow('frame',frame)
# # cv2.imshow('fgmask', fgmask)
# # #k = cv2.waitKey(1) & 0xff == ord('q')
# # #if k == 300:
# # # break
# # if cv2.waitKey(25) & 0xFF == ord('q'):
# # break
# # else:
# # break
#
# # Count += 1
# # print(ZH)
# # print(np.unique(ZH))
# # print(len(np.unique(ZH)))
# cap.release()
# #cv2.destroyAllWindows() #删除任何我们建立的窗口
# # -*- coding: utf-8 -*-
# import numpy as np
# import cv2
#
# #第三问加一个去除噪声的处理
# #BackgroundSubtractorMOG2
# #opencv自带的一个视频
# cap = cv2.VideoCapture('/Users/vicky/Desktop/附件2-典型视频/不带晃动-静态背景/hall/input.avi',0)
# #创建一个3*3的椭圆核
# kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
# #创建BackgroundSubtractorMOG2
# fgbg = cv2.createBackgroundSubtractorMOG2()
# #fgbg = cv2.createBackgroundSubtractorKNN()
# # Define the codec and create VideoWriter object
# #size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# #fourcc = cv2.VideoWriter_fourcc(*'DIVX') # 'x264' doesn't work
# #out = cv2.VideoWriter('output.mp4',fourcc, 29.0, size, False) # 'False' for 1-ch instead of 3-ch for color
# #fps = cap.get(cv2.CAP_PROP_FPS)
# size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# #out = cv2.VideoWriter('output_hall.avi', -1, fps, size)Z
# fourcc=cv2.VideoWriter_fourcc('X','V','I','D')
# out = cv2.VideoWriter('output_hall.avi',fourcc,20.0,size,False)
#
#
# while(1):
# ret, frame = cap.read()
# if ret == True:
# blured1 = cv2.bilateralFilter(frame, 10, 5, 5)
# blured2 = cv2.GaussianBlur(frame, (5, 5), 0)
# blured3 = cv2.blur(frame, (5, 5))
# blured4 = cv2.medianBlur(frame, 5)
#
# img_new = frame + blured2
#
# fgmask = fgbg.apply(img_new)
# #形态学开运算去噪点
# fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)
# #寻找视频中的轮廓
# im, contours, hierarchy = cv2.findContours(fgmask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#
# for c in contours:
# #计算各轮廓的周长
# perimeter = cv2.arcLength(c,True)
# if perimeter > 188:
# #找到一个直矩形(不会旋转)
# x,y,w,h = cv2.boundingRect(c)
# #画出这个矩形
# cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
# out.write(fgmask)
# cv2.imshow('frame',frame)
# cv2.imshow('fgmask', fgmask)
# else:
# break
#
# if cv2.waitKey(25) & 0xFF == ord('q'):
# break
# cap.release()
# #cv2.destroyAllWindows()
#cap = cv2.VideoCapture('/Users/vicky/Desktop/附件3-检测前景视频/escalator/Escalator.avi')
#
## 定义解码器并创建VideoWrite对象
## linux: XVID、X264; windows:DIVX
## 20.0指定一分钟的帧数
##fourcc = cv2.VideoWriter_fourcc(*'XVID')
##fourcc=cv2.cv.CV_FOURCC('X','V','I','D')
#fourcc=cv2.VideoWriter_fourcc('X','V','I','D')
##fourCC = cv2.cv.CV_FOURCC('i','Y','U', 'V')
#size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
#out = cv2.VideoWriter('output.avi',fourcc,20.0,size)
#
#while(cap.isOpened()):
# ret, frame = cap.read()
# if ret==True:
# frame = cv2.flip(frame,0)
#
# # 写入帧
# out.write(frame)
#
# #cv2.imshow('frame',frame)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
# else:
# break
#
## 释放内存
#cap.release()
#out.release()
#cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
cap = cv2.VideoCapture('/Users/vicky/Desktop/附件3-检测前景视频/escalator/Escalator.avi')
#创建一个3*3的椭圆核
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
#创建BackgroundSubtractorMOG2
fgbg = cv2.createBackgroundSubtractorKNN()
# Define the codec and create VideoWriter object
#fps = cap.get(cv2.CAP_PROP_FPS)
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
#out = cv2.VideoWriter('output.avi', -1, fps, size)
fourcc=cv2.VideoWriter_fourcc('X','V','I','D')
#fourcc = cv2.cv.CV_FOURCC(*'avc1')
out = cv2.VideoWriter('output_Escalator.avi',fourcc,20.0,size)
while(True):
ret, frame = cap.read(-1)
if ret == True:
img_new = frame
fgmask = fgbg.apply(img_new)
# 形态学开运算去噪点
fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)
#平滑方式去除噪声,
blured1 = cv2.bilateralFilter(fgmask, 10, 5, 5)
blured2 = cv2.GaussianBlur(fgmask, (3, 3), 0)
blured3 = cv2.blur(fgmask, (5, 5))
blured4 = cv2.medianBlur(fgmask, 5)
fgmask = blured2
# 闭运算去噪点
fgmask = closing = cv2.morphologyEx(fgmask, cv2.MORPH_CLOSE, kernel)
#寻找视频中的轮廓
im, contours, hierarchy = cv2.findContours(fgmask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
#计算各轮廓的周长
perimeter = cv2.arcLength(c,True)
if perimeter > 200:
#找到一个直矩形(不会旋转)
x,y,w,h = cv2.boundingRect(c)
#画出这个矩形
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
out.write(fgmask)
cv2.imshow('frame',frame)
cv2.imshow('fgmask', fgmask)
#cv2.waitKey(0)
else:
break
if cv2.waitKey(25) & 0xFF == ord('q'):
break
out.release()
cap.release()
cv2.destroyAllWindows()