13 KNN背景分割器
传统的前景背景分割方法有GrabCut,分水岭算法,当然也包括一些阈值分割的算法。但是这些算法在应用中往往显得鲁棒性较弱,达不到一个好的分割效果。
现代的背景分割算法融入了机器学习的一些方法来提高分类的效果。如KNN,混合高斯(MOG2),Geometric Multigrid。这些算法的基本原理就是对每一帧图像的环境进行学习,从而推断出背景区域。
opencv的BackgroundSubtractor提供了这些现代的背景分割算法。
1.思想
1.定义1个KNN背景分割器对象
2.定义视频对象 while True: 3.一帧帧读取视频
4.计算前景掩码 5.二值化操作
6.膨胀操作 7.查找轮廓
8.轮廓筛选
9.画出轮廓(在原图像) 10.显示图像帧,
2。代码
#-*- coding:utf-8 -*-
import cv2
import numpy as np # 1.常见一个BackgroundSubtractorKNN接口
bs = cv2.createBackgroundSubtractorKNN(detectShadows=True) #2.读取视频
camera = cv2.VideoCapture('traffic.flv') #定义卷积核圆形
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)) while True:
ret,frame = camera.read() #3. apply()函数计算了前景掩码
fgmask = bs.apply(frame) #4. 获得前景掩码(含有白色值以及阴影的灰色值),通过设定阈值将非白色(244~255)的所有像素都设为0,而不是1;
th = cv2.threshold(fgmask.copy(),244,255,cv2.THRESH_BINARY)[1] #二值化操作 dilated = cv2.dilate(th,kernel,iterations =2) #5.膨胀操作
#cv2.getStructuringElement 构建一个椭圆形的核
#3x3卷积核中有2个1那就设置为1 #6. findContours函数参数说明cv2.RETR_EXTERNAL只检测外轮廓,
# cv2.CHAIN_APPROX_SIMPLE只存储水平,垂直,对角直线的起始点。
image,contours,hier = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) #查找轮廓 for c in contours: #从list列表取出每个轮廓
if cv2.contourArea(c) < 1500: #进行轮廓筛选 轮廓面积小于1500
continue (x,y,w,h) = cv2.boundingRect(c)
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2) cv2.imshow("mog",fgmask)
cv2.imshow("thresh",th)
cv2.imshow("detection",frame) if cv2.waitKey(100) & 0xff == ord("q"):
break camera.release()
cv2.destroyAllWindows()
# coding:utf-8
import cv2
import numpy as np
#from MyCvUtils import MyCvUtils #datapath = "D:/imgData/video/" bs = cv2.createBackgroundSubtractorKNN(detectShadows=True)
camera = cv2.VideoCapture("traffic.flv") ret, frame = camera.read() while True:
ret, frame = camera.read()
# 计算前景掩码,包含 前景的白色值 以及 阴影的灰色值
fgmask = bs.apply(frame)
# 前景区域二值化,将非白色(0-244)的非前景区域(包含背景以及阴影)均设为0,前景的白色(244-255)设置为255
th = cv2.threshold(fgmask.copy(), 244, 255, cv2.THRESH_BINARY)[1]
# 前景区域形态学处理
th = cv2.erode(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), iterations=2)
dilated = cv2.dilate(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (8, 3)), iterations=2)
# 绘制前景图像的轮廓矩形
image, contours, hier = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
# 对轮廓设置最小区域,对检测结果降噪
if cv2.contourArea(c) > 1000:
(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("diff", frame & cv2.cvtColor(fgmask, cv2.COLOR_GRAY2BGR))
cv2.imshow("detection", frame) if (cv2.waitKey(30) & 0xFF) == 27:
break
if (cv2.waitKey(30) & 0xFF) == ord('q'):
break camera.release()
cv2.destroyAllWindows()
3.效果图
4.源码KNN
def createBackgroundSubtractorKNN(history=None, dist2Threshold=None, detectShadows=None): # real signature unknown; restored from __doc__
"""
createBackgroundSubtractorKNN([, history[, dist2Threshold[, detectShadows]]]) -> retval
. @brief Creates KNN Background Subtractor . @param history
Length of the history.
. @param dist2Threshold
Threshold on the squared distance between the pixel and the sample to decide whether a pixel is close to that sample. This parameter does not affect the background update.
. @param detectShadows
If true, the algorithm will detect shadows and mark them. It decreases the speed a bit, so if you do not need this feature, set the parameter to false.
"""
pass
@简单创建KNN背景减法器 @param历史
历史长度。 @param dist2threshold
阈值像素和样本之间的平方距离,以决定像素是否接近该样本。此参数不影响后台更新。 @param detectshadows
如果为真,该算法将检测阴影并标记它们。它会稍微降低速度,所以如果您不需要这个特性,请将参数设置为false。
13 KNN背景分割器的更多相关文章
- Opencv中KNN背景分割器
背景分割器BackgroundSubtractor是专门用来视频分析的,会对视频中的每一帧进行"学习",比较,计算阴影,排除检测图像的阴影区域,按照时间推移的方法提高运动分析的结果 ...
- python opencv3 背景分割 mog2 knn
git:https://github.com/linyi0604/Computer-Vision 使用mog2算法进行背景分割 # coding:utf-8 import cv2 # 获取摄像头对象 ...
- Qt之四方分割器QuadSplitter
在Qt经常会用到分割器QSplitter,可以对多个控件进行水平或者垂直分割,但有一些特殊的需求无法满足,比如:四方分割...QuadSplitter是qt-apps里面的一个应用,挺不错的,拿来和大 ...
- Qt布局与分割器QSplitter
Qt的布局方式主要有四种: QGridLayout 栅格布局 QFormLayout 表格布局 QHBoxLayout 水平布局 QVBoxLayout ...
- JAVA学习课第五 — IO流程(九)文件分割器合成器
文件分割器 private static final int SIZE = 1024 *1024; public static void splitFile(File file) throws IOE ...
- C#txt文本分割器
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 用duilib制作仿QQ2013动态背景登录器
转载请说明原出处,谢谢~~ 在上一篇博客里,我修复了CActiveXUI控件的bug,从而可以使用flash动画来制作程序的背景,这篇博客说明一下应该怎么使用CActiveXUI控件创建透明无窗体的背 ...
- [开源]基于WPF实现的Gif图片分割器,提取GIf图片中的每一帧
不知不觉又半个月没有更新博客了,今天终于抽出点时间,来分享一下前段时间的成果. 在网上,我们经常看到各种各样的图片,尤其是GIF图片的动态效果,让整个网站更加富有表现力!有时候,我们看到一些比较好看的 ...
- echarts背景分割区域填充不同颜色(x轴为time),实时刷新
先来看下图片吧,这是实现效果: 思路: 因为要实时刷新,可以使用setInterval(),但是要控制好定时器的起与停,否则容易错乱以及页面卡死: 主要就是利用定时器五秒刷新,重绘echarts图:= ...
随机推荐
- Struts2_访问Web元素
取得Map 类型的 request,session,application, HttpServletRequest,HttpSession,ServletContext的引用. 分访问 Map 类型和 ...
- Quick How-To deny/allow IP using iptables
How to block an IP using iptables? iptables -A INPUT -s xx.xx.xx.xx -j DROP How to block an IP for a ...
- kubernetes组件helm
1.安装helm Helm由客户端helm命令行工具和服务端tiller组成,Helm的安装十分简单. 下载helm命令行工具到master节点node1的/usr/local/bin下(只需要在其中 ...
- leetcode: 复杂度
1. single-number Given an array of integers, every element appears twice except for one. Find that s ...
- 332. Reconstruct Itinerary (leetcode)
1. build the graph and then dfs -- graph <String, List<String>>, (the value is sorted a ...
- robotframework实战一
1.环境 1.Robotframework 安装环境,见以下帖子,只要保证python安装成功,安装了python的pip,就可以使用pip一路安装下去了.以下帖子也有基础的操作,帖子路径 http: ...
- nodejs使用MYSQL连接池,断线重连
两种方式解决1.你可以配置mysql的连接池 var mysql = require('mysql'); var pool = mysql.createPool({ host: 'localhost' ...
- php无法保存SESSION问题总汇
昨天客户又过来说网站的问题,说的也都是些毛毛雨的东西,管理那么多网站,再有这么些客户的存在,本人也是累了,但当登录后台的时候突然发现后台登录不了,查看了一下验证码服务器端的session为空值,之前登 ...
- P1217 [USACO1.5]回文质数 Prime Palindromes
题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围[a,b](5 <= a < b <= 100,000 ...
- _default_ VirtualHost overlap on port 80, the first has precedence
去掉#NameVirtualHost *:80,然后重启httpd