1 #include "cvvideocapture.h" 2 #include "opencv2/imgproc.hpp" 3 #include "opencv2/core.hpp" 4 CVVideoCapture::CVVideoCapture(QWidget *parent) :
6 m_container{
new QGridLayout(
this)},
7 m_frameLabel{
new QLabel(
this)},
9 m_infoLabel{
new QLabel(
"0",
this)},
10 m_startButton{
new QPushButton(
"Capture",
this)},
11 m_stopButton{
new QPushButton(
"Stop",
this)},
12 m_stateLabel{
new QLabel(
"idle",
this)},
13 m_deviceSpinBox{
new QSpinBox(
this)}
17 m_timer.setInterval(30);
19 m_container->addWidget(m_frameLabel, 0, 0);
20 QHBoxLayout *lout =
new QHBoxLayout(
this);
21 lout->addWidget(m_startButton);
22 lout->addWidget(m_stopButton);
24 m_container->addLayout(lout, 2, 0);
25 QHBoxLayout *lblsbox =
new QHBoxLayout(
this);
26 lblsbox->addWidget(m_infoLabel);
27 lblsbox->addWidget(m_stateLabel);
28 lblsbox->addWidget(m_deviceSpinBox);
29 m_container->addLayout(lblsbox, 1, 0);
34 this->setLayout(m_container);
39 CVVideoCapture::~CVVideoCapture()
46 int CVVideoCapture::device()
const 51 void CVVideoCapture::setDevice(
int device)
56 cv::Mat CVVideoCapture::currentFrame()
const 58 return m_currentFrame;
61 void CVVideoCapture::setCurrentFrame(
const cv::Mat ¤tFrame)
63 m_currentFrame = currentFrame;
66 void CVVideoCapture::createConnections()
69 connect(&(this->m_timer), &QTimer::timeout,
this, &CVVideoCapture::captureFrame);
70 connect(
this, &CVVideoCapture::stopped, &(this->m_timer), &QTimer::stop);
71 connect(
this, SIGNAL(started()), &(this->m_timer), SLOT(start()));
72 connect(
this, &CVVideoCapture::deviceChanged, [&](
int dev){
73 m_infoLabel->setText(QString(
"Current Capture Device %1").arg(dev));
75 connect(this->m_stopButton, SIGNAL(clicked()),
this, SLOT(stop()));
76 connect(this->m_startButton, SIGNAL(clicked()),
this, SLOT(start()));
77 connect(this->m_deviceSpinBox, SIGNAL(valueChanged(
int)),
this, SLOT(changeDevice(
int)));
80 QImage CVVideoCapture::toImage(
const cv::Mat &m)
83 if(m.type() == CV_8UC3){
84 cv::cvtColor(m, temp, cv::COLOR_BGR2RGB);
85 return QImage(m.data, m.cols, m.rows, m.step, QImage::Format_RGB888).rgbSwapped();
87 return QImage(m.data, m.cols, m.rows, m.step, QImage::Format_Grayscale8);
91 void CVVideoCapture::captureFrame()
93 if(m_capture.isOpened())
95 m_capture >> m_currentFrame;
96 emit frameCaptured(m_currentFrame);
98 m_frameLabel->setPixmap(QPixmap::fromImage(toImage(m_currentFrame)));
102 void CVVideoCapture::stop()
108 void CVVideoCapture::start()
110 m_capture.open(m_device);
114 void CVVideoCapture::changeDevice(
int device)
116 if(device == m_device)
120 emit deviceChanged(device);