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 
24 #include "geom.hpp"
25 #include "color.hpp"
26 #include "config.hpp"
27 #include "export.h"
28 #include <cstring>
29 #include <string>
30 #include <vector>
31 #include <map>
32 #include <iostream>
33 #include <sys/time.h>
34 
35 #include <opencv2/core/core.hpp>
36 
37 // These keys are used in the config files loaded by
38 // Camera::Device
39 #define CAMERA_GROUP ("camera")
40 #define CAMERA_NUM_CHANNELS_KEY ("num_channels")
41 #define CAMERA_CHANNEL_GROUP_PREFIX ("channel_")
42 #define CAMERA_CHANNEL_TYPE_KEY ("type")
43 
44 #define CAMERA_CHANNEL_TYPE_HSV_KEY ("hsv")
45 #define CAMERA_CHANNEL_TYPE_QR_KEY ("qr")
46 
47 namespace cv
48 {
49  class VideoCapture;
50 }
51 
52 namespace Camera
53 {
54  class Device;
55 
57  {
58  public:
59  Object(const Point2<unsigned> &centroid,
60  const Rectangle<unsigned> &boundingBox,
61  const double confidence, const char *const data = 0,
62  const size_t dataLength = 0);
63  Object(const Object &rhs);
64  ~Object();
65 
66  const Point2<unsigned> &centroid() const;
67  const Rectangle<unsigned> &boundingBox() const;
68  const double confidence() const;
69  const char *data() const;
70  const size_t dataLength() const;
71 
72  private:
73  Point2<unsigned> m_centroid;
74  Rectangle<unsigned> m_boundingBox;
75  double m_confidence;
76  char *m_data;
77  size_t m_dataLength;
78  };
79 
80  typedef std::vector<Object> ObjectVector;
81 
83  {
84  public:
85  ChannelImpl();
86  virtual ~ChannelImpl();
87 
88  void setImage(const cv::Mat &image);
89  ObjectVector objects(const Config &config);
90 
91  protected:
92  virtual void update(const cv::Mat &image) = 0;
93  virtual ObjectVector findObjects(const Config &config) = 0;
94 
95  private:
96  bool m_dirty;
97  cv::Mat m_image;
98  };
99 
101  {
102  public:
103  virtual ~ChannelImplManager();
104  virtual void setImage(const cv::Mat &image) = 0;
105  virtual ChannelImpl *channelImpl(const std::string &name) = 0;
106  };
107 
109  {
110  public:
113 
114  virtual void setImage(const cv::Mat &image);
115  virtual ChannelImpl *channelImpl(const std::string &name);
116 
117  private:
118  std::map<std::string, ChannelImpl *> m_channelImpls;
119  };
120 
122  {
123  public:
124  Channel(Device *device, const Config &config);
125  ~Channel();
126 
127  void invalidate();
128 
129  const ObjectVector *objects() const;
130 
131  Device *device() const;
132 
136  void setConfig(const Config &config);
137 
138  private:
139  Device *m_device;
140  Config m_config;
141  mutable ObjectVector m_objects;
142  ChannelImpl *m_impl;
143  mutable bool m_valid;
144  };
145 
146  typedef std::vector<Channel *> ChannelPtrVector;
147 
149  {
150  public:
151  static std::string extension();
152 
153  static void setBasePath(const std::string &path);
154  static std::string path(const std::string &name = std::string());
155  static std::string defaultPath();
156  static std::string defaultConfigPath();
157  static void setDefaultConfigPath(const std::string &name);
158 
159  private:
160  static std::string s_path;
161  };
162 
164  {
165  public:
166  virtual ~InputProvider();
167  virtual bool open(const int number) = 0;
168  virtual bool isOpen() const = 0;
169  virtual void setWidth(const unsigned width) = 0;
170  virtual void setHeight(const unsigned height) = 0;
171  virtual bool next(cv::Mat &image) = 0;
172  virtual bool close() = 0;
173  };
174 
176  {
177  public:
179  ~UsbInputProvider();
180 
181  virtual bool open(const int number);
182  virtual bool isOpen() const;
183  virtual void setWidth(const unsigned width);
184  virtual void setHeight(const unsigned height);
185  virtual bool next(cv::Mat &image);
186  virtual bool close();
187 
188  private:
189  cv::VideoCapture *m_capture;
190  };
191 
193  {
194  public:
195  Device(InputProvider *const inputProvider);
196  ~Device();
197 
198  bool open(const int number = 0);
199  bool isOpen() const;
200  bool close();
201  bool update();
202 
203  void setWidth(const unsigned width);
204  void setHeight(const unsigned height);
205 
206  unsigned width() const;
207  unsigned height() const;
208 
209  const ChannelPtrVector &channels() const;
210 
211  InputProvider *inputProvider() const;
212  const cv::Mat &rawImage() const;
213 
214  void setConfig(const Config &config);
215  const Config &config() const;
216 
217  void setChannelImplManager(ChannelImplManager *channelImplManager);
218  ChannelImplManager *channelImplManager() const;
219 
220  private:
221  void updateConfig();
222 
223  InputProvider *const m_inputProvider;
224  Config m_config;
225  ChannelPtrVector m_channels;
226  ChannelImplManager *m_channelImplManager;
227  cv::Mat m_image;
228  timeval m_lastUpdate;
229  };
230 }
231 
232 
233 
234 #endif