OpenCVWidget
 All Classes Files Functions Variables
opencvcapture.cpp
Go to the documentation of this file.
1 #include "opencvcapture.h"
2 
3 using namespace cv;
4 
6 {
7  timer = new QTimer();
8  frameRate = -1;
9 }
10 
12 {
13 }
14 
15 
17 {
18  if(captureSource.isNull() || captureSource.isEmpty()) {
19  qDebug() << "No capture source specified.";
20  return;
21  }
22 
23  bool intConvert;
24  captureSource.toInt(&intConvert);
25  if(intConvert) {
26  int source = captureSource.toInt();
27  capture.open(source);
28  } else {
29  QByteArray ba = captureSource.toLocal8Bit();
30  const char *source = ba.data();
31  capture.open(source);
32  }
33 
34  if(!capture.isOpened()) {
35  qDebug() << "Error initializing capture.";
36  return;
37  }
38 
39  //Get an initial frame
40  capture >> image;
41 
42  //Connect the timer signal with the capture action
43  connect(timer, SIGNAL(timeout()), this, SLOT(captureFrame()));
44 
45  // Start the timer scheduled for firing according to the frame rate
46  // If the frame rate is 0, the timer will not be started.
47  if(frameRate < 0) {
48  // Attempt to set the frame rate automatically.
49  timer->start(floor(1.0f/(float)capture.get(CV_CAP_PROP_FPS)*1000.0f));
50  } else if(frameRate > 0) {
51  timer->start(floor(1.0f/(float)frameRate*1000.0f));
52  }
53 
54  emit frameCaptured(image);
55 }
56 
57 void OpenCVCapture::startCapture(QString source, int frameRate)
58 {
59  setSource(source);
60  setFrameRate(frameRate);
61  startCapture();
62 }
63 
64 void OpenCVCapture::setSource(QString source)
65 {
66  captureSource = source;
67 }
68 
70 {
71  return captureSource;
72 }
73 
75 {
76  frameRate = rate;
77  if(rate == 0) {
78  pauseCapture();
79  }
80 }
81 
83 {
84  return frameRate;
85 }
86 
88 {
89  timer->stop();
90  capture.release();
91 }
92 
94 {
95  timer->stop();
96 }
97 
99 {
100  //Start the timer scheduled for firing according to the frame rate
101  if(frameRate < 0) {
102  // Attempt to set the frame rate automatically.
103  timer->start(floor(1.0f/(float)capture.get(CV_CAP_PROP_FPS)*1000.0f));
104  } else {
105  timer->start(floor(1.0f/(float)frameRate*1000.0f));
106  }
107 }
108 
110 {
111  if(frameRate == 0 && capture.isOpened()) {
112  captureFrame();
113  }
114 }
115 
117 {
118  if(frameRate == 0 && capture.isOpened()) {
119  capture.grab();
120  }
121 }
122 
123 void OpenCVCapture::loadImage(int loadFlag)
124 {
125  stopCapture();
126 
127  QByteArray ba = captureSource.toLocal8Bit();
128  const char *source = ba.data();
129  image = cv::imread(source, loadFlag);
130 
131  emit frameCaptured(image);
132 }
133 
134 void OpenCVCapture::loadImage(QString source, int loadFlag)
135 {
136  setSource(source);
137  loadImage(loadFlag);
138 }
139 
141 {
142  return image;
143 }
144 
146 {
147  return this->capture.get(propId);
148 }
149 
150 bool OpenCVCapture::setCaptureProperty(int propId, double value)
151 {
152  return this->capture.set(propId, value);
153 }
154 
156 {
157  return capture.isOpened();
158 }
159 
161 {
162  //Get an image from the webcam
163  capture >> image;
164  if(image.empty()) {
165  stopCapture();
166  return;
167  }
168 
169  emit frameCaptured(image);
170 }