CinderOpenCV.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "opencv2/opencv.hpp"
4 
5 #include "cinder/Cinder.h"
6 #include "cinder/ImageIo.h"
7 
8 namespace cinder {
9 
10 class ImageTargetCvMat : public ImageTarget {
11  public:
12  static std::shared_ptr<ImageTargetCvMat> createRef( cv::Mat *mat ) { return std::shared_ptr<ImageTargetCvMat>( new ImageTargetCvMat( mat ) ); }
13 
14  virtual bool hasAlpha() const { return mMat->channels() == 4; }
15  virtual void* getRowPointer( int32_t row ) { return reinterpret_cast<void*>( reinterpret_cast<uint8_t*>(mMat->data) + row * mMat->step ); }
16 
17  protected:
19 
21 };
22 
23 class ImageSourceCvMat : public ImageSource {
24  public:
26  : ImageSource()
27  {
28  mWidth = mat.cols;
29  mHeight = mat.rows;
30  if( (mat.channels() == 3) || (mat.channels() == 4) ) {
32  if( mat.channels() == 4 )
34  else
36  }
37  else if( mat.channels() == 1 ) {
40  }
41 
42  switch( mat.depth() ) {
43  case CV_8U: setDataType( ImageIo::UINT8 ); break;
44  case CV_16U: setDataType( ImageIo::UINT16 ); break;
45  case CV_32F: setDataType( ImageIo::FLOAT32 ); break;
46  default:
48  }
49 
50  mRowBytes = mat.step;
51  mData = reinterpret_cast<const uint8_t*>( mat.data );
52  }
53 
55  // get a pointer to the ImageSource function appropriate for handling our data configuration
57 
58  const uint8_t *data = mData;
59  for( int32_t row = 0; row < mHeight; ++row ) {
60  ((*this).*func)( target, row, data );
61  data += mRowBytes;
62  }
63  }
64 
65  const uint8_t *mData;
66  int32_t mRowBytes;
67 };
68 
70 // ImageTargetCvMat
72  : ImageTarget(), mMat( mat )
73 {
74  switch( mat->depth() ) {
75  case CV_8U: setDataType( ImageIo::UINT8 ); break;
76  case CV_16U: setDataType( ImageIo::UINT16 ); break;
77  case CV_32F: setDataType( ImageIo::FLOAT32 ); break;
78  default:
80  }
81 
82  switch( mat->channels() ) {
83  case 1:
86  break;
87  case 3:
90  break;
91  case 4:
94  break;
95  default:
97  break;
98  }
99 }
100 
101 inline cv::Mat toOcv( ci::ImageSourceRef sourceRef, int type = -1 )
102 {
103  if( type == -1 ) {
104  int depth = CV_8U;
105  if( sourceRef->getDataType() == ImageIo::UINT16 )
106  depth = CV_16U;
107  else if( sourceRef->getDataType() == ImageIo::FLOAT32 )
108  depth = CV_32F;
109  int channels = ImageIo::channelOrderNumChannels( sourceRef->getChannelOrder() );
110  type = CV_MAKETYPE( depth, channels );
111  }
112 
113  cv::Mat result( sourceRef->getHeight(), sourceRef->getWidth(), type );
115  sourceRef->load( target );
116  return result;
117 }
118 
119 inline cv::Mat toOcvRef( Channel8u &channel )
120 {
121  return cv::Mat( channel.getHeight(), channel.getWidth(), CV_MAKETYPE( CV_8U, 1 ), channel.getData(), channel.getRowBytes() );
122 }
123 
124 inline cv::Mat toOcvRef( Channel32f &channel )
125 {
126  return cv::Mat( channel.getHeight(), channel.getWidth(), CV_MAKETYPE( CV_32F, 1 ), channel.getData(), channel.getRowBytes() );
127 }
128 
129 inline cv::Mat toOcvRef( Surface8u &surface )
130 {
131  return cv::Mat( surface.getHeight(), surface.getWidth(), CV_MAKETYPE( CV_8U, surface.hasAlpha()?4:3), surface.getData(), surface.getRowBytes() );
132 }
133 
134 inline cv::Mat toOcvRef( Surface32f &surface )
135 {
136  return cv::Mat( surface.getHeight(), surface.getWidth(), CV_MAKETYPE( CV_32F, surface.hasAlpha()?4:3), surface.getData(), surface.getRowBytes() );
137 }
138 
139 inline ImageSourceRef fromOcv( cv::Mat &mat )
140 {
141  return ImageSourceRef( new ImageSourceCvMat( mat ) );
142 }
143 
144 inline cv::Scalar toOcv( const Color &color )
145 {
146  return CV_RGB( color.r * 255, color.g * 255, color.b * 255 );
147 }
148 
149 inline Vec2f fromOcv( const cv::Point2f &point )
150 {
151  return Vec2f( point.x, point.y );
152 }
153 
154 inline cv::Point2f toOcv( const Vec2f &point )
155 {
156  return cv::Point2f( point.x, point.y );
157 }
158 
159 inline Vec2i fromOcv( const cv::Point &point )
160 {
161  return Vec2i( point.x, point.y );
162 }
163 
164 inline cv::Point toOcv( const Vec2i &point )
165 {
166  return cv::Point( point.x, point.y );
167 }
168 
169 inline cv::Rect toOcv( const ci::Area &r )
170 {
171  return cv::Rect( r.x1, r.y1, r.getWidth(), r.getHeight() );
172 }
173 
174 inline ci::Area fromOcv( const cv::Rect &r )
175 {
176  return Area( r.x, r.y, r.x + r.width, r.y + r.height );
177 }
178 
179 } // namespace cinder
GLdouble GLdouble GLdouble r
Point2i Point
Definition: core.hpp:893
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions ...
Definition: core.hpp:1962
static int8_t channelOrderNumChannels(ChannelOrder channelOrder)
Vec2< float > Vec2f
int channels() const
returns element type, similar to CV_MAT_CN(cvmat->type)
Definition: mat.hpp:401
const uint8_t * mData
Definition: CinderOpenCV.h:65
uchar * data
pointer to the data
Definition: core.hpp:1964
CvCmpFunc func
Definition: core_c.h:1072
virtual bool hasAlpha() const
Definition: CinderOpenCV.h:14
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: core_c.h:403
_Tp x
Definition: core.hpp:766
void setChannelOrder(ChannelOrder aChannelOrder)
GLenum target
cv::Mat * mMat
Definition: CinderOpenCV.h:20
int depth() const
returns element type, similar to CV_MAT_DEPTH(cvmat->type)
Definition: mat.hpp:400
int32_t mRowBytes
Definition: CinderOpenCV.h:66
_Tp height
Definition: core.hpp:883
double CvStereoLineCoeff CvPoint3D64f * point
Definition: legacy.hpp:558
std::shared_ptr< class ImageTarget > ImageTargetRef
const CvArr const CvArr CvArr * result
Definition: core_c.h:805
Definition: CinderOpenCV.h:23
int cols
Definition: core.hpp:1962
ChannelT< float > Channel32f
RowFunc setupRowFunc(ImageTargetRef target)
GLenum GLenum GLvoid * row
void setDataType(DataType aDataType)
CvArr const CvMat * mat
Definition: core_c.h:700
_Tp y
Definition: core.hpp:883
_Tp x
Definition: core.hpp:883
void load(ImageTargetRef target)
Definition: CinderOpenCV.h:54
The n-dimensional matrix class.
Definition: core.hpp:1688
GLuint GLuint GLsizei GLenum type
Definition: core_c.h:114
int int type
Definition: core_c.h:109
void(ImageSource::* RowFunc)(ImageTargetRef, int32_t, const void *)
int int channel
int int channels
Definition: core_c.h:73
ImageSourceCvMat(const cv::Mat &mat)
Definition: CinderOpenCV.h:25
SurfaceT< uint8_t > Surface8u
ColorT< float > Color
void setColorModel(ColorModel colorModel)
_Tp y
Definition: core.hpp:766
ImageTargetCvMat(cv::Mat *mat)
Definition: CinderOpenCV.h:71
std::shared_ptr< class ImageSource > ImageSourceRef
GLint GLint GLsizei GLsizei GLsizei depth
Definition: core_c.h:76
Point_< float > Point2f
Definition: core.hpp:898
Definition: CinderOpenCV.h:10
static std::shared_ptr< ImageTargetCvMat > createRef(cv::Mat *mat)
Definition: CinderOpenCV.h:12
SurfaceT< float > Surface32f
virtual void * getRowPointer(int32_t row)
Definition: CinderOpenCV.h:15
MStep step
Definition: core.hpp:2007
ChannelT< uint8_t > Channel8u
Rect_< int > Rect
Definition: core.hpp:897
_Tp width
Definition: core.hpp:883
Vec2< int > Vec2i
GLuint color
Definition: core_c.h:1276