SMIL  1.0.4
DOpenCVInterface.hpp
1 /*
2  * Copyright (c) 2011-2016, Matthieu FAESSEL and ARMINES
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Matthieu FAESSEL, or ARMINES nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 
30 #ifndef _D_OPENCV_IMAGE_HPP
31 #define _D_OPENCV_IMAGE_HPP
32 
33 #include <opencv/cv.h>
34 
35 #include "Core/include/private/DImage.hxx"
36 #include "Core/include/private/DSharedImage.hpp"
37 
38 
39 
40 namespace smil
41 {
42 
52  template <class T>
53  class OpenCVInt : public SharedImage<T>
54  {
55  public:
57 
59  OpenCVInt(cv::Mat &_cvMat)
60  {
61  BaseObject::className = "OpenCVInt";
62  parentClass::init();
63  this->attach((T*)(_cvMat.data), _cvMat.size().width, _cvMat.size().height);
64  }
66  OpenCVInt(IplImage *cvIm)
67  {
68  BaseObject::className = "OpenCVInt";
69  parentClass::init();
70  this->attach((T*)(cvIm->imageData), cvIm->width, cvIm->height);
71  }
72 
73  #if defined Py_PYCONFIG_H || defined SWIGPYTHON
74  private:
76  {
77  PyObject_HEAD;
78  IplImage *img;
79  PyObject *data;
80  size_t offset;
81  };
82 
83  public:
84  OpenCVInt(PyObject *obj)
85  {
86  if (!obj)
87  {
88  ERR_MSG("input object is NULL.");
89  return;
90  }
91  python_iplimage *pIm = (python_iplimage*)obj;
92  IplImage *cvIm = pIm->img;
93  if (!cvIm)
94  {
95  ERR_MSG("Error: Input object must be an IplImage.");
96  return;
97  }
98  BaseObject::className = "OpenCVInt";
99  parentClass::init();
100  this->attach((T*)(cvIm->imageData), cvIm->width, cvIm->height, 1);
101  }
102  #endif // SWIGPYTHON
103 
104  };
105 
106 
107  template <class T>
108  int getCvType()
109  {
110  return -1;
111  }
112  template<> inline int getCvType<UINT8>() { return CV_8UC1; }
113  template<> inline int getCvType<UINT16>() { return CV_16UC1; }
114 
118  template <class T>
119  cv::Mat toMatImage(Image<T> &im)
120  {
121  int cvType = getCvType<T>();
122 
123  ASSERT(cvType!=-1, "Data type conversion not implemented", cv::Mat())
124 
125  return cv::Mat(im.getHeight(), im.getWidth(), cvType, im.getPixels());
126  }
127 
128 
129 
132 } // namespace smil
133 
134 #endif // _D_OPENCV_IMAGE_HPP
size_t getWidth() const
Get image width.
Definition: DBaseImage.h:80
size_t getHeight() const
Get image height.
Definition: DBaseImage.h:85
Main Image class.
Definition: DImage.hpp:57
lineType getPixels() const
Get the pixels as a 1D array.
Definition: DImage.hpp:105
OpenCV Image Interface.
Definition: DOpenCVInterface.hpp:54
Image that uses an existing (1D) data pointer.
Definition: DSharedImage.hpp:51
OpenCVInt(cv::Mat &_cvMat)
Constructor.
Definition: DOpenCVInterface.hpp:59
cv::Mat toMatImage(Image< T > &im)
Create a OpneCV Mat from a Smil image (the data is copied)
Definition: DOpenCVInterface.hpp:119
OpenCVInt(IplImage *cvIm)
Constructor.
Definition: DOpenCVInterface.hpp:66
Definition: DOpenCVInterface.hpp:76