mf
Media Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
opencv.h
Go to the documentation of this file.
1 #ifndef MF_OPENCV_H_
2 #define MF_OPENCV_H_
3 
4 #include <opencv2/opencv.hpp>
5 
6 #include "color.h"
7 #include "ndarray/ndcoord.h"
8 #include "ndarray/ndarray.h"
9 
10 namespace cv { // in OpenCV namespace
11  template<>
12  class DataType<::mf::rgb_color> {
13  public:
15  using work_type = int;
16  using channel_type = uchar;
17  enum {
18  generic_type = 0,
19  depth = DataDepth<channel_type>::value,
20  channels = 3,
21  fmt = ((channels - 1)<<8) + DataDepth<channel_type>::fmt,
22  type = CV_MAKETYPE(depth, channels)
23  };
24  using vec_type = Vec<channel_type, channels>;
25  };
26 
27  template<>
28  class DataType<::mf::ycbcr_color> {
29  public:
31  using work_type = int;
32  using channel_type = uchar;
33  enum {
34  generic_type = 0,
35  depth = DataDepth<channel_type>::fmt,
36  channels = 3,
37  fmt = ((channels - 1)<<8) + DataDepth<channel_type>::fmt,
38  type = CV_MAKETYPE(depth, channels)
39  };
40  using vec_type = Vec<channel_type, channels>;
41  };
42 }
43 
44 
45 namespace mf {
46 
47 
48 /*
50 
83 template<std::size_t Dim, typename Elem>
84 auto to_opencv_mat(const ndarray_view<Dim, Elem>& vw) {
85  using opencv_type = cv::DataType<Elem>;
86  using channel_type = typename opencv_type::channel_type;
87 
88  int sizes[Dim];
89  for(std::ptrdiff_t i = 0; i < Dim; ++i) sizes[i] = vw.shape()[i];
90 
91 /* if(vw.has_default_strides()) {
92  cv::Mat mat(
93  Dim,
94  sizes,
95  opencv_type::type,
96  reinterpret_cast<void*>(vw.start())
97  );
98  cv::Mat_<Elem> mat_(mat);
99  return mat_;
100 
101  } else {*/
102  ndarray<Dim, Elem> arr(vw);
103  cv::Mat mat(
104  Dim,
105  sizes,
106  opencv_type::type,
107  reinterpret_cast<void*>(arr.start())
108  );
109  cv::Mat_<Elem> mat_(mat);
110  cv::Mat_<Elem> mat_copy_;
111  mat_.copyTo(mat_copy_);
112  return mat_copy_;
113 
114 
115 }
116 
117 
118 template<std::size_t Dim, typename Elem>
119 bool copy_less_convertible_to_opencv_mat(const ndarray_view<Dim, Elem>& vw) {
120  return vw.has_default_strides();
121 }
122 
123 
124 
125 template<std::size_t Dim, typename T>
126 auto to_ndarray_view(const cv::Mat_<T>& mat) {
127  T* start = reinterpret_cast<T*>(mat.data);
128  ndcoord<Dim, int> shape(mat.size.p, mat.size.p + Dim);
129  ndcoord<Dim, std::size_t> strides(mat.step.p, mat.step.p + Dim);
130  return ndarray_view<Dim, T>(start, shape, strides);
131 }
132 
133 
134 }
135 
136 #endif
YCbCr color, 8 bit.
Definition: color.h:74
Vec< channel_type, channels > vec_type
Definition: opencv.h:40
int work_type
Definition: opencv.h:15
uchar channel_type
Definition: opencv.h:32
uchar channel_type
Definition: opencv.h:16
RGB color, 8 bit.
Definition: color.h:27
Vec< channel_type, channels > vec_type
Definition: opencv.h:24
int work_type
Definition: opencv.h:31