I want to use QLabel class to display every frame(an OpenCV Mat) from a camera,but update()
cannot display images continuously, so I use repain();
but here a problem, my ui cannot move and other button can not click, so if I want to display a video, how can I do; use opencv and qt and vs; Thanks in advance!
Asked
Active
Viewed 287 times
0

Sabir Al Fateh
- 1,743
- 3
- 20
- 27

Wck
- 1
1 Answers
1
Just move your video capturing cycle into another thread and send frames to gui thread with signal/slot system.
Thread
class VideoThread : public QThread
{
Q_OBJECT
public:
VideoThread(QObject *parent = nullptr);
protected:
void run();
signals:
void frameCaptured(cv::Mat frame);
};
void VideoThread::run()
{
VideoCapture cap(0);
if(!cap.isOpened()){
qDebug() << "Cant capture video";
return ;
}
while(1){
Mat frame;
cap >> frame;
if (frame.empty()) {
qDebug() << "Empty frame";
break;
}
emit frameCaptured(frame);
}
}
Using
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(&m_videoThread, &VideoThread::frameCaptured,
this, &MainWindow::OnFrameCaptured);
}
void MainWindow::OnFrameCaptured(const cv::Mat &frame)
{
QImage imgIn= QImage((uchar*) frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
ui->lblVideo->setPixmap(QPixmap::fromImage(imgIn));
}
Don't forget register metatype
qRegisterMetaType<cv::Mat>("cv::Mat");
and run thread
m_videoThread.start();

Serhiy Kulish
- 1,057
- 1
- 6
- 8
-
thanks again,it works !! – Wck Nov 06 '18 at 12:04
-
still can not move the mainwindow, but when i put cv:: imshow(“frame”,frame)in while(1){ Mat frame; cap >> frame; if (frame.empty()) { qDebug() << "Empty frame"; break; } emit frameCaptured(frame); imshow(“frame”,frame) } it works ,and the buttons on Mainwindow can be clicked...oh...that's strange – Wck Nov 06 '18 at 12:51
-
I've solved the problem by put all image processing in camera thread,and send a Qpixmap to mainwindow – Wck Nov 07 '18 at 03:10