工程描述

  1. opencv2.4.8
  2. QT5

背景建模后,当有异物入侵时,把入侵的帧写到视频文件

使用BackgroundSubtractorMOG2背景建模

程序基于QT对话框

.pro

#-------------------------------------------------
#
# Project created by QtCreator --19T16::40
#
#------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, ): QT += widgets TARGET = opencvqt
TEMPLATE = app
INCLUDEPATH += f:/opencv/build/include/opencv
INCLUDEPATH += f:/opencv/build/include/opencv2
INCLUDEPATH += f:/opencv/build/include LIBS += -Lf:/opencv/build/x86/vc11/lib \
-lopencv_core248d \
-lopencv_highgui248d \
-lopencv_imgproc248d \
-lopencv_features2d248d \
-lopencv_calib3d248d \
-lopencv_contrib248d \
-lopencv_flann248d \
-lopencv_gpu248d \
-lopencv_legacy248d \
-lopencv_ml248d \
-lopencv_nonfree248d \
-lopencv_objdetect248d \
-lopencv_ocl248d \
-lopencv_photo248d \
-lopencv_stitching248d \
-lopencv_superres248d \
-lopencv_ts248d \
-lopencv_video248d \
-lopencv_videostab248d SOURCES += main.cpp\
dialog.cpp HEADERS += dialog.h FORMS += dialog.ui RESOURCES += \
img.qrc

dialog.h

#ifndef DIALOG_H
#define DIALOG_H #include <QDialog>
#include <QTimer>
#include <QPixmap>
#include <QDebug>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/opencv.hpp>
using namespace cv;
namespace Ui {
class Dialog;
} class Dialog : public QDialog
{
Q_OBJECT public:
explicit Dialog(QWidget *parent = );
~Dialog();
void initwebcam();
void initBackgroundSubtractorMOG2();
Mat frame ;
private slots:
void getFrame(); //实现定时从摄像头取图并显示在label上的功能。 private:
VideoCapture cap; //highgui 里提供的一个专门处理摄像头图像的结构体
cv::BackgroundSubtractorMOG2 bg;
VideoWriter writer; //摄像头每次抓取的图像为一帧,使用该指针指向一帧图像的内存空间
Size videoSize;
Ui::Dialog *ui;
QTimer *timer; /* 定时器,更新界面 */
}; #endif // DIALOG_H

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h" static bool recodeflag=false;
static cv::Mat frame;
static cv::Mat back;
static cv::Mat fore;
static Mat copyimg;
static std::vector<std::vector<cv::Point> > contours; static int count=; Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
timer = new QTimer(this);
initwebcam();
initBackgroundSubtractorMOG2();
connect(timer,SIGNAL(timeout()),this,SLOT(getFrame())); //超时就去取
timer->start(); //1000为1秒,10毫秒去取一帧 } Dialog::~Dialog()
{
timer->stop(); //停止取帧 delete ui;
} void Dialog::initwebcam()
{
cap=VideoCapture();
if(!cap.isOpened()){ qDebug()<<"init webcam error ,program exit...";
return;
}
cap.set(CV_CAP_PROP_FRAME_WIDTH, ); /* set width */
cap.set(CV_CAP_PROP_FRAME_HEIGHT, ); /* set height */ videoSize=Size(,); } void Dialog::initBackgroundSubtractorMOG2()
{
bg.setInt("nmixtures", );
bg.setBool("detectShadows", false); writer.open("result.avi",CV_FOURCC('D','I','V','X'),,videoSize);
} void Dialog::getFrame(){
//从摄像头取帧
// static QPixmap pixmapObject(":/image/webcam.png");
// ui->label_3->setPixmap(pixmapObject); cap>>frame;
frame.copyTo(copyimg);
QImage image = QImage((const uchar*)frame.data, frame.cols, frame.rows, QImage::Format_RGB888).rgbSwapped(); //简单地转换一下为Image对象,rgbSwapped是为了显示效果色彩好一些。
ui->label->setPixmap(QPixmap::fromImage(image));
bg.operator ()(frame,fore);
bg.getBackgroundImage(back);
cv::erode(fore,fore,cv::Mat());
cv::dilate(fore,fore,cv::Mat());
cv::findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
if (contours.size()> &&count>)
{
//如果轮廓的面积太小了也不用写到输出视频
for (int i = ; i< contours.size(); i++)
{
double a=contourArea( contours[i],false);
if (a>)//面积小于1000
{
cv::drawContours(frame,contours,i,cv::Scalar(,,),);
recodeflag=true;
}
else
{
recodeflag=false;
}
} }
else
{
recodeflag=false; }
if(recodeflag){
QImage imagelable2 = QImage((const uchar*)frame.data, frame.cols, frame.rows, QImage::Format_RGB888).rgbSwapped(); ui->label_2->setPixmap(QPixmap::fromImage(imagelable2));
}else {
//static QPixmap pixmapObject(":/image/webcam.png");
// ui->label_2->setPixmap(pixmapObject);
ui->label_2->setText("<h1><center><font color='red'>NOTHING</font></center></h1>"); } if (recodeflag)
{
//std::string outfile(cv::format("%d.jpg",imagecount));
//writer<<frame;
writer<<copyimg;
//imwrite(outfile,frame);
//imagecount++;
}
count++;
}

效果图

背景建模

异物入侵

生成异物入侵的视频文件

最新文章

  1. 使用DBI(perl)实现文本文件的导入导出mysql
  2. Prince2的七大原则(6)
  3. 监听器初始化Job、JobTracker相应TaskTracker心跳、调度器分配task源码级分析
  4. 使用Html5+C#+微信 开发移动端游戏详细教程 :(二)准备工作&amp;开发环境
  5. (13)odoo翻译
  6. hdu 5273 Dylans loves sequence
  7. 【英语】Bingo口语笔记(33) - 面部器官系列
  8. Linux下的Memcache安装
  9. sqlserver2008 解决 ldf文件过大的方法
  10. Nginx配置中运行与启动的详细介绍【转】
  11. angularjs改变路由时控制器每次都执行两次
  12. Windows Phone 8初学者开发—第22部分:用演示图板创建卷盘的动画
  13. 建立TCP连接的三次握手
  14. HBase源代码分析之HRegion上MemStore的flsuh流程(一)
  15. pytest 15 fixture之autouse=True
  16. MyElasticsearch
  17. 两个1/x类的广义函数
  18. 关于Java抽象类,接口与实现接口及派生类继承基类
  19. C语言程序设计I—寒假作业
  20. Web用户控件开发--星型评分控件

热门文章

  1. 苹果Mac操作系统下怎么显示隐藏文件
  2. [转载]jquery tmpl使用方法
  3. uva 10034
  4. python url编码
  5. linux jps 命令
  6. HeadFirst设计模式之单例模式
  7. vimrc for windows
  8. CodeForces250B——Restoring IPv6(字符串处理)
  9. Error Code: 1175
  10. 键盘KeyCode值列表