github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/opencv4/include/opencv2/dnn/shape_utils.hpp (about)

     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  
    42  #ifndef OPENCV_DNN_DNN_SHAPE_UTILS_HPP
    43  #define OPENCV_DNN_DNN_SHAPE_UTILS_HPP
    44  
    45  #include <opencv2/dnn/dnn.hpp>
    46  #include <opencv2/core/types_c.h>  // CV_MAX_DIM
    47  #include <iostream>
    48  #include <ostream>
    49  #include <sstream>
    50  
    51  namespace cv {
    52  namespace dnn {
    53  CV__DNN_INLINE_NS_BEGIN
    54  
    55  //Slicing
    56  
    57  struct _Range : public cv::Range
    58  {
    59      _Range(const Range &r) : cv::Range(r) {}
    60      _Range(int start_, int size_ = 1) : cv::Range(start_, start_ + size_) {}
    61  };
    62  
    63  static inline Mat slice(const Mat &m, const _Range &r0)
    64  {
    65      Range ranges[CV_MAX_DIM];
    66      for (int i = 1; i < m.dims; i++)
    67          ranges[i] = Range::all();
    68      ranges[0] = r0;
    69      return m(&ranges[0]);
    70  }
    71  
    72  static inline Mat slice(const Mat &m, const _Range &r0, const _Range &r1)
    73  {
    74      CV_Assert(m.dims >= 2);
    75      Range ranges[CV_MAX_DIM];
    76      for (int i = 2; i < m.dims; i++)
    77          ranges[i] = Range::all();
    78      ranges[0] = r0;
    79      ranges[1] = r1;
    80      return m(&ranges[0]);
    81  }
    82  
    83  static inline Mat slice(const Mat &m, const _Range &r0, const _Range &r1, const _Range &r2)
    84  {
    85      CV_Assert(m.dims >= 3);
    86      Range ranges[CV_MAX_DIM];
    87      for (int i = 3; i < m.dims; i++)
    88          ranges[i] = Range::all();
    89      ranges[0] = r0;
    90      ranges[1] = r1;
    91      ranges[2] = r2;
    92      return m(&ranges[0]);
    93  }
    94  
    95  static inline Mat slice(const Mat &m, const _Range &r0, const _Range &r1, const _Range &r2, const _Range &r3)
    96  {
    97      CV_Assert(m.dims >= 4);
    98      Range ranges[CV_MAX_DIM];
    99      for (int i = 4; i < m.dims; i++)
   100          ranges[i] = Range::all();
   101      ranges[0] = r0;
   102      ranges[1] = r1;
   103      ranges[2] = r2;
   104      ranges[3] = r3;
   105      return m(&ranges[0]);
   106  }
   107  
   108  static inline Mat getPlane(const Mat &m, int n, int cn)
   109  {
   110      CV_Assert(m.dims > 2);
   111      int sz[CV_MAX_DIM];
   112      for(int i = 2; i < m.dims; i++)
   113      {
   114          sz[i-2] = m.size.p[i];
   115      }
   116      return Mat(m.dims - 2, sz, m.type(), (void*)m.ptr<float>(n, cn));
   117  }
   118  
   119  static inline MatShape shape(const int* dims, const int n)
   120  {
   121      MatShape shape;
   122      shape.assign(dims, dims + n);
   123      return shape;
   124  }
   125  
   126  static inline MatShape shape(const Mat& mat)
   127  {
   128      return shape(mat.size.p, mat.dims);
   129  }
   130  
   131  static inline MatShape shape(const MatSize& sz)
   132  {
   133      return shape(sz.p, sz.dims());
   134  }
   135  
   136  static inline MatShape shape(const UMat& mat)
   137  {
   138      return shape(mat.size.p, mat.dims);
   139  }
   140  
   141  #if 0  // issues with MatExpr wrapped into InputArray
   142  static inline
   143  MatShape shape(InputArray input)
   144  {
   145      int sz[CV_MAX_DIM];
   146      int ndims = input.sizend(sz);
   147      return shape(sz, ndims);
   148  }
   149  #endif
   150  
   151  namespace {inline bool is_neg(int i) { return i < 0; }}
   152  
   153  static inline MatShape shape(int a0, int a1=-1, int a2=-1, int a3=-1)
   154  {
   155      int dims[] = {a0, a1, a2, a3};
   156      MatShape s = shape(dims, 4);
   157      s.erase(std::remove_if(s.begin(), s.end(), is_neg), s.end());
   158      return s;
   159  }
   160  
   161  static inline int total(const MatShape& shape, int start = -1, int end = -1)
   162  {
   163      if (start == -1) start = 0;
   164      if (end == -1) end = (int)shape.size();
   165  
   166      if (shape.empty())
   167          return 0;
   168  
   169      int elems = 1;
   170      CV_Assert(start <= (int)shape.size() && end <= (int)shape.size() &&
   171                start <= end);
   172      for(int i = start; i < end; i++)
   173      {
   174          elems *= shape[i];
   175      }
   176      return elems;
   177  }
   178  
   179  static inline MatShape concat(const MatShape& a, const MatShape& b)
   180  {
   181      MatShape c = a;
   182      c.insert(c.end(), b.begin(), b.end());
   183  
   184      return c;
   185  }
   186  
   187  static inline std::string toString(const MatShape& shape, const String& name = "")
   188  {
   189      std::ostringstream ss;
   190      if (!name.empty())
   191          ss << name << ' ';
   192      ss << '[';
   193      for(size_t i = 0, n = shape.size(); i < n; ++i)
   194          ss << ' ' << shape[i];
   195      ss << " ]";
   196      return ss.str();
   197  }
   198  static inline void print(const MatShape& shape, const String& name = "")
   199  {
   200      std::cout << toString(shape, name) << std::endl;
   201  }
   202  static inline std::ostream& operator<<(std::ostream &out, const MatShape& shape)
   203  {
   204      out << toString(shape);
   205      return out;
   206  }
   207  
   208  /// @brief Converts axis from `[-dims; dims)` (similar to Python's slice notation) to `[0; dims)` range.
   209  static inline
   210  int normalize_axis(int axis, int dims)
   211  {
   212      CV_Check(axis, axis >= -dims && axis < dims, "");
   213      axis = (axis < 0) ? (dims + axis) : axis;
   214      CV_DbgCheck(axis, axis >= 0 && axis < dims, "");
   215      return axis;
   216  }
   217  
   218  static inline
   219  int normalize_axis(int axis, const MatShape& shape)
   220  {
   221      return normalize_axis(axis, (int)shape.size());
   222  }
   223  
   224  static inline
   225  Range normalize_axis_range(const Range& r, int axisSize)
   226  {
   227      if (r == Range::all())
   228          return Range(0, axisSize);
   229      CV_CheckGE(r.start, 0, "");
   230      Range clamped(r.start,
   231                    r.end > 0 ? std::min(r.end, axisSize) : axisSize + r.end + 1);
   232      CV_DbgCheckGE(clamped.start, 0, "");
   233      CV_CheckLT(clamped.start, clamped.end, "");
   234      CV_CheckLE(clamped.end, axisSize, "");
   235      return clamped;
   236  }
   237  
   238  static inline
   239  bool isAllOnes(const MatShape &inputShape, int startPos, int endPos)
   240  {
   241      CV_Assert(!inputShape.empty());
   242  
   243      CV_CheckGE((int) inputShape.size(), startPos, "");
   244      CV_CheckGE(startPos, 0, "");
   245      CV_CheckLE(startPos, endPos, "");
   246      CV_CheckLE((size_t)endPos, inputShape.size(), "");
   247  
   248      for (size_t i = startPos; i < endPos; i++)
   249      {
   250          if (inputShape[i] != 1)
   251              return false;
   252      }
   253      return true;
   254  }
   255  
   256  CV__DNN_INLINE_NS_END
   257  }
   258  }
   259  #endif