C++ 如何使用openCV启动和停止usb摄像头流媒体,方法是单击按钮并将其绘制到Qt上的自定义小部件中?
我对qt很陌生。我正在尝试在我的ui上创建一个选项卡,该选项卡可以提供计算连接到计算机的usb摄像头数量的选项,然后使用OPENCV列出。之后,我想选择其中一个,然后开始使用paintEvent将视频流传输到自定义小部件中。我的主要困难是:按下按钮时如何启动和停止流媒体。下面是我的代码 主窗口C++ 如何使用openCV启动和停止usb摄像头流媒体,方法是单击按钮并将其绘制到Qt上的自定义小部件中?,c++,qt,opencv,qt5,C++,Qt,Opencv,Qt5,我对qt很陌生。我正在尝试在我的ui上创建一个选项卡,该选项卡可以提供计算连接到计算机的usb摄像头数量的选项,然后使用OPENCV列出。之后,我想选择其中一个,然后开始使用paintEvent将视频流传输到自定义小部件中。我的主要困难是:按下按钮时如何启动和停止流媒体。下面是我的代码 主窗口 //includes... namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT
//includes...
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void on_CheckCamerasButton_clicked();
void on_StartStreamingButton_clicked();
private:
Ui::MainWindow *ui;
cameraimage camera;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow(){
delete ui;
}
void MainWindow::on_StartStreamingButton_clicked(){
camera.startStreaming();
}
void MainWindow::on_CheckCamerasButton_clicked(){
camera.stopStreaming();
}
cameraimage.h
//includes...
class cameraimage : public QWidget
{
Q_OBJECT
public:
explicit cameraimage(QWidget *parent = nullptr);
private:
QPoint mPoint;
QTimer *timer;
cv::VideoCapture captureVideo;
public slots:
void paintEvent(QPaintEvent * event);
void startStreaming();
void stopStreaming();
};
#endif // CAMERAIMAGE_H
cameraimage.cpp
#include "cameraimage.h"
cameraimage::cameraimage(QWidget *parent) : QWidget(parent)
{
setMouseTracking(true);
}
void cameraimage::startStreaming(){
qDebug() << "Starting Streaming";
captureVideo.open(-1);
if (captureVideo.isOpened() == false){
qDebug() << "Camera can't open";
return;
}
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1);
}
void cameraimage::stopStreaming(){
captureVideo.release();
timer->stop();
}
void cameraimage::paintEvent(QPaintEvent *){
cv::Mat tmpImage;
cv::Mat image;
captureVideo.read(tmpImage);
if (tmpImage.empty() == true){
qDebug() << "EMPTY!";
return;
}
cv::cvtColor(tmpImage, image, CV_BGR2RGB);
QImage img((const unsigned char*)(image.data), image.cols, image.rows, QImage::Format_RGB888);
QPixmap pixmap = QPixmap::fromImage(img);
QPainter painter(this);
float comprimento = 1.0*width()/pixmap.width();
float altura = 1.0*height()/pixmap.height();
float ratio = 0.;
if (comprimento<=altura)
ratio = comprimento;
else
ratio = altura;
QSize size = ratio*pixmap.size();
size.setHeight(size.height()-10);
QPoint p;
p.setX(0 + (width()-size.width())/2);
p.setY(5);
painter.drawPixmap(QRect(p, size), pixmap.scaled(size, Qt::KeepAspectRatio));
}
有人能帮我吗
致以最诚挚的问候:D我已经测试了您的代码,它运行良好,除非您的电脑性能不佳。你的操作系统是什么?这是我的测试:几年前,我在一个项目中处理图像,所以时间是一种稀缺资源,所以使用线程,也许这是你的选择。:Hello@eyllansc。我使用的是Ubuntu 16.04。仔细看你的剧本,我发现了我的错误。所以,我只是将“camera.startStreaming()”更改为“ui->cameraImageWidget->startStreaming()”。现在它在运行。再次感谢您的帮助/计划/建议。问候:)当然@eyllanesc!比帮助更多人更容易。再次感谢您。我已经测试了您的代码,它运行良好,除非您的电脑性能不佳。你的操作系统是什么?这是我的测试:几年前,我在一个项目中处理图像,所以时间是一种稀缺资源,所以使用线程,也许这是你的选择。:Hello@eyllansc。我使用的是Ubuntu 16.04。仔细看你的剧本,我发现了我的错误。所以,我只是将“camera.startStreaming()”更改为“ui->cameraImageWidget->startStreaming()”。现在它在运行。再次感谢您的帮助/计划/建议。问候:)当然@eyllanesc!比帮助更多人更容易。再次感谢你。
Starting Streaming...
EMPTY!
EMPTY!
EMPTY!
...