gmat.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. /// @private
  72. GMat(const GNode &n, std::size_t out); // Operation result constructor
  73. /// @private
  74. GOrigin& priv(); // Internal use only
  75. /// @private
  76. const GOrigin& priv() const; // Internal use only
  77. private:
  78. std::shared_ptr<GOrigin> m_priv;
  79. };
  80. class GAPI_EXPORTS GMatP : public GMat
  81. {
  82. public:
  83. using GMat::GMat;
  84. };
  85. class RMat;
  86. /** @} */
  87. /**
  88. * \addtogroup gapi_meta_args
  89. * @{
  90. */
  91. struct GAPI_EXPORTS_W_SIMPLE GMatDesc
  92. {
  93. // FIXME: Default initializers in C++14
  94. GAPI_PROP int depth;
  95. GAPI_PROP int chan;
  96. GAPI_PROP cv::Size size; // NB.: no multi-dimensional cases covered yet
  97. GAPI_PROP bool planar;
  98. GAPI_PROP std::vector<int> dims; // FIXME: Maybe it's real questionable to have it here
  99. GAPI_WRAP GMatDesc(int d, int c, cv::Size s, bool p = false)
  100. : depth(d), chan(c), size(s), planar(p) {}
  101. GAPI_WRAP GMatDesc(int d, const std::vector<int> &dd)
  102. : depth(d), chan(-1), size{-1,-1}, planar(false), dims(dd) {}
  103. GAPI_WRAP GMatDesc(int d, std::vector<int> &&dd)
  104. : depth(d), chan(-1), size{-1,-1}, planar(false), dims(std::move(dd)) {}
  105. GAPI_WRAP GMatDesc() : GMatDesc(-1, -1, {-1,-1}) {}
  106. inline bool operator== (const GMatDesc &rhs) const
  107. {
  108. return depth == rhs.depth
  109. && chan == rhs.chan
  110. && size == rhs.size
  111. && planar == rhs.planar
  112. && dims == rhs.dims;
  113. }
  114. inline bool operator!= (const GMatDesc &rhs) const
  115. {
  116. return !(*this == rhs);
  117. }
  118. bool isND() const { return !dims.empty(); }
  119. // Checks if the passed mat can be described by this descriptor
  120. // (it handles the case when
  121. // 1-channel mat can be reinterpreted as is (1-channel mat)
  122. // and as a 3-channel planar mat with height divided by 3)
  123. bool canDescribe(const cv::Mat& mat) const;
  124. bool canDescribe(const cv::RMat& mat) const;
  125. // Meta combinator: return a new GMatDesc which differs in size by delta
  126. // (all other fields are taken unchanged from this GMatDesc)
  127. // FIXME: a better name?
  128. GAPI_WRAP GMatDesc withSizeDelta(cv::Size delta) const
  129. {
  130. GMatDesc desc(*this);
  131. desc.size += delta;
  132. return desc;
  133. }
  134. // Meta combinator: return a new GMatDesc which differs in size by delta
  135. // (all other fields are taken unchanged from this GMatDesc)
  136. //
  137. // This is an overload.
  138. GAPI_WRAP GMatDesc withSizeDelta(int dx, int dy) const
  139. {
  140. return withSizeDelta(cv::Size{dx,dy});
  141. }
  142. GAPI_WRAP GMatDesc withSize(cv::Size sz) const
  143. {
  144. GMatDesc desc(*this);
  145. desc.size = sz;
  146. return desc;
  147. }
  148. // Meta combinator: return a new GMatDesc with specified data depth.
  149. // (all other fields are taken unchanged from this GMatDesc)
  150. GAPI_WRAP GMatDesc withDepth(int ddepth) const
  151. {
  152. GAPI_Assert(CV_MAT_CN(ddepth) == 1 || ddepth == -1);
  153. GMatDesc desc(*this);
  154. if (ddepth != -1) desc.depth = ddepth;
  155. return desc;
  156. }
  157. // Meta combinator: return a new GMatDesc with specified data depth
  158. // and number of channels.
  159. // (all other fields are taken unchanged from this GMatDesc)
  160. GAPI_WRAP GMatDesc withType(int ddepth, int dchan) const
  161. {
  162. GAPI_Assert(CV_MAT_CN(ddepth) == 1 || ddepth == -1);
  163. GMatDesc desc = withDepth(ddepth);
  164. desc.chan = dchan;
  165. return desc;
  166. }
  167. // Meta combinator: return a new GMatDesc with planar flag set
  168. // (no size changes are performed, only channel interpretation is changed
  169. // (interleaved -> planar)
  170. GAPI_WRAP GMatDesc asPlanar() const
  171. {
  172. GAPI_Assert(planar == false);
  173. GMatDesc desc(*this);
  174. desc.planar = true;
  175. return desc;
  176. }
  177. // Meta combinator: return a new GMatDesc
  178. // reinterpreting 1-channel input as planar image
  179. // (size height is divided by plane number)
  180. GAPI_WRAP GMatDesc asPlanar(int planes) const
  181. {
  182. GAPI_Assert(planar == false);
  183. GAPI_Assert(chan == 1);
  184. GAPI_Assert(planes > 1);
  185. GAPI_Assert(size.height % planes == 0);
  186. GMatDesc desc(*this);
  187. desc.size.height /= planes;
  188. desc.chan = planes;
  189. return desc.asPlanar();
  190. }
  191. // Meta combinator: return a new GMatDesc with planar flag set to false
  192. // (no size changes are performed, only channel interpretation is changed
  193. // (planar -> interleaved)
  194. GAPI_WRAP GMatDesc asInterleaved() const
  195. {
  196. GAPI_Assert(planar == true);
  197. GMatDesc desc(*this);
  198. desc.planar = false;
  199. return desc;
  200. }
  201. };
  202. static inline GMatDesc empty_gmat_desc() { return GMatDesc{-1,-1,{-1,-1}}; }
  203. namespace gapi { namespace detail {
  204. /** Checks GMatDesc fields if the passed matrix is a set of n-dimentional points.
  205. @param in GMatDesc to check.
  206. @param n expected dimensionality.
  207. @return the amount of points. In case input matrix can't be described as vector of points
  208. of expected dimensionality, returns -1.
  209. */
  210. int checkVector(const GMatDesc& in, const size_t n);
  211. /** @overload
  212. Checks GMatDesc fields if the passed matrix can be described as a set of points of any
  213. dimensionality.
  214. @return array of two elements in form of std::vector<int>: the amount of points
  215. and their calculated dimensionality. In case input matrix can't be described as vector of points,
  216. returns {-1, -1}.
  217. */
  218. std::vector<int> checkVector(const GMatDesc& in);
  219. }} // namespace gapi::detail
  220. #if !defined(GAPI_STANDALONE)
  221. GAPI_EXPORTS GMatDesc descr_of(const cv::UMat &mat);
  222. #endif // !defined(GAPI_STANDALONE)
  223. //Fwd declarations
  224. namespace gapi { namespace own {
  225. class Mat;
  226. GAPI_EXPORTS GMatDesc descr_of(const Mat &mat);
  227. }}//gapi::own
  228. GAPI_EXPORTS GMatDesc descr_of(const RMat &mat);
  229. #if !defined(GAPI_STANDALONE)
  230. GAPI_EXPORTS GMatDesc descr_of(const cv::Mat &mat);
  231. #else
  232. using gapi::own::descr_of;
  233. #endif
  234. /** @} */
  235. GAPI_EXPORTS std::ostream& operator<<(std::ostream& os, const cv::GMatDesc &desc);
  236. } // namespace cv
  237. #endif // OPENCV_GAPI_GMAT_HPP