shape_utils.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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/core.hpp>
  44. #include <opencv2/core/types_c.h>
  45. #include <ostream>
  46. namespace cv {
  47. namespace dnn {
  48. CV__DNN_EXPERIMENTAL_NS_BEGIN
  49. //Useful shortcut
  50. inline std::ostream &operator<< (std::ostream &s, cv::Range &r)
  51. {
  52. return s << "[" << r.start << ", " << r.end << ")";
  53. }
  54. //Slicing
  55. struct _Range : public cv::Range
  56. {
  57. _Range(const Range &r) : cv::Range(r) {}
  58. _Range(int start_, int size_ = 1) : cv::Range(start_, start_ + size_) {}
  59. };
  60. static inline Mat slice(const Mat &m, const _Range &r0)
  61. {
  62. Range ranges[CV_MAX_DIM];
  63. for (int i = 1; i < m.dims; i++)
  64. ranges[i] = Range::all();
  65. ranges[0] = r0;
  66. return m(&ranges[0]);
  67. }
  68. static inline Mat slice(const Mat &m, const _Range &r0, const _Range &r1)
  69. {
  70. CV_Assert(m.dims >= 2);
  71. Range ranges[CV_MAX_DIM];
  72. for (int i = 2; i < m.dims; i++)
  73. ranges[i] = Range::all();
  74. ranges[0] = r0;
  75. ranges[1] = r1;
  76. return m(&ranges[0]);
  77. }
  78. static inline Mat slice(const Mat &m, const _Range &r0, const _Range &r1, const _Range &r2)
  79. {
  80. CV_Assert(m.dims >= 3);
  81. Range ranges[CV_MAX_DIM];
  82. for (int i = 3; i < m.dims; i++)
  83. ranges[i] = Range::all();
  84. ranges[0] = r0;
  85. ranges[1] = r1;
  86. ranges[2] = r2;
  87. return m(&ranges[0]);
  88. }
  89. static inline Mat slice(const Mat &m, const _Range &r0, const _Range &r1, const _Range &r2, const _Range &r3)
  90. {
  91. CV_Assert(m.dims >= 4);
  92. Range ranges[CV_MAX_DIM];
  93. for (int i = 4; i < m.dims; i++)
  94. ranges[i] = Range::all();
  95. ranges[0] = r0;
  96. ranges[1] = r1;
  97. ranges[2] = r2;
  98. ranges[3] = r3;
  99. return m(&ranges[0]);
  100. }
  101. static inline Mat getPlane(const Mat &m, int n, int cn)
  102. {
  103. CV_Assert(m.dims > 2);
  104. Range range[CV_MAX_DIM];
  105. int sz[CV_MAX_DIM];
  106. for(int i = 2; i < m.dims; i++)
  107. {
  108. sz[i-2] = m.size.p[i];
  109. range[i] = Range::all();
  110. }
  111. range[0] = Range(n, n+1);
  112. range[1] = Range(cn, cn+1);
  113. return m(range).reshape(1, m.dims-2, sz);
  114. }
  115. static inline MatShape shape(const int* dims, const int n = 4)
  116. {
  117. MatShape shape;
  118. shape.assign(dims, dims + n);
  119. return shape;
  120. }
  121. static inline MatShape shape(const Mat& mat)
  122. {
  123. return shape(mat.size.p, mat.dims);
  124. }
  125. namespace {inline bool is_neg(int i) { return i < 0; }}
  126. static inline MatShape shape(int a0, int a1=-1, int a2=-1, int a3=-1)
  127. {
  128. int dims[] = {a0, a1, a2, a3};
  129. MatShape s = shape(dims);
  130. s.erase(std::remove_if(s.begin(), s.end(), is_neg), s.end());
  131. return s;
  132. }
  133. static inline int total(const MatShape& shape, int start = -1, int end = -1)
  134. {
  135. if (start == -1) start = 0;
  136. if (end == -1) end = (int)shape.size();
  137. if (shape.empty())
  138. return 0;
  139. int elems = 1;
  140. CV_Assert(start < (int)shape.size() && end <= (int)shape.size() &&
  141. start <= end);
  142. for(int i = start; i < end; i++)
  143. {
  144. elems *= shape[i];
  145. }
  146. return elems;
  147. }
  148. static inline MatShape concat(const MatShape& a, const MatShape& b)
  149. {
  150. MatShape c = a;
  151. c.insert(c.end(), b.begin(), b.end());
  152. return c;
  153. }
  154. inline void print(const MatShape& shape, const String& name = "")
  155. {
  156. printf("%s: [", name.c_str());
  157. size_t i, n = shape.size();
  158. for( i = 0; i < n; i++ )
  159. printf(" %d", shape[i]);
  160. printf(" ]\n");
  161. }
  162. inline int clamp(int ax, int dims)
  163. {
  164. return ax < 0 ? ax + dims : ax;
  165. }
  166. inline int clamp(int ax, const MatShape& shape)
  167. {
  168. return clamp(ax, (int)shape.size());
  169. }
  170. CV__DNN_EXPERIMENTAL_NS_END
  171. }
  172. }
  173. #endif