Image Component Library (ICL)
OpenCV.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLCore/src/ICLCore/OpenCV.h **
10 ** Module : ICLCore **
11 ** Authors: Christof Elbrechter, Christian Groszewski **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <opencv/cxcore.h>
34 
35 #ifndef ICL_HAVE_OPENCV_OLD_STYLE
36 
37 #include <opencv2/core/core.hpp>
38 #include <opencv2/core/version.hpp>
39 
40 #if defined(CV_VERSION_EPOCH) && (CV_VERSION_EPOCH > 2)
41 #define ICL_HAVE_OPENCV_3
42 #elif !defined(CV_VERSION_EPOCH) && (CV_VERSION_MAJOR > 2)
43 #define ICL_HAVE_OPENCV_3
44 #endif
45 
46 #endif
47 
48 // crazy stuff !!
49 #ifdef ICL_HAVE_OPENCV_3
50 #define ICL_OPENCV_VERSION_MAJOR CV_VERSION_MAJOR
51 #define ICL_OPENCV_VERSION_MINOR CV_VERSION_MINOR
52 #else
53 #define ICL_OPENCV_VERSION_MAJOR CV_VERSION_EPOCH
54 #define ICL_OPENCV_VERSION_MINOR CV_VERSION_MAJOR
55 #endif
56 
57 
58 #if defined(ICL_HAVE_OPENCV_3)
59  #include "opencv2/calib3d/calib3d_c.h"
60 // This will be inlcuded when at least OpenCV 2.4 is detected
61 #elif ICL_OPENCV_VERSION_MINOR > 3
62  #include "opencv2/imgproc/types_c.h"
63  #include "opencv2/imgproc/imgproc_c.h"
64  #include "opencv2/calib3d/calib3d.hpp"
65 #else
66  #error ICL requires at least OpenCV 2.4
67 #endif
68 
69 #include <ICLCore/CCFunctions.h>
70 #include <ICLCore/Img.h>
71 #include <ICLCore/ImgBase.h>
72 #include <ICLUtils/Uncopyable.h>
73 
74 namespace icl{
75  namespace core{
76 
81  };
82 
84 
89  ICLCore_API ImgBase *ipl_to_img(CvArr *src,ImgBase **dst=0,DepthPreference e=PREFERE_SRC_DEPTH) ;
90 
92 
97  ICLCore_API IplImage *img_to_ipl(const ImgBase *src, IplImage **dst = 0, DepthPreference e = PREFERE_SRC_DEPTH);
98 
100 
105  ICLCore_API CvMat* img_to_cvmat(const ImgBase *src, CvMat *dst = 0, int channel = 0) ;
106 
108 
114  ICLCore_API IplImage *img_to_ipl_shallow(ImgBase *src, IplImage *dst = 0);
115 
117 
122  ICLCore_API CvMat *img_to_cvmat_shallow(const ImgBase *src, CvMat *dst = 0) ;
123 
125 
127  ICLCore_API ::cv::Mat *img_to_mat(const ImgBase *src, ::cv::Mat *dst=0) ;
128 
130  ICLCore_API ImgBase *mat_to_img(const ::cv::Mat *src, ImgBase *dstIn=0) ;
131 
133  ICLCore_API void mat_to_img(const ::cv::Mat *src, ImgBase **dstIn) ;
134 
135 
139  MatWrapper();
140  MatWrapper(depth d, const utils::Size &size, int channels);
141  MatWrapper(const MatWrapper &other);
142  explicit MatWrapper(const ::cv::Mat &other);
143 
144  void adapt(depth d, const utils::Size &size, int channels);
145 
146  MatWrapper &operator=(const ::cv::Mat &other);
147  MatWrapper &operator=(const MatWrapper &other);
148  MatWrapper &operator=(const ImgBase &image);
149  void copyTo(ImgBase **dst);
150  void convertTo(ImgBase &dst);
151 
152  utils::Size getSize() const;
153  int getChannels() const;
154  depth getDepth() const;
155 
156  template<class T> ICLCore_API
157  T* getInterleavedData();
158 
159  template<class T> ICLCore_API
160  const T* getInterleavedData() const;
161  };
162 
164  template<class T>
166  inline int icl_get_cv_mat_type() {
167  throw utils::ICLException("icl_get_cv_mat_type: invalid type");
168  return 0;
169  }
170 
172  template<> inline int icl_get_cv_mat_type<int>() { return CV_32SC1; }
173 
175  template<> inline int icl_get_cv_mat_type<float>() { return CV_32FC1; }
178  struct CvMatDelOp : public utils::DelOpBase{
180  static void delete_func(CvMat *m){
181  if(m) cvReleaseMat(&m);
182  }
183  };
184 
186 
188  template<class T>
192 
193  public:
195  CvMatWrapper(int nrows=0, int ncols=0):
196  size(ncols, nrows),
197  m( nrows * ncols > 0 ? cvCreateMat(nrows, ncols, icl_get_cv_mat_type<T>()) : 0, true){}
198 
200  size(size),
201  m( size.getDim() > 0 ? cvCreateMat(size.height, size.width, icl_get_cv_mat_type<T>()) : 0, true){}
202 
204 
205  CvMatWrapper(CvMat *other, bool takeOwnerShip=false):
206  size(0,0), m(other, takeOwnerShip){
207 
208  if(other){
209  CvSize size = cvGetSize(other);
210  this->size = utils::Size(size.width,size.height);
211  }
212  }
213 
215  CvMat *get() { return m.get(); }
216 
218  const CvMat *get() const { return m.get(); }
219 
221  void operator=(const Img<T> &image){
222  img_to_cvmat(&image, get(), 0);
223  }
224 
226 
227  inline void setSize(const utils::Size &size){
228  if(size == this->size) return;
229  this->size = size;
230  if(size.getDim() > 0){
231  CvMat *m = cvCreateMat(size.height, size.width, icl_get_cv_mat_type<T>());
233  }else{
235  }
236  }
237 
239  inline bool isNull() const { return m.get() == 0; }
240 
242  inline const utils::Size &getSize() const {
243  return size;
244  }
245 
247  T &operator()(int y, int x) {
248  return CV_MAT_ELEM(*m,T,y,x);
249  }
250 
252  const T &operator()(int y, int x) const{
253  return CV_MAT_ELEM((const_cast<CvMat&>(*m)),T,y,x);
254  }
255 
256  };
257 
258 
260  template<class T>
261  inline std::ostream &operator<<(std::ostream &str, const CvMatWrapper<T> &m){
262  utils::Size s = m.getSize();
263  std::cout << "CvMatWrapper of a " << s.height << " x " << s.width << " matrix:" << "\n";
264 
265  for(int y=0;y<s.height;++y){
266  for(int x=0;x<s.width;++x){
267  str << m(y,x) << ((x == s.width-1) ? "\n" : " ");
268  }
269  }
270  return str;
271  }
272 
273  } // namespace core
274 }
undocument this line if you encounter any issues!
Definition: Any.h:37
Class interface for un-copyable classes.
Definition: Uncopyable.h:64
static void delete_func(CvMat *m)
Definition: OpenCV.h:180
Very simply wrapper about the opencv C++ matrix type cv::Mat.
Definition: OpenCV.h:137
CvMat * get()
returns the current wrapped CvMat pointer
Definition: OpenCV.h:215
const T & operator()(int y, int x) const
index operator (const)
Definition: OpenCV.h:252
ICLCore_API std::ostream & operator<<(std::ostream &s, const format &f)
puts a string representation of format into the given stream
Mat4D32f Mat
Short typedef for 4D float matrices.
Definition: GeomDefs.h:90
ICLCore_API IplImage * img_to_ipl(const ImgBase *src, IplImage **dst=0, DepthPreference e=PREFERE_SRC_DEPTH)
Convert ICLimage to OpenCV IplImage.
CvMatWrapper(int nrows=0, int ncols=0)
Constructor, creates a CvMat with given dimensions.
Definition: OpenCV.h:195
Utility class that wraps around a CvMat of type CV_32FC1.
Definition: OpenCV.h:189
utils::Size size
current size
Definition: OpenCV.h:190
ICLCore_API IplImage * img_to_ipl_shallow(ImgBase *src, IplImage *dst=0)
Convert single channel ICLimage to OpenCV IplImage.
void operator=(const Img< T > &image)
assigns given image (channel 0 only)
Definition: OpenCV.h:221
prefer source depth
Definition: OpenCV.h:79
ICLCore_API ImgBase * ipl_to_img(CvArr *src, ImgBase **dst=0, DepthPreference e=PREFERE_SRC_DEPTH)
Convert OpenCV IplImage to ICLimage.
ICLCore_API ImgBase * mat_to_img(const ::cv::Mat *src, ImgBase *dstIn=0)
converts cv::Mat to ImgBase (internally the pixel data is type-converted if needed)
::cv::Mat mat
Definition: OpenCV.h:138
const utils::Size & getSize() const
returns the current size
Definition: OpenCV.h:242
Size class of the ICL.
Definition: Size.h:61
CvMatWrapper(const utils::Size &size)
Definition: OpenCV.h:199
depth getDepth()
getDepth<T> returns to depth enum associated to type T
ICLCore_API ::cv::Mat * img_to_mat(const ImgBase *src, ::cv::Mat *dst=0)
converts icl image into opencv's C++ image type cv::Mat (data is deeply copied)
std::string str(const T &t)
convert a data type into a string using an std::ostringstream instance
Definition: StringUtils.h:136
ICLCore_API CvMat * img_to_cvmat_shallow(const ImgBase *src, CvMat *dst=0)
Convert single channel ICLimage to OpenCV CvMat.
depth
determines the pixel type of an image (8Bit-int or 32Bit-float)
Definition: Types.h:60
utils::SmartPtrBase< CvMat, CvMatDelOp > m
Definition: OpenCV.h:191
Base class for Exception handling in the ICL.
Definition: Exception.h:42
ICLCore_API CvMat * img_to_cvmat(const ImgBase *src, CvMat *dst=0, int channel=0)
Copy single ICLimage channel to OpenCV single channel CvMat.
CvMatWrapper(CvMat *other, bool takeOwnerShip=false)
Constructor with given source matrix (wrappes around that)
Definition: OpenCV.h:205
bool isNull() const
returns whether a non-null pointer is wrapped
Definition: OpenCV.h:239
T & operator()(int y, int x)
index operator
Definition: OpenCV.h:247
int getDim() const
reutrns width*height
Definition: Size.h:211
Base class for reference counting smart-pointers.
Definition: SmartPtrBase.h:222
ICLQt_API ImgQ channel(const ImgQ &image, int channel)
picks a specific image channel
const CvMat * get() const
returns the current wrapped CvMat pointer (const)
Definition: OpenCV.h:218
void setSize(const utils::Size &size)
adapts the size (if necessary)
Definition: OpenCV.h:227
DepthPreference
Modes that define whether to prefer the source image's or the destination image's depth.
Definition: OpenCV.h:78
ImgBase is the Image-Interface class that provides save access to underlying Img-template .
Definition: ImgBase.h:131
#define ICLCore_API
Definition: CompatMacros.h:174
prefer destination depth
Definition: OpenCV.h:80