工程描述
- opencv2.4.8
- QT5
背景建模后,当有异物入侵时,把入侵的帧写到视频文件
使用BackgroundSubtractorMOG2背景建模
程序基于QT对话框
.pro
#------------------------------------------------- # # Project created by QtCreator 2014-06-19T16:00:40# #------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, 4): 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 = 0); ~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=0; 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(5); //1000为1秒,10毫秒去取一帧 } Dialog::~Dialog() { timer->stop(); //停止取帧 delete ui; } void Dialog::initwebcam() { cap=VideoCapture(0); if(!cap.isOpened()){ qDebug()<<"init webcam error ,program exit..."; return; } cap.set(CV_CAP_PROP_FRAME_WIDTH, 320); /* set width */ cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240); /* set height */ videoSize=Size(320,240); } void Dialog::initBackgroundSubtractorMOG2() { bg.setInt("nmixtures", 3); bg.setBool("detectShadows", false); writer.open("result.avi",CV_FOURCC(‘D‘,‘I‘,‘V‘,‘X‘),15,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()>0 &&count>0) { //如果轮廓的面积太小了也不用写到输出视频 for (int i = 0; i< contours.size(); i++) { double a=contourArea( contours[i],false); if (a>500)//面积小于1000 { cv::drawContours(frame,contours,i,cv::Scalar(0,0,255),2); 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++; }
效果图
背景建模
异物入侵
生成异物入侵的视频文件
基于QT的一个简易的安防
时间: 2024-10-10 06:59:17