libkovan  1
The kovan standard library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
81  const Point2<unsigned> &centroid() const;
82  const Rect<unsigned> &boundingBox() const;
83  const double confidence() const;
84  const char *data() const;
85  const size_t dataLength() const;
86 
87  private:
88  Point2<unsigned> m_centroid;
89  Rect<unsigned> m_boundingBox;
90  double m_confidence;
91  char *m_data;
92  size_t m_dataLength;
93  };
94 
95  typedef std::vector<Object> ObjectVector;
96 
98  {
99  public:
100  ChannelImpl();
101  virtual ~ChannelImpl();
102 
103  void setImage(const cv::Mat &image);
104  ObjectVector objects(const Config &config);
105 
106  protected:
107  virtual void update(const cv::Mat &image) = 0;
108  virtual ObjectVector findObjects(const Config &config) = 0;
109 
110  private:
111  bool m_dirty;
112  cv::Mat m_image;
113  };
114 
116  {
117  public:
118  virtual ~ChannelImplManager();
119  virtual void setImage(const cv::Mat &image) = 0;
120  virtual ChannelImpl *channelImpl(const std::string &name) = 0;
121  };
122 
124  {
125  public:
128 
129  virtual void setImage(const cv::Mat &image);
130  virtual ChannelImpl *channelImpl(const std::string &name);
131 
132  private:
133  std::map<std::string, ChannelImpl *> m_channelImpls;
134  };
135 
137  {
138  public:
139  Channel(Device *device, const Config &config);
140  ~Channel();
141 
142  void invalidate();
143 
144  const ObjectVector *objects() const;
145 
146  Device *device() const;
147 
151  void setConfig(const Config &config);
152 
153  private:
154  Device *m_device;
155  Config m_config;
156  mutable ObjectVector m_objects;
157  ChannelImpl *m_impl;
158  mutable bool m_valid;
159  };
160 
161  typedef std::vector<Channel *> ChannelPtrVector;
162 
164  {
165  public:
166  static std::string extension();
167 
168  static void setBasePath(const std::string &path);
169  static std::string path(const std::string &name = std::string());
170  static std::string defaultPath();
171  static std::string defaultConfigPath();
172  static void setDefaultConfigPath(const std::string &name);
173 
174  private:
175  static std::string s_path;
176  };
177 
179  {
180  public:
181  virtual ~InputProvider();
182  virtual bool open(const int number) = 0;
183  virtual bool isOpen() const = 0;
184  virtual void setWidth(const unsigned width) = 0;
185  virtual void setHeight(const unsigned height) = 0;
186  virtual bool next(cv::Mat &image) = 0;
187  virtual bool close() = 0;
188  };
189 
191  {
192  public:
194  ~UsbInputProvider();
195 
196  virtual bool open(const int number);
197  virtual bool isOpen() const;
198  virtual void setWidth(const unsigned width);
199  virtual void setHeight(const unsigned height);
200  virtual bool next(cv::Mat &image);
201  virtual bool close();
202 
203  private:
204  cv::VideoCapture *m_capture;
205  };
206 
208  {
209  public:
210  Device(InputProvider *const inputProvider);
211  ~Device();
212 
213  bool open(const int number = 0);
214  bool isOpen() const;
215  bool close();
216  bool update();
217 
218  void setWidth(const unsigned width);
219  void setHeight(const unsigned height);
220 
221  unsigned width() const;
222  unsigned height() const;
223 
224  const ChannelPtrVector &channels() const;
225 
226  InputProvider *inputProvider() const;
227  const cv::Mat &rawImage() const;
228 
229  void setConfig(const Config &config);
230  const Config &config() const;
231 
232  void setChannelImplManager(ChannelImplManager *channelImplManager);
233  ChannelImplManager *channelImplManager() const;
234 
235  const unsigned char *bgr() const;
236 
237  private:
238  void updateConfig();
239 
240  InputProvider *const m_inputProvider;
241  Config m_config;
242  ChannelPtrVector m_channels;
243  ChannelImplManager *m_channelImplManager;
244  cv::Mat m_image;
245  timeval m_lastUpdate;
246 
247  mutable unsigned char *m_bgr;
248  mutable unsigned m_bgrSize;
249  };
250 
255 }
256 
257 
258 
259 #endif
Camera::Device * cDevice()
Definition: camera.hpp:207
Definition: camera.hpp:190
std::vector< Object > ObjectVector
Definition: camera.hpp:95
Definition: config.hpp:9
Definition: camera.hpp:163
std::vector< Channel * > ChannelPtrVector
Definition: camera.hpp:161
Definition: camera.hpp:136
Definition: camera.hpp:178
#define EXPORT_SYM
Definition: export.h:7
Definition: camera.hpp:97
Definition: camera.hpp:115
Definition: camera.hpp:71
Definition: camera.hpp:123