CAVAPA-GUI  30.5.2014
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
opencvcapture.h
1 /****************************************************************************
2  * Copyright (c) 2014, Joel Kivelä, Erkki Koskenkorva, Oskari Leppäaho,
3  * Mika Lehtinen and Petri Partanen.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 ****************************************************************************/
30 #ifndef OPENCVCAPTURE_H
31 #define OPENCVCAPTURE_H
32 
33 #include <condition_variable>
34 #include <string>
35 #include <thread>
36 
37 #include <opencv2/opencv.hpp>
38 #include <opencv2/highgui/highgui.hpp>
39 
40 #include "captureinterface.h"
41 
42 namespace cavapa_gui
43 {
66 {
67 public:
68  OpenCVCapture() {}
69  virtual ~OpenCVCapture();
70 
75 
76  double getBarrelCorrection() override;
77 
78  FrameCapture getBuffered() override;
79 
80  std::string getDescription() const override;
81 
82  int getDeviceID() const override { return device_id; }
83 
84  double getFramerate() const override { return framerate; }
85 
86  int getMissedFrames() const override { return missed_frames; }
87 
88  FrameCapture getNext(bool skip, bool* error) override;
89 
90  std::string getPath() const override { return path; }
91 
102  double getProperty(int propId);
103 
104  cv::Size getResolution() const override { return resolution; }
105 
106  int getRetrievedFrames() const override { return retrieved_frames; }
107 
108  INTERFACE_TYPE getType() const override final
109  { return INTERFACE_TYPE::OPENCV; }
110 
117  bool hasNew() override;
118 
126  bool isOpen() const override { return opened; }
127 
133  bool open(int index);
134 
135  bool open(const std::string& path) override;
136 
148  void release();
149 
150  void resetStats() override { missed_frames = 0; retrieved_frames = 0; }
151 
152  void setBarrelCorrection(double amount) override;
153 
168  bool setProperty(int propId, double value);
169 
170  bool setResolution(const cv::Size &new_size) override;
171 
172 private:
180  static cv::Mat applyBarrel(const cv::Mat& image, double effect);
181 
188  void clearBuffer() { buffer = createEmptyFrame(resolution); }
189 
199  static cv::Mat convertTo3Channels(const cv::Mat& image);
200 
206  static FrameCapture createEmptyFrame(const cv::Size& frame_size);
207 
214  inline void getFrameSize();
215 
219  void init();
220 
225  inline void keepWorkerReady();
226 
230  void retrieve();
231 
238  inline bool retrieve_next(std::unique_lock<std::mutex>& guard);
239 
244  static inline FrameTime timeNow();
245 
246  // Variables that are only used by the main thread
247  int device_id = UNKNOWN_DEVICE; // Device initialization index
248  double framerate; // Device FPS
249  FrameTime last_timestamp = 0; // Timestamp of the last image get.
250  int missed_frames; // Recorded frame misses
251  bool opened = false; // If device was opened at init
252  std::string path = ""; // Device initialization path
253  cv::Size resolution; // Device resolution
254  int retrieved_frames; // Total frame count by getNext()
255 
256  // Variables that are protected by a mutex
257  FrameCapture buffer; // Frame buffer
258  double barrel_correction = 0.0; // Barrel effect applied
259  bool capture = false; // If retrieval is in progress
260  std::condition_variable capture_next; // Conditional for next retrieve
261  std::mutex lock; // Thread mutex
262  bool read_error = false; // Indicates retrieval error
263  std::condition_variable retrieved; // Conditional for retrieve
264  std::unique_ptr<std::thread> retriever = nullptr; // Retrieval thread
265  cv::VideoCapture source; // OpenCV source
266  bool terminate = false; // Terminate message for thread
267 };
268 } // namespace
269 
270 #endif // OPENCVCAPTURE_H
double getProperty(int propId)
Gets the OpenCV specific source properties.
Definition: opencvcapture.cpp:241
DISALLOW_COPY_AND_ASSIGN(OpenCVCapture)
Copy and assign of the class is not allowed.
std::string getPath() const override
Retrieves the path given during the initialization.
Definition: opencvcapture.h:90
Interface for the capture devices.
Definition: captureinterface.h:47
bool setProperty(int propId, double value)
Sets OpenCV specific source properties.
Definition: opencvcapture.cpp:480
std::string getDescription() const override
Retrieves the description of the device.
Definition: opencvcapture.cpp:180
bool hasNew() override
Checks whether the device has a new image to be retrieved or not.
Definition: opencvcapture.cpp:278
The OpenCV capturing class.
Definition: opencvcapture.h:65
int getMissedFrames() const override
Retrieves the logged frame misses.
Definition: opencvcapture.h:86
void release()
Forces the release of the worker thread.
Definition: opencvcapture.cpp:364
void setBarrelCorrection(double amount) override
Sets the amount of the barrel correction to be applied.
Definition: opencvcapture.cpp:468
void resetStats() override
Resets the statistical counters.
Definition: opencvcapture.h:150
bool setResolution(const cv::Size &new_size) override
Sets the capturing device resolution.
Definition: opencvcapture.cpp:506
bool isOpen() const override
Checks whether the source has been initialized or not.
Definition: opencvcapture.h:126
std::uint64_t FrameTime
Used to store milliseconds interval in frame times.
Definition: common.h:138
double getBarrelCorrection() override
Retrieves the barrel correction applied to the source.
Definition: opencvcapture.cpp:158
The structure is used for storing a single source frame.
Definition: common.h:241
double getFramerate() const override
Retrieves the capture device framerate.
Definition: opencvcapture.h:84
cv::Size getResolution() const override
Retrieves the capture device resolution.
Definition: opencvcapture.h:104
int getRetrievedFrames() const override
Retrieves the total logged frames.
Definition: opencvcapture.h:106
const int UNKNOWN_DEVICE
Used to indicate unknown hardware device.
Definition: common.h:224
FrameCapture getNext(bool skip, bool *error) override
Retrieves the next frame.
Definition: opencvcapture.cpp:208
int getDeviceID() const override
Returns the ID number the device was initialized with.
Definition: opencvcapture.h:82
INTERFACE_TYPE
Used to define the type of source interface.
Definition: captureinterface.h:140
bool open(int index)
Opens OpenCV VideoCapture for the hardware device.
Definition: opencvcapture.cpp:334
FrameCapture getBuffered() override
Retrieves the currently buffered frame.
Definition: opencvcapture.cpp:173
INTERFACE_TYPE getType() const overridefinal
Retrieves the type of the device.
Definition: opencvcapture.h:108