gmat.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 or `cv::GArray<T>`) and are
  27. * used only to construct 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 when passing input and output data
  34. * structures to G-API:
  35. *
  36. * G-API data type | I/O data type
  37. * ------------------ | -------------
  38. * cv::GMat | cv::Mat, cv::UMat, cv::RMat
  39. * cv::GScalar | cv::Scalar
  40. * `cv::GArray<T>` | std::vector<T>
  41. * `cv::GOpaque<T>` | T
  42. * cv::GFrame | cv::MediaFrame
  43. */
  44. /**
  45. * @brief GMat class represents image or tensor data in the
  46. * graph.
  47. *
  48. * GMat doesn't store any data itself, instead it describes a
  49. * functional relationship between operations consuming and producing
  50. * GMat objects.
  51. *
  52. * GMat is a virtual counterpart of Mat and UMat, but it
  53. * doesn't mean G-API use Mat or UMat objects internally to represent
  54. * GMat objects -- the internal data representation may be
  55. * backend-specific or optimized out at all.
  56. *
  57. * @sa Mat, GMatDesc
  58. */
  59. class GAPI_EXPORTS_W_SIMPLE GMat
  60. {
  61. public:
  62. /**
  63. * @brief Constructs an empty GMat
  64. *
  65. * Normally, empty G-API data objects denote a starting point of
  66. * the graph. When an empty GMat is assigned to a result of some
  67. * operation, it obtains a functional link to this operation (and
  68. * is not empty anymore).
  69. */
  70. GAPI_WRAP GMat(); // Empty constructor
  71. /**
  72. * @brief Constructs a value-initialized GMat
  73. *
  74. * GMat may be associated with a buffer at graph construction time.
  75. * It is useful when some operation has a Mat input which doesn't
  76. * change during the program execution, and is set only once.
  77. * In this case, there's no need to declare such GMat as graph input.
  78. *
  79. * @param m a cv::Mat buffer to associate with this GMat object.
  80. */
  81. GAPI_WRAP explicit GMat(cv::Mat m); // Value-initialization constructor
  82. /// @private
  83. GMat(const GNode &n, std::size_t out); // Operation result constructor
  84. /// @private
  85. GOrigin& priv(); // Internal use only
  86. /// @private
  87. const GOrigin& priv() const; // Internal use only
  88. private:
  89. std::shared_ptr<GOrigin> m_priv;
  90. };
  91. class GAPI_EXPORTS GMatP : public GMat
  92. {
  93. public:
  94. using GMat::GMat;
  95. };
  96. class RMat;
  97. /** @} */
  98. /**
  99. * \addtogroup gapi_meta_args
  100. * @{
  101. */
  102. struct GAPI_EXPORTS_W_SIMPLE GMatDesc
  103. {
  104. // FIXME: Default initializers in C++14
  105. GAPI_PROP int depth;
  106. GAPI_PROP int chan;
  107. GAPI_PROP cv::Size size; // NB.: no multi-dimensional cases covered yet
  108. GAPI_PROP bool planar;
  109. GAPI_PROP std::vector<int> dims; // FIXME: Maybe it's real questionable to have it here
  110. GAPI_WRAP GMatDesc(int d, int c, cv::Size s, bool p = false)
  111. : depth(d), chan(c), size(s), planar(p) {}
  112. GAPI_WRAP GMatDesc(int d, const std::vector<int> &dd)
  113. : depth(d), chan(-1), size{-1,-1}, planar(false), dims(dd) {}
  114. GAPI_WRAP GMatDesc(int d, std::vector<int> &&dd)
  115. : depth(d), chan(-1), size{-1,-1}, planar(false), dims(std::move(dd)) {}
  116. GAPI_WRAP GMatDesc() : GMatDesc(-1, -1, {-1,-1}) {}
  117. inline bool operator== (const GMatDesc &rhs) const
  118. {
  119. return depth == rhs.depth
  120. && chan == rhs.chan
  121. && size == rhs.size
  122. && planar == rhs.planar
  123. && dims == rhs.dims;
  124. }
  125. inline bool operator!= (const GMatDesc &rhs) const
  126. {
  127. return !(*this == rhs);
  128. }
  129. bool isND() const { return !dims.empty(); }
  130. // Checks if the passed mat can be described by this descriptor
  131. // (it handles the case when
  132. // 1-channel mat can be reinterpreted as is (1-channel mat)
  133. // and as a 3-channel planar mat with height divided by 3)
  134. bool canDescribe(const cv::Mat& mat) const;
  135. bool canDescribe(const cv::RMat& mat) const;
  136. // Meta combinator: return a new GMatDesc which differs in size by delta
  137. // (all other fields are taken unchanged from this GMatDesc)
  138. // FIXME: a better name?
  139. GAPI_WRAP GMatDesc withSizeDelta(cv::Size delta) const
  140. {
  141. GMatDesc desc(*this);
  142. desc.size += delta;
  143. return desc;
  144. }
  145. // Meta combinator: return a new GMatDesc which differs in size by delta
  146. // (all other fields are taken unchanged from this GMatDesc)
  147. //
  148. // This is an overload.
  149. GAPI_WRAP GMatDesc withSizeDelta(int dx, int dy) const
  150. {
  151. return withSizeDelta(cv::Size{dx,dy});
  152. }
  153. GAPI_WRAP GMatDesc withSize(cv::Size sz) const
  154. {
  155. GMatDesc desc(*this);
  156. desc.size = sz;
  157. return desc;
  158. }
  159. // Meta combinator: return a new GMatDesc with specified data depth.
  160. // (all other fields are taken unchanged from this GMatDesc)
  161. GAPI_WRAP GMatDesc withDepth(int ddepth) const
  162. {
  163. GAPI_Assert(CV_MAT_CN(ddepth) == 1 || ddepth == -1);
  164. GMatDesc desc(*this);
  165. if (ddepth != -1) desc.depth = ddepth;
  166. return desc;
  167. }
  168. // Meta combinator: return a new GMatDesc with specified data depth
  169. // and number of channels.
  170. // (all other fields are taken unchanged from this GMatDesc)
  171. GAPI_WRAP GMatDesc withType(int ddepth, int dchan) const
  172. {
  173. GAPI_Assert(CV_MAT_CN(ddepth) == 1 || ddepth == -1);
  174. GMatDesc desc = withDepth(ddepth);
  175. desc.chan = dchan;
  176. return desc;
  177. }
  178. // Meta combinator: return a new GMatDesc with planar flag set
  179. // (no size changes are performed, only channel interpretation is changed
  180. // (interleaved -> planar)
  181. GAPI_WRAP GMatDesc asPlanar() const
  182. {
  183. GAPI_Assert(planar == false);
  184. GMatDesc desc(*this);
  185. desc.planar = true;
  186. return desc;
  187. }
  188. // Meta combinator: return a new GMatDesc
  189. // reinterpreting 1-channel input as planar image
  190. // (size height is divided by plane number)
  191. GAPI_WRAP GMatDesc asPlanar(int planes) const
  192. {
  193. GAPI_Assert(planar == false);
  194. GAPI_Assert(chan == 1);
  195. GAPI_Assert(planes > 1);
  196. GAPI_Assert(size.height % planes == 0);
  197. GMatDesc desc(*this);
  198. desc.size.height /= planes;
  199. desc.chan = planes;
  200. return desc.asPlanar();
  201. }
  202. // Meta combinator: return a new GMatDesc with planar flag set to false
  203. // (no size changes are performed, only channel interpretation is changed
  204. // (planar -> interleaved)
  205. GAPI_WRAP GMatDesc asInterleaved() const
  206. {
  207. GAPI_Assert(planar == true);
  208. GMatDesc desc(*this);
  209. desc.planar = false;
  210. return desc;
  211. }
  212. };
  213. static inline GMatDesc empty_gmat_desc() { return GMatDesc{-1,-1,{-1,-1}}; }
  214. namespace gapi { namespace detail {
  215. /** Checks GMatDesc fields if the passed matrix is a set of n-dimentional points.
  216. @param in GMatDesc to check.
  217. @param n expected dimensionality.
  218. @return the amount of points. In case input matrix can't be described as vector of points
  219. of expected dimensionality, returns -1.
  220. */
  221. int checkVector(const GMatDesc& in, const size_t n);
  222. /** @overload
  223. Checks GMatDesc fields if the passed matrix can be described as a set of points of any
  224. dimensionality.
  225. @return array of two elements in form of std::vector<int>: the amount of points
  226. and their calculated dimensionality. In case input matrix can't be described as vector of points,
  227. returns {-1, -1}.
  228. */
  229. std::vector<int> checkVector(const GMatDesc& in);
  230. }} // namespace gapi::detail
  231. #if !defined(GAPI_STANDALONE)
  232. GAPI_EXPORTS GMatDesc descr_of(const cv::UMat &mat);
  233. #endif // !defined(GAPI_STANDALONE)
  234. //Fwd declarations
  235. namespace gapi { namespace own {
  236. class Mat;
  237. GAPI_EXPORTS GMatDesc descr_of(const Mat &mat);
  238. }}//gapi::own
  239. GAPI_EXPORTS GMatDesc descr_of(const RMat &mat);
  240. #if !defined(GAPI_STANDALONE)
  241. GAPI_EXPORTS GMatDesc descr_of(const cv::Mat &mat);
  242. #else
  243. using gapi::own::descr_of;
  244. #endif
  245. /** @} */
  246. GAPI_EXPORTS std::ostream& operator<<(std::ostream& os, const cv::GMatDesc &desc);
  247. } // namespace cv
  248. #endif // OPENCV_GAPI_GMAT_HPP