Object Detection  5.0
Image Processing Using Qt and Opencv
cvvideocapture.cpp
1 #include "cvvideocapture.h"
2 #include "opencv2/imgproc.hpp"
3 #include "opencv2/core.hpp"
4 CVVideoCapture::CVVideoCapture(QWidget *parent) :
5  QWidget(parent),
6  m_container{new QGridLayout(this)},
7  m_frameLabel{new QLabel(this)},
8  m_device{0},
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)}
14 
15 {
16 
17  m_timer.setInterval(30);
18  createConnections();
19  m_container->addWidget(m_frameLabel, 0, 0);
20  QHBoxLayout *lout = new QHBoxLayout(this);
21  lout->addWidget(m_startButton);
22  lout->addWidget(m_stopButton);
23 
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);
30 // m_container->addWidget(m_deviceSpinBox, 1, 0);
31 // m_container->addWidget(m_startButton, 2, 0);
32 // m_container->addWidget(m_stopButton, 2, 0);
33 // m_container->add
34  this->setLayout(m_container);
35 // this->show();
36 
37 }
38 
39 CVVideoCapture::~CVVideoCapture()
40 {
41  m_capture.release();
42  delete m_container;
43 
44 }
45 
46 int CVVideoCapture::device() const
47 {
48  return m_device;
49 }
50 
51 void CVVideoCapture::setDevice(int device)
52 {
53  m_device = device;
54 }
55 
56 cv::Mat CVVideoCapture::currentFrame() const
57 {
58  return m_currentFrame;
59 }
60 
61 void CVVideoCapture::setCurrentFrame(const cv::Mat &currentFrame)
62 {
63  m_currentFrame = currentFrame;
64 }
65 
66 void CVVideoCapture::createConnections()
67 {
68 
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));
74  });
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)));
78 
79 }
80 QImage CVVideoCapture::toImage(const cv::Mat &m)
81 {
82  cv::Mat temp;
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();
86  }else {
87  return QImage(m.data, m.cols, m.rows, m.step, QImage::Format_Grayscale8);
88 
89  }
90 }
91 void CVVideoCapture::captureFrame()
92 {
93  if(m_capture.isOpened())
94  {
95  m_capture >> m_currentFrame;
96  emit frameCaptured(m_currentFrame);
97  }
98  m_frameLabel->setPixmap(QPixmap::fromImage(toImage(m_currentFrame)));
99 
100 }
101 
102 void CVVideoCapture::stop()
103 {
104  m_capture.release();
105  emit stopped();
106 }
107 
108 void CVVideoCapture::start()
109 {
110  m_capture.open(m_device);
111  emit started();
112 }
113 
114 void CVVideoCapture::changeDevice(int device)
115 {
116  if(device == m_device)
117  return;
118  m_device = device;
119  m_capture.release();
120  emit deviceChanged(device);
121 }