gmat.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. //
  5. // Copyright (C) 2018-2020 Intel Corporation
  6. #ifndef OPENCV_GAPI_GMAT_HPP
  7. #define OPENCV_GAPI_GMAT_HPP
  8. #include <ostream>
  9. #include <memory> // std::shared_ptr
  10. #include <opencv2/gapi/opencv_includes.hpp>
  11. #include <opencv2/gapi/gcommon.hpp> // GShape
  12. #include <opencv2/gapi/own/assert.hpp>
  13. // TODO GAPI_EXPORTS or so
  14. namespace cv
  15. {
  16. // Forward declaration; GNode and GOrigin are an internal
  17. // (user-inaccessible) classes.
  18. class GNode;
  19. struct GOrigin;
  20. /** \addtogroup gapi_data_objects
  21. * @{
  22. *
  23. * @brief G-API data objects used to build G-API expressions.
  24. *
  25. * These objects do not own any particular data (except compile-time
  26. * associated values like with cv::GScalar) and are used to construct
  27. * graphs.
  28. *
  29. * Every graph in G-API starts and ends with data objects.
  30. *
  31. * Once constructed and compiled, G-API operates with regular host-side
  32. * data instead. Refer to the below table to find the mapping between
  33. * G-API and regular data types.
  34. *
  35. * G-API data type | I/O data type
  36. * ------------------ | -------------
  37. * cv::GMat | cv::Mat
  38. * cv::GScalar | cv::Scalar
  39. * `cv::GArray<T>` | std::vector<T>
  40. * `cv::GOpaque<T>` | T
  41. */
  42. class GAPI_EXPORTS_W_SIMPLE GMat
  43. {
  44. public:
  45. GAPI_WRAP GMat(); // Empty constructor
  46. GMat(const GNode &n, std::size_t out); // Operation result constructor
  47. GOrigin& priv(); // Internal use only
  48. const GOrigin& priv() const; // Internal use only
  49. private:
  50. std::shared_ptr<GOrigin> m_priv;
  51. };
  52. class GAPI_EXPORTS GMatP : public GMat
  53. {
  54. public:
  55. using GMat::GMat;
  56. };
  57. class RMat;
  58. /** @} */
  59. /**
  60. * \addtogroup gapi_meta_args
  61. * @{
  62. */
  63. struct GAPI_EXPORTS GMatDesc
  64. {
  65. // FIXME: Default initializers in C++14
  66. int depth;
  67. int chan;
  68. cv::Size size; // NB.: no multi-dimensional cases covered yet
  69. bool planar;
  70. std::vector<int> dims; // FIXME: Maybe it's real questionable to have it here
  71. GMatDesc(int d, int c, cv::Size s, bool p = false)
  72. : depth(d), chan(c), size(s), planar(p) {}
  73. GMatDesc(int d, const std::vector<int> &dd)
  74. : depth(d), chan(-1), size{-1,-1}, planar(false), dims(dd) {}
  75. GMatDesc(int d, std::vector<int> &&dd)
  76. : depth(d), chan(-1), size{-1,-1}, planar(false), dims(std::move(dd)) {}
  77. GMatDesc() : GMatDesc(-1, -1, {-1,-1}) {}
  78. inline bool operator== (const GMatDesc &rhs) const
  79. {
  80. return depth == rhs.depth
  81. && chan == rhs.chan
  82. && size == rhs.size
  83. && planar == rhs.planar
  84. && dims == rhs.dims;
  85. }
  86. inline bool operator!= (const GMatDesc &rhs) const
  87. {
  88. return !(*this == rhs);
  89. }
  90. bool isND() const { return !dims.empty(); }
  91. // Checks if the passed mat can be described by this descriptor
  92. // (it handles the case when
  93. // 1-channel mat can be reinterpreted as is (1-channel mat)
  94. // and as a 3-channel planar mat with height divided by 3)
  95. bool canDescribe(const cv::Mat& mat) const;
  96. bool canDescribe(const cv::RMat& mat) const;
  97. // Meta combinator: return a new GMatDesc which differs in size by delta
  98. // (all other fields are taken unchanged from this GMatDesc)
  99. // FIXME: a better name?
  100. GMatDesc withSizeDelta(cv::Size delta) const
  101. {
  102. GMatDesc desc(*this);
  103. desc.size += delta;
  104. return desc;
  105. }
  106. // Meta combinator: return a new GMatDesc which differs in size by delta
  107. // (all other fields are taken unchanged from this GMatDesc)
  108. //
  109. // This is an overload.
  110. GMatDesc withSizeDelta(int dx, int dy) const
  111. {
  112. return withSizeDelta(cv::Size{dx,dy});
  113. }
  114. GMatDesc withSize(cv::Size sz) const
  115. {
  116. GMatDesc desc(*this);
  117. desc.size = sz;
  118. return desc;
  119. }
  120. // Meta combinator: return a new GMatDesc with specified data depth.
  121. // (all other fields are taken unchanged from this GMatDesc)
  122. GMatDesc withDepth(int ddepth) const
  123. {
  124. GAPI_Assert(CV_MAT_CN(ddepth) == 1 || ddepth == -1);
  125. GMatDesc desc(*this);
  126. if (ddepth != -1) desc.depth = ddepth;
  127. return desc;
  128. }
  129. // Meta combinator: return a new GMatDesc with specified data depth
  130. // and number of channels.
  131. // (all other fields are taken unchanged from this GMatDesc)
  132. GMatDesc withType(int ddepth, int dchan) const
  133. {
  134. GAPI_Assert(CV_MAT_CN(ddepth) == 1 || ddepth == -1);
  135. GMatDesc desc = withDepth(ddepth);
  136. desc.chan = dchan;
  137. return desc;
  138. }
  139. // Meta combinator: return a new GMatDesc with planar flag set
  140. // (no size changes are performed, only channel interpretation is changed
  141. // (interleaved -> planar)
  142. GMatDesc asPlanar() const
  143. {
  144. GAPI_Assert(planar == false);
  145. GMatDesc desc(*this);
  146. desc.planar = true;
  147. return desc;
  148. }
  149. // Meta combinator: return a new GMatDesc
  150. // reinterpreting 1-channel input as planar image
  151. // (size height is divided by plane number)
  152. GMatDesc asPlanar(int planes) const
  153. {
  154. GAPI_Assert(planar == false);
  155. GAPI_Assert(chan == 1);
  156. GAPI_Assert(planes > 1);
  157. GAPI_Assert(size.height % planes == 0);
  158. GMatDesc desc(*this);
  159. desc.size.height /= planes;
  160. desc.chan = planes;
  161. return desc.asPlanar();
  162. }
  163. // Meta combinator: return a new GMatDesc with planar flag set to false
  164. // (no size changes are performed, only channel interpretation is changed
  165. // (planar -> interleaved)
  166. GMatDesc asInterleaved() const
  167. {
  168. GAPI_Assert(planar == true);
  169. GMatDesc desc(*this);
  170. desc.planar = false;
  171. return desc;
  172. }
  173. };
  174. static inline GMatDesc empty_gmat_desc() { return GMatDesc{-1,-1,{-1,-1}}; }
  175. namespace gapi { namespace detail {
  176. /** Checks GMatDesc fields if the passed matrix is a set of n-dimentional points.
  177. @param in GMatDesc to check.
  178. @param n expected dimensionality.
  179. @return the amount of points. In case input matrix can't be described as vector of points
  180. of expected dimensionality, returns -1.
  181. */
  182. int checkVector(const GMatDesc& in, const size_t n);
  183. /** @overload
  184. Checks GMatDesc fields if the passed matrix can be described as a set of points of any
  185. dimensionality.
  186. @return array of two elements in form of std::vector<int>: the amount of points
  187. and their calculated dimensionality. In case input matrix can't be described as vector of points,
  188. returns {-1, -1}.
  189. */
  190. std::vector<int> checkVector(const GMatDesc& in);
  191. }} // namespace gapi::detail
  192. #if !defined(GAPI_STANDALONE)
  193. GAPI_EXPORTS GMatDesc descr_of(const cv::UMat &mat);
  194. #endif // !defined(GAPI_STANDALONE)
  195. //Fwd declarations
  196. namespace gapi { namespace own {
  197. class Mat;
  198. GAPI_EXPORTS GMatDesc descr_of(const Mat &mat);
  199. }}//gapi::own
  200. GAPI_EXPORTS GMatDesc descr_of(const RMat &mat);
  201. #if !defined(GAPI_STANDALONE)
  202. GAPI_EXPORTS GMatDesc descr_of(const cv::Mat &mat);
  203. #else
  204. using gapi::own::descr_of;
  205. #endif
  206. /** @} */
  207. GAPI_EXPORTS std::ostream& operator<<(std::ostream& os, const cv::GMatDesc &desc);
  208. } // namespace cv
  209. #endif // OPENCV_GAPI_GMAT_HPP