赞
踩
- import cv2
-
- """
- 背景和需要跟踪的物体差异很大
- """
-
- # 获取视频
- video = cv2.VideoCapture('../opencv/20210423_164452.mp4')
-
- # 生成椭圆结构元素
- es = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9, 4))
- # 设置背景帧
- background = None
-
- while True:
- # 读取视频每一帧
- ret, frame = video.read()
- print(ret)
-
- # 获取背景帧
- if background is None:
- # 将视频的第一帧图像转为灰度图
- background = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- # 对灰度图进行高斯模糊,平滑图像
- background = cv2.GaussianBlur(background, (21, 21), 0)
- continue
- if ret:
- # 将视频的每一帧图像转为灰度图
- gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
-
- # 对灰度图进行高斯模糊,平滑图像
- gray_frame = cv2.GaussianBlur(gray_frame, (21, 21), 0)
-
- # 获取当前帧与背景帧之间的图像差异,得到差分图
- diff = cv2.absdiff(background, gray_frame)
-
- # 利用像素点值进行阈值分割,得到一副黑白图像
- diff = cv2.threshold(diff, 25, 255, cv2.THRESH_BINARY)[1]
-
- # 膨胀图像,减少错误
- diff = cv2.dilate(diff, es, iterations=2)
-
- # 得到图像中的目标轮廓
- cnts, hierarchy = cv2.findContours(diff.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
-
- for c in cnts:
- if cv2.contourArea(c) < 1500:
- continue
- # 绘制目标矩形框
- (x, y, w, h) = cv2.boundingRect(c)
- cv2.rectangle(frame, (x+2, y+2), (x+w, y+h), (0, 255, 0), 2)
-
- # 显示检测视频
- cv2.namedWindow('contours', 0)
- cv2.resizeWindow('contours', 600, 400)
- cv2.imshow('contours', frame)
-
- # 显示差异视频
- cv2.namedWindow('diff', 0)
- cv2.resizeWindow('diff', 600, 400)
- cv2.imshow('diff', diff)
- if cv2.waitKey(1) & 0xff == ord('q'):
- break
- else:
- break
- # 结束
- cv2.destroyAllWindows()
- video.release()
背景分割器
- import cv2
-
- # 获取视频
- video = cv2.VideoCapture('../opencv/20210423_164452.mp4')
- # KNN背景分割器,设置阴影检测
- bs = cv2.createBackgroundSubtractorKNN(detectShadows=True)
-
- while True:
- # 读取视频每一帧
- ret, frame = video.read()
- # 计算视频的前景掩码
- if ret:
- fgmask = bs.apply(frame)
- # 图像阈值化
- th = cv2.threshold(fgmask.copy(), 244, 255, cv2.THRESH_BINARY)[1]
- # 膨胀图像,减少错误
- dilated = cv2.dilate(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), iterations=2)
-
- # 得到图像中的目标轮廓
- contours, hier = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
-
- for c in contours:
- if cv2.contourArea(c) > 1600:
- # 绘制目标矩形框
- (x, y, w, h) = cv2.boundingRect(c)
- cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 255, 0), 2)
-
- # 显示差异视频
- cv2.imshow('mog', fgmask)
- # cv2.imshow('thresh', th)
- # 显示检测视频
- cv2.imshow('detection', frame)
- if cv2.waitKey(1) & 0xff == ord('q'):
- break
- else:
- break
-
- video.release()
- cv2.destroyAllWindows()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。