shape_utils.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 (start == -1) start = 0;
  146. if (end == -1) end = (int)shape.size();
  147. if (shape.empty())
  148. return 0;
  149. int elems = 1;
  150. CV_Assert(start <= (int)shape.size() && end <= (int)shape.size() &&
  151. start <= end);
  152. for(int i = start; i < end; i++)
  153. {
  154. elems *= shape[i];
  155. }
  156. return elems;
  157. }
  158. static inline MatShape concat(const MatShape& a, const MatShape& b)
  159. {
  160. MatShape c = a;
  161. c.insert(c.end(), b.begin(), b.end());
  162. return c;
  163. }
  164. template<typename _Tp>
  165. static inline std::string toString(const std::vector<_Tp>& shape, const String& name = "")
  166. {
  167. std::ostringstream ss;
  168. if (!name.empty())
  169. ss << name << ' ';
  170. ss << '[';
  171. for(size_t i = 0, n = shape.size(); i < n; ++i)
  172. ss << ' ' << shape[i];
  173. ss << " ]";
  174. return ss.str();
  175. }
  176. template<typename _Tp>
  177. static inline void print(const std::vector<_Tp>& shape, const String& name = "")
  178. {
  179. std::cout << toString(shape, name) << std::endl;
  180. }
  181. template<typename _Tp>
  182. static inline std::ostream& operator<<(std::ostream &out, const std::vector<_Tp>& shape)
  183. {
  184. out << toString(shape);
  185. return out;
  186. }
  187. /// @brief Converts axis from `[-dims; dims)` (similar to Python's slice notation) to `[0; dims)` range.
  188. static inline
  189. int normalize_axis(int axis, int dims)
  190. {
  191. CV_Check(axis, axis >= -dims && axis < dims, "");
  192. axis = (axis < 0) ? (dims + axis) : axis;
  193. CV_DbgCheck(axis, axis >= 0 && axis < dims, "");
  194. return axis;
  195. }
  196. static inline
  197. int normalize_axis(int axis, const MatShape& shape)
  198. {
  199. return normalize_axis(axis, (int)shape.size());
  200. }
  201. static inline
  202. Range normalize_axis_range(const Range& r, int axisSize)
  203. {
  204. if (r == Range::all())
  205. return Range(0, axisSize);
  206. CV_CheckGE(r.start, 0, "");
  207. Range clamped(r.start,
  208. r.end > 0 ? std::min(r.end, axisSize) : axisSize + r.end + 1);
  209. CV_DbgCheckGE(clamped.start, 0, "");
  210. CV_CheckLT(clamped.start, clamped.end, "");
  211. CV_CheckLE(clamped.end, axisSize, "");
  212. return clamped;
  213. }
  214. static inline
  215. bool isAllOnes(const MatShape &inputShape, int startPos, int endPos)
  216. {
  217. CV_Assert(!inputShape.empty());
  218. CV_CheckGE((int) inputShape.size(), startPos, "");
  219. CV_CheckGE(startPos, 0, "");
  220. CV_CheckLE(startPos, endPos, "");
  221. CV_CheckLE((size_t)endPos, inputShape.size(), "");
  222. for (size_t i = startPos; i < endPos; i++)
  223. {
  224. if (inputShape[i] != 1)
  225. return false;
  226. }
  227. return true;
  228. }
  229. CV__DNN_INLINE_NS_END
  230. }
  231. }
  232. #endif