blob.inl.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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_BLOB_INL_HPP__
  42. #define __OPENCV_DNN_DNN_BLOB_INL_HPP__
  43. #include "blob.hpp"
  44. namespace cv
  45. {
  46. namespace dnn
  47. {
  48. inline BlobShape::BlobShape(int ndims, int fill) : sz( (size_t)std::max(ndims, 0) )
  49. {
  50. CV_Assert(ndims >= 0);
  51. for (int i = 0; i < ndims; i++)
  52. sz[i] = fill;
  53. }
  54. inline BlobShape::BlobShape(int ndims, const int *sizes) : sz( (size_t)std::max(ndims, 0) )
  55. {
  56. CV_Assert(ndims >= 0);
  57. for (int i = 0; i < ndims; i++)
  58. sz[i] = sizes[i];
  59. }
  60. inline BlobShape::BlobShape(int num, int cn, int rows, int cols) : sz(4)
  61. {
  62. sz[0] = num;
  63. sz[1] = cn;
  64. sz[2] = rows;
  65. sz[3] = cols;
  66. }
  67. inline BlobShape::BlobShape(const std::vector<int> &sizes) : sz( sizes.size() )
  68. {
  69. for (int i = 0; i < (int)sizes.size(); i++)
  70. sz[i] = sizes[i];
  71. }
  72. template<int n>
  73. inline BlobShape::BlobShape(const Vec<int, n> &shape) : sz(n)
  74. {
  75. for (int i = 0; i < n; i++)
  76. sz[i] = shape[i];
  77. }
  78. inline int BlobShape::dims() const
  79. {
  80. return (int)sz.size();
  81. }
  82. inline int BlobShape::xsize(int axis) const
  83. {
  84. if (axis < -dims() || axis >= dims())
  85. return 1;
  86. return sz[(axis < 0) ? axis + dims() : axis];
  87. }
  88. inline int BlobShape::size(int axis) const
  89. {
  90. CV_Assert(-dims() <= axis && axis < dims());
  91. return sz[(axis < 0) ? axis + dims() : axis];
  92. }
  93. inline int &BlobShape::size(int axis)
  94. {
  95. CV_Assert(-dims() <= axis && axis < dims());
  96. return sz[(axis < 0) ? axis + dims() : axis];
  97. }
  98. inline int BlobShape::operator[] (int axis) const
  99. {
  100. CV_Assert(-dims() <= axis && axis < dims());
  101. return sz[(axis < 0) ? axis + dims() : axis];
  102. }
  103. inline int &BlobShape::operator[] (int axis)
  104. {
  105. CV_Assert(-dims() <= axis && axis < dims());
  106. return sz[(axis < 0) ? axis + dims() : axis];
  107. }
  108. inline ptrdiff_t BlobShape::total()
  109. {
  110. if (dims() == 0)
  111. return 0;
  112. ptrdiff_t res = 1;
  113. for (int i = 0; i < dims(); i++)
  114. res *= sz[i];
  115. return res;
  116. }
  117. inline const int *BlobShape::ptr() const
  118. {
  119. return sz;
  120. }
  121. inline bool BlobShape::equal(const BlobShape &other) const
  122. {
  123. if (this->dims() != other.dims())
  124. return false;
  125. for (int i = 0; i < other.dims(); i++)
  126. {
  127. if (sz[i] != other.sz[i])
  128. return false;
  129. }
  130. return true;
  131. }
  132. inline bool BlobShape::operator==(const BlobShape &r) const
  133. {
  134. return this->equal(r);
  135. }
  136. CV_EXPORTS std::ostream &operator<< (std::ostream &stream, const BlobShape &shape);
  137. /////////////////////////////////////////////////////////////////////
  138. inline int Blob::canonicalAxis(int axis) const
  139. {
  140. CV_Assert(-dims() <= axis && axis < dims());
  141. return (axis < 0) ? axis + dims() : axis;
  142. }
  143. inline int Blob::dims() const
  144. {
  145. return m.dims;
  146. }
  147. inline int Blob::xsize(int axis) const
  148. {
  149. if (axis < -dims() || axis >= dims())
  150. return 1;
  151. return sizes()[(axis < 0) ? axis + dims() : axis];
  152. }
  153. inline int Blob::size(int axis) const
  154. {
  155. CV_Assert(-dims() <= axis && axis < dims());
  156. return sizes()[(axis < 0) ? axis + dims() : axis];
  157. }
  158. inline size_t Blob::total(int startAxis, int endAxis) const
  159. {
  160. if (startAxis < 0)
  161. startAxis += dims();
  162. if (endAxis == INT_MAX)
  163. endAxis = dims();
  164. else if (endAxis < 0)
  165. endAxis += dims();
  166. CV_Assert(0 <= startAxis && startAxis <= endAxis && endAxis <= dims());
  167. size_t size = 1; //fix: assume that slice isn't empty
  168. for (int i = startAxis; i < endAxis; i++)
  169. size *= (size_t)sizes()[i];
  170. return size;
  171. }
  172. template<int n>
  173. inline size_t Blob::offset(const Vec<int, n> &pos) const
  174. {
  175. size_t ofs = 0;
  176. int i;
  177. for (i = 0; i < std::min(n, dims()); i++)
  178. {
  179. CV_DbgAssert(pos[i] >= 0 && pos[i] < size(i));
  180. ofs = ofs * (size_t)size(i) + pos[i];
  181. }
  182. for (; i < dims(); i++)
  183. ofs *= (size_t)size(i);
  184. return ofs;
  185. }
  186. inline size_t Blob::offset(int n, int cn, int row, int col) const
  187. {
  188. return offset(Vec4i(n, cn, row, col));
  189. }
  190. inline float *Blob::ptrf(int n, int cn, int row, int col)
  191. {
  192. CV_Assert(type() == CV_32F);
  193. return (float*)m.data + offset(n, cn, row, col);
  194. }
  195. inline uchar *Blob::ptr(int n, int cn, int row, int col)
  196. {
  197. return m.data + m.elemSize() * offset(n, cn, row, col);
  198. }
  199. template<typename TFloat>
  200. inline TFloat* Blob::ptr(int n, int cn, int row, int col)
  201. {
  202. CV_Assert(type() == cv::DataDepth<TFloat>::value);
  203. return (TFloat*) ptr(n, cn, row, col);
  204. }
  205. inline BlobShape Blob::shape() const
  206. {
  207. return BlobShape(dims(), sizes());
  208. }
  209. inline bool Blob::equalShape(const Blob &other) const
  210. {
  211. if (this->dims() != other.dims())
  212. return false;
  213. for (int i = 0; i < dims(); i++)
  214. {
  215. if (this->sizes()[i] != other.sizes()[i])
  216. return false;
  217. }
  218. return true;
  219. }
  220. inline Mat& Blob::matRef()
  221. {
  222. return m;
  223. }
  224. inline const Mat& Blob::matRefConst() const
  225. {
  226. return m;
  227. }
  228. inline UMat &Blob::umatRef()
  229. {
  230. CV_Error(Error::StsNotImplemented, "");
  231. return *(new UMat());
  232. }
  233. inline const UMat &Blob::umatRefConst() const
  234. {
  235. CV_Error(Error::StsNotImplemented, "");
  236. return *(new UMat());
  237. }
  238. inline Mat Blob::getPlane(int n, int cn)
  239. {
  240. CV_Assert(dims() > 2);
  241. return Mat(dims() - 2, sizes() + 2, type(), ptr(n, cn));
  242. }
  243. inline int Blob::cols() const
  244. {
  245. return xsize(3);
  246. }
  247. inline int Blob::rows() const
  248. {
  249. return xsize(2);
  250. }
  251. inline int Blob::channels() const
  252. {
  253. return xsize(1);
  254. }
  255. inline int Blob::num() const
  256. {
  257. return xsize(0);
  258. }
  259. inline Size Blob::size2() const
  260. {
  261. return Size(cols(), rows());
  262. }
  263. inline int Blob::type() const
  264. {
  265. return m.depth();
  266. }
  267. inline const int * Blob::sizes() const
  268. {
  269. return &m.size[0];
  270. }
  271. inline Blob &Blob::shareFrom(const Blob &blob)
  272. {
  273. this->m = blob.m;
  274. return *this;
  275. }
  276. inline Blob &Blob::reshape(const BlobShape &shape)
  277. {
  278. m = m.reshape(1, shape.dims(), shape.ptr());
  279. return *this;
  280. }
  281. }
  282. }
  283. #endif