上个文章就说了,我正在实现一个智能监控系统。 由此实现了Qt的FTP文件传输:http://blog.csdn.net/u013812682/article/details/52199502。 还有Qt的tcp视频传输:http://blog.csdn.net/u013812682/article/details/52185540。 在此我将实现基于Qt和opencv的基于多摄像头的视频显示。 下一步将是整个系统的智能化操作,可能包括人脸检测,人脸识别,单摄像头人物追踪(也可能是多摄像头)以及还没想到的其他功能。 最后将视频或图片上传云服务器 PS:只有一个usb摄像头加上笔记本摄像头测试的,没有悬挂的摄像头有点心累呀!!!
整个显示框架包括了两个类,一个是自定义的按钮类,一个是主界面显示的类; 开始也纠结了好久,多线程这样的,发现没有其实也是可以实现的。 有图有真相: 开始只有一个按钮: 点击一次就很生成一个新按钮,并且加载一次摄像头, 有多少摄像头就可以点击几次,我设置了最大值是九个,方便查看(毕竟屏幕大小有限) 上代码了: 自定义的按钮:
#ifndef MYBUTTON_H
#define MYBUTTON_H
#include<QPushButton>
#include<QPen>
#include<QPainter>
#include<QMouseEvent>
#include<QLabel>
class MyButton : public QPushButton
{
Q_OBJECT
public:
explicit MyButton(QWidget* parent=
0);
~MyButton();
void paintEvent(QPaintEvent*);
private:
bool israised;
};
#endif // MYBUTTON_H
#include "mybutton.h"
MyButton::MyButton(QWidget *parent) :
QPushButton(parent)
{
setAutoFillBackground(
true);
}
MyButton::~MyButton()
{
}
void MyButton::paintEvent(QPaintEvent *)
{
int w=width();
int h=height();
QColor clrw(
255,
255,
255);
QColor clrb(
0,
0,
0);
QBrush brw(clrw);
QBrush brb(clrb);
QPen penw(brb,
2);
QPen penb(brb,
2);
QPoint pttop(
0,
0);
QPoint ptbottom(
0,h);
QPoint ptleft(w,
0);
QPoint ptright(w,h);
QPainter g(
this);
if(israised)
{
g.setPen(penw);
}
else
{
g.setPen(penb);
}
g.drawLine(pttop,ptleft);
g.drawLine(ptleft,ptright);
if(israised)
{
g.setPen(penb);
}
else
{
g.setPen(penw);
}
g.drawLine(ptbottom,pttop);
g.drawLine(ptbottom,ptright);
}
这个其实很简单,实现自定义按钮的意义在于可以实现点击效果还有就是自带的QPushbutton是不可以加图片背景的(最起码我不知道怎么加,要是有人知道,希望不吝赐教),还有一个是按钮的凹凸感因为重写的原因也是没有了,在此我也没有想到好的方法,要是哪位同学知道的,也可以告诉我 :)
这个是界面类
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include<mybutton.h>
#include<QButtonGroup>
#include<QGridLayout>
#include<QDebug>
#include<opencv2/opencv.hpp>
#include<QTimer>
#include<QImage>
using namespace std;
using namespace cv;
class MainWindow :
public QWidget
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent =
0);
~MainWindow();
int num;
private slots:
void OnClicked();
void test();
private:
VideoCapture cap[
9];
MyButton* button;
QPushButton* add_ptn;
bool add_widget;
QButtonGroup* bgp;
QTimer* timer;
QGridLayout* layout;
};
#endif
int max=
9;
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
num=
0;
add_widget=
false;
bgp=
new QButtonGroup;
timer=
new QTimer(
this);
button=
new MyButton;
bgp->addButton(button,
0);
bgp->button
(0)->setMinimumSize(
200,
200);
bgp->button
(0)->setMaximumSize(
200,
200);
layout=
new QGridLayout;
layout->addWidget(bgp->button(
0),
0,
0);
connect(bgp->button(
0),SIGNAL(pressed()),
this,SLOT(OnClicked()));
connect(timer,SIGNAL(timeout()),
this,SLOT(test()));
resize(
800,
800);
setLayout(layout);
}
MainWindow::~MainWindow()
{
}
void MainWindow::OnClicked()
{
timer->start(
10);
num++;
for(int i=
0;i<num;i++){
cap[i].open(i);
if(!cap[i].isOpened()){
qDebug()<<
"cap["<<i<<
"] not open!";
// timer->stop();
}
}
add_widget=
true;
if(add_widget){
bgp->addButton(
new MyButton(
this),num);
}
bgp->button
(num)->setMinimumSize(
200,
200);
bgp->button
(num)->setMaximumSize(
200,
200);
add_widget=
false;
for(int i=
1;i<num+
1;i++){
layout->addWidget(bgp->button(i),i/
3,i%
3);
}
bgp->button
(num-1)->setEnabled(
false);
connect(bgp->button(num),SIGNAL(pressed()),
this,SLOT(OnClicked()));
}
void MainWindow::test(){
for(int i=
0;i<num;i++){
Mat frame;
cap[i]>>frame;
if(frame.empty()){
bgp->button
(i)->setToolTip(
"摄像头没有连接!");
bgp->button
(i)->setText(
"摄像头没有连接!");
break;
}
cvtColor(frame,frame,CV_BGR2RGB);
QImage image(frame.data,frame.cols,frame.rows,
QImage::Format_RGB888);
char file[
10];
sprintf_s(file,
"%d%s",i,
".jpg");
image.save(file,
"jpg");
QPixmap dst(file);
QPixmap pix=dst.scaled(QSize(
200,
200),
Qt::IgnoreAspectRatio);
QPalette p;
p.setBrush(
QPalette::Button,QBrush(pix));
bgp->button
(i)->setPalette(p);
}
}
至此,多摄像头的显示就完成了,接下来要开始下一步的工作,大家加油。
转载请注明原文地址: https://ju.6miu.com/read-1308173.html