'Can't display OpenCV VideoCapture in Qt
I want to display the content of my VideoCapture in a QGraphicsView, however if I run the Program my WebCam activates, but nothing is displayed onto my GraphicsView. This is what happen if I run this on my laptop, however if I run this on my PC, it works fine.
Do you know what's the issue here?
Codes:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFile>
#include <QString>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QPixmap>
#include <QTimer>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void Update();
private:
Ui::MainWindow *ui;
cv::VideoCapture videoCap;
cv::Mat liveImage;
QImage image;
QGraphicsScene* scene = new QGraphicsScene(this);
};
#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);
videoCap.open(0);
QTimer* timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(Update()));
timer->start(20);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::Update()
{
videoCap.read(liveImage);
image = QImage(liveImage.data, liveImage.cols, liveImage.rows, QImage::Format_RGB888).rgbSwapped();
scene->addPixmap(QPixmap::fromImage(image));
ui->graphicsView->setScene(scene);
ui->graphicsView->show();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
