libkovan  1
The kovan standard library
camera.hpp
Go to the documentation of this file.
1 /**************************************************************************
2  * Copyright 2012 KISS Institute for Practical Robotics *
3  * *
4  * This file is part of libkovan. *
5  * *
6  * libkovan is free software: you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License as published by *
8  * the Free Software Foundation, either version 2 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * libkovan is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with libkovan. Check the LICENSE file in the project root. *
18  * If not, see <http://www.gnu.org/licenses/>. *
19  **************************************************************************/
20 
21 #ifndef _CAMERA_HPP_
22 #define _CAMERA_HPP_
23 
32 #include "geom.hpp"
33 #include "color.hpp"
34 #include "config.hpp"
35 #include "export.h"
36 #include <cstring>
37 #include <string>
38 #include <vector>
39 #include <map>
40 #include <iostream>
41 
42 #ifndef WIN32
43 #include <sys/time.h>
44 #else
45 #define NOMINMAX
46 #include <time.h>
47 #include <winsock2.h>
48 #endif
49 
50 #include <opencv2/core/core.hpp>
51 
52 // These keys are used in the config files loaded by
53 // Camera::Device
54 #define CAMERA_GROUP ("camera")
55 #define CAMERA_NUM_CHANNELS_KEY ("num_channels")
56 #define CAMERA_CHANNEL_GROUP_PREFIX ("channel_")
57 #define CAMERA_CHANNEL_TYPE_KEY ("type")
58 
59 #define CAMERA_CHANNEL_TYPE_HSV_KEY ("hsv")
60 #define CAMERA_CHANNEL_TYPE_QR_KEY ("qr")
61 
62 namespace cv
63 {
64  class VideoCapture;
65 }
66 
67 namespace Camera
68 {
69  class Device;
70 
72  {
73  public:
74  Object(const Point2<unsigned> &centroid,
75  const Rect<unsigned> &boundingBox,
76  const double &confidence, const char *data = 0,
77  const size_t &dataLength = 0);
78  Object(const Object &rhs);
79  ~Object();
80 #ifndef SWIG
81  const Point2<unsigned> &centroid() const;
82  const Rect<unsigned> &boundingBox() const;
83 #endif
84 
85  const double confidence() const;
86  const char *data() const;
87  const size_t dataLength() const;
88 
89  private:
90  Point2<unsigned> m_centroid;
91  Rect<unsigned> m_boundingBox;
92  double m_confidence;
93  char *m_data;
94  size_t m_dataLength;
95  };
96 
97  typedef std::vector<Object> ObjectVector;
98 
100  {
101  public:
102  ChannelImpl();
103  virtual ~ChannelImpl();
104 
105  void setImage(const cv::Mat &image);
106  ObjectVector objects(const Config &config);
107 
108  protected:
109  virtual void update(const cv::Mat &image) = 0;
110  virtual ObjectVector findObjects(const Config &config) = 0;
111 
112  private:
113  bool m_dirty;
114  cv::Mat m_image;
115  };
116 
118  {
119  public:
120  virtual ~ChannelImplManager();
121  virtual void setImage(const cv::Mat &image) = 0;
122  virtual ChannelImpl *channelImpl(const std::string &name) = 0;
123  };
124 
126  {
127  public:
130 
131  virtual void setImage(const cv::Mat &image);
132  virtual ChannelImpl *channelImpl(const std::string &name);
133 
134  private:
135  std::map<std::string, ChannelImpl *> m_channelImpls;
136  };
137 
139  {
140  public:
141  Channel(Device *device, const Config &config);
142  ~Channel();
143 
144  void invalidate();
145 
146 #ifndef SWIG
147  const ObjectVector *objects() const;
148 #endif
149 
150  Device *device() const;
151 
155  void setConfig(const Config &config);
156 
157  private:
158  Device *m_device;
159  Config m_config;
160  mutable ObjectVector m_objects;
161  ChannelImpl *m_impl;
162  mutable bool m_valid;
163  };
164 
165  typedef std::vector<Channel *> ChannelPtrVector;
166 
168  {
169  public:
170  static std::string extension();
171 
172  static void setBasePath(const std::string &path);
173  static std::string path(const std::string &name = std::string());
174  static std::string defaultPath();
175  static std::string defaultConfigPath();
176  static void setDefaultConfigPath(const std::string &name);
177 
178  private:
179  static std::string s_path;
180  };
181 
183  {
184  public:
185  virtual ~InputProvider();
186  virtual bool open(const int number) = 0;
187  virtual bool isOpen() const = 0;
188  virtual void setWidth(const unsigned width) = 0;
189  virtual void setHeight(const unsigned height) = 0;
190  virtual bool next(cv::Mat &image) = 0;
191  virtual bool close() = 0;
192  };
193 
195  {
196  public:
198  ~UsbInputProvider();
199 
200  virtual bool open(const int number);
201  virtual bool isOpen() const;
202  virtual void setWidth(const unsigned width);
203  virtual void setHeight(const unsigned height);
204  virtual bool next(cv::Mat &image);
205  virtual bool close();
206 
207  private:
208  cv::VideoCapture *m_capture;
209  };
210 
212  {
213  public:
214  Device(InputProvider *const inputProvider);
215  ~Device();
216 
217  bool open(const int number = 0);
218  bool isOpen() const;
219  bool close();
220  bool update();
221 
222  void setWidth(const unsigned width);
223  void setHeight(const unsigned height);
224 
225  unsigned width() const;
226  unsigned height() const;
227 #ifndef SWIG
228  const ChannelPtrVector &channels() const;
229 #endif
230 
231  InputProvider *inputProvider() const;
232  const cv::Mat &rawImage() const;
233 
234  void setConfig(const Config &config);
235  const Config &config() const;
236 
237  void setChannelImplManager(ChannelImplManager *channelImplManager);
238  ChannelImplManager *channelImplManager() const;
239 
240  const unsigned char *bgr() const;
241 
242  private:
243  void updateConfig();
244 
245  InputProvider *const m_inputProvider;
246  Config m_config;
247  ChannelPtrVector m_channels;
248  ChannelImplManager *m_channelImplManager;
249  cv::Mat m_image;
250  timeval m_lastUpdate;
251 
252  mutable unsigned char *m_bgr;
253  mutable unsigned m_bgrSize;
254  };
255 
260 }
261 
262 
263 
264 #endif
Camera::Device * cDevice()
Definition: camera.hpp:211
Definition: camera.hpp:194
Definition: camera.hpp:62
std::vector< Object > ObjectVector
Definition: camera.hpp:97
Definition: config.hpp:9
Definition: ardrone.hpp:144
Definition: camera.hpp:167
std::vector< Channel * > ChannelPtrVector
Definition: camera.hpp:165
Definition: camera.hpp:138
Definition: camera.hpp:182
#define EXPORT_SYM
Definition: export.h:7
Definition: camera.hpp:99
Definition: camera.hpp:117
Definition: camera.hpp:71
Definition: camera.hpp:125