shape_utils.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. namespace {inline bool is_neg(int i) { return i < 0; }}
  127. static inline MatShape shape(int a0, int a1=-1, int a2=-1, int a3=-1)
  128. {
  129. int dims[] = {a0, a1, a2, a3};
  130. MatShape s = shape(dims, 4);
  131. s.erase(std::remove_if(s.begin(), s.end(), is_neg), s.end());
  132. return s;
  133. }
  134. static inline int total(const MatShape& shape, int start = -1, int end = -1)
  135. {
  136. if (start == -1) start = 0;
  137. if (end == -1) end = (int)shape.size();
  138. if (shape.empty())
  139. return 0;
  140. int elems = 1;
  141. CV_Assert(start <= (int)shape.size() && end <= (int)shape.size() &&
  142. start <= end);
  143. for(int i = start; i < end; i++)
  144. {
  145. elems *= shape[i];
  146. }
  147. return elems;
  148. }
  149. static inline MatShape concat(const MatShape& a, const MatShape& b)
  150. {
  151. MatShape c = a;
  152. c.insert(c.end(), b.begin(), b.end());
  153. return c;
  154. }
  155. static inline std::string toString(const MatShape& shape, const String& name = "")
  156. {
  157. std::ostringstream ss;
  158. if (!name.empty())
  159. ss << name << ' ';
  160. ss << '[';
  161. for(size_t i = 0, n = shape.size(); i < n; ++i)
  162. ss << ' ' << shape[i];
  163. ss << " ]";
  164. return ss.str();
  165. }
  166. static inline void print(const MatShape& shape, const String& name = "")
  167. {
  168. std::cout << toString(shape, name) << std::endl;
  169. }
  170. static inline std::ostream& operator<<(std::ostream &out, const MatShape& shape)
  171. {
  172. out << toString(shape);
  173. return out;
  174. }
  175. inline int clamp(int ax, int dims)
  176. {
  177. return ax < 0 ? ax + dims : ax;
  178. }
  179. inline int clamp(int ax, const MatShape& shape)
  180. {
  181. return clamp(ax, (int)shape.size());
  182. }
  183. inline Range clamp(const Range& r, int axisSize)
  184. {
  185. Range clamped(std::max(r.start, 0),
  186. r.end > 0 ? std::min(r.end, axisSize) : axisSize + r.end + 1);
  187. CV_Assert_N(clamped.start < clamped.end, clamped.end <= axisSize);
  188. return clamped;
  189. }
  190. CV__DNN_INLINE_NS_END
  191. }
  192. }
  193. #endif