shape_utils.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #ifndef OPENCV_DNN_DNN_SHAPE_UTILS_HPP
  42. #define OPENCV_DNN_DNN_SHAPE_UTILS_HPP
  43. #include <opencv2/dnn/dnn.hpp>
  44. #include <opencv2/core/types_c.h> // CV_MAX_DIM
  45. #include <iostream>
  46. #include <ostream>
  47. #include <sstream>
  48. namespace cv {
  49. namespace dnn {
  50. CV__DNN_INLINE_NS_BEGIN
  51. //Slicing
  52. struct _Range : public cv::Range
  53. {
  54. _Range(const Range &r) : cv::Range(r) {}
  55. _Range(int start_, int size_ = 1) : cv::Range(start_, start_ + size_) {}
  56. };
  57. static inline Mat slice(const Mat &m, const _Range &r0)
  58. {
  59. Range ranges[CV_MAX_DIM];
  60. for (int i = 1; i < m.dims; i++)
  61. ranges[i] = Range::all();
  62. ranges[0] = r0;
  63. return m(&ranges[0]);
  64. }
  65. static inline Mat slice(const Mat &m, const _Range &r0, const _Range &r1)
  66. {
  67. CV_Assert(m.dims >= 2);
  68. Range ranges[CV_MAX_DIM];
  69. for (int i = 2; i < m.dims; i++)
  70. ranges[i] = Range::all();
  71. ranges[0] = r0;
  72. ranges[1] = r1;
  73. return m(&ranges[0]);
  74. }
  75. static inline Mat slice(const Mat &m, const _Range &r0, const _Range &r1, const _Range &r2)
  76. {
  77. CV_Assert(m.dims >= 3);
  78. Range ranges[CV_MAX_DIM];
  79. for (int i = 3; i < m.dims; i++)
  80. ranges[i] = Range::all();
  81. ranges[0] = r0;
  82. ranges[1] = r1;
  83. ranges[2] = r2;
  84. return m(&ranges[0]);
  85. }
  86. static inline Mat slice(const Mat &m, const _Range &r0, const _Range &r1, const _Range &r2, const _Range &r3)
  87. {
  88. CV_Assert(m.dims >= 4);
  89. Range ranges[CV_MAX_DIM];
  90. for (int i = 4; i < m.dims; i++)
  91. ranges[i] = Range::all();
  92. ranges[0] = r0;
  93. ranges[1] = r1;
  94. ranges[2] = r2;
  95. ranges[3] = r3;
  96. return m(&ranges[0]);
  97. }
  98. static inline Mat getPlane(const Mat &m, int n, int cn)
  99. {
  100. CV_Assert(m.dims > 2);
  101. int sz[CV_MAX_DIM];
  102. for(int i = 2; i < m.dims; i++)
  103. {
  104. sz[i-2] = m.size.p[i];
  105. }
  106. return Mat(m.dims - 2, sz, m.type(), (void*)m.ptr<float>(n, cn));
  107. }
  108. static inline MatShape shape(const int* dims, const int n)
  109. {
  110. MatShape shape;
  111. shape.assign(dims, dims + n);
  112. return shape;
  113. }
  114. static inline MatShape shape(const Mat& mat)
  115. {
  116. return shape(mat.size.p, mat.dims);
  117. }
  118. static inline MatShape shape(const MatSize& sz)
  119. {
  120. return shape(sz.p, sz.dims());
  121. }
  122. static inline MatShape shape(const UMat& mat)
  123. {
  124. return shape(mat.size.p, mat.dims);
  125. }
  126. #if 0 // issues with MatExpr wrapped into InputArray
  127. static inline
  128. MatShape shape(InputArray input)
  129. {
  130. int sz[CV_MAX_DIM];
  131. int ndims = input.sizend(sz);
  132. return shape(sz, ndims);
  133. }
  134. #endif
  135. namespace {inline bool is_neg(int i) { return i < 0; }}
  136. static inline MatShape shape(int a0, int a1=-1, int a2=-1, int a3=-1)
  137. {
  138. int dims[] = {a0, a1, a2, a3};
  139. MatShape s = shape(dims, 4);
  140. s.erase(std::remove_if(s.begin(), s.end(), is_neg), s.end());
  141. return s;
  142. }
  143. static inline int total(const MatShape& shape, int start = -1, int end = -1)
  144. {
  145. if (shape.empty())
  146. return 0;
  147. int dims = (int)shape.size();
  148. if (start == -1) start = 0;
  149. if (end == -1) end = dims;
  150. CV_CheckLE(0, start, "");
  151. CV_CheckLE(start, end, "");
  152. CV_CheckLE(end, dims, "");
  153. int elems = 1;
  154. for (int i = start; i < end; i++)
  155. {
  156. elems *= shape[i];
  157. }
  158. return elems;
  159. }
  160. // TODO: rename to countDimsElements()
  161. static inline int total(const Mat& mat, int start = -1, int end = -1)
  162. {
  163. if (mat.empty())
  164. return 0;
  165. int dims = mat.dims;
  166. if (start == -1) start = 0;
  167. if (end == -1) end = dims;
  168. CV_CheckLE(0, start, "");
  169. CV_CheckLE(start, end, "");
  170. CV_CheckLE(end, dims, "");
  171. int elems = 1;
  172. for (int i = start; i < end; i++)
  173. {
  174. elems *= mat.size[i];
  175. }
  176. return elems;
  177. }
  178. static inline MatShape concat(const MatShape& a, const MatShape& b)
  179. {
  180. MatShape c = a;
  181. c.insert(c.end(), b.begin(), b.end());
  182. return c;
  183. }
  184. template<typename _Tp>
  185. static inline std::string toString(const std::vector<_Tp>& shape, const String& name = "")
  186. {
  187. std::ostringstream ss;
  188. if (!name.empty())
  189. ss << name << ' ';
  190. ss << '[';
  191. for(size_t i = 0, n = shape.size(); i < n; ++i)
  192. ss << ' ' << shape[i];
  193. ss << " ]";
  194. return ss.str();
  195. }
  196. template<typename _Tp>
  197. static inline void print(const std::vector<_Tp>& shape, const String& name = "")
  198. {
  199. std::cout << toString(shape, name) << std::endl;
  200. }
  201. template<typename _Tp>
  202. static inline std::ostream& operator<<(std::ostream &out, const std::vector<_Tp>& shape)
  203. {
  204. out << toString(shape);
  205. return out;
  206. }
  207. /// @brief Converts axis from `[-dims; dims)` (similar to Python's slice notation) to `[0; dims)` range.
  208. static inline
  209. int normalize_axis(int axis, int dims)
  210. {
  211. CV_Check(axis, axis >= -dims && axis < dims, "");
  212. axis = (axis < 0) ? (dims + axis) : axis;
  213. CV_DbgCheck(axis, axis >= 0 && axis < dims, "");
  214. return axis;
  215. }
  216. static inline
  217. int normalize_axis(int axis, const MatShape& shape)
  218. {
  219. return normalize_axis(axis, (int)shape.size());
  220. }
  221. static inline
  222. Range normalize_axis_range(const Range& r, int axisSize)
  223. {
  224. if (r == Range::all())
  225. return Range(0, axisSize);
  226. CV_CheckGE(r.start, 0, "");
  227. Range clamped(r.start,
  228. r.end > 0 ? std::min(r.end, axisSize) : axisSize + r.end + 1);
  229. CV_DbgCheckGE(clamped.start, 0, "");
  230. CV_CheckLT(clamped.start, clamped.end, "");
  231. CV_CheckLE(clamped.end, axisSize, "");
  232. return clamped;
  233. }
  234. static inline
  235. bool isAllOnes(const MatShape &inputShape, int startPos, int endPos)
  236. {
  237. CV_Assert(!inputShape.empty());
  238. CV_CheckGE((int) inputShape.size(), startPos, "");
  239. CV_CheckGE(startPos, 0, "");
  240. CV_CheckLE(startPos, endPos, "");
  241. CV_CheckLE((size_t)endPos, inputShape.size(), "");
  242. for (size_t i = startPos; i < endPos; i++)
  243. {
  244. if (inputShape[i] != 1)
  245. return false;
  246. }
  247. return true;
  248. }
  249. CV__DNN_INLINE_NS_END
  250. }
  251. }
  252. #endif