42#ifndef OPENCV_DNN_DNN_SHAPE_UTILS_HPP
43#define OPENCV_DNN_DNN_SHAPE_UTILS_HPP
45#include <opencv2/dnn/dnn.hpp>
46#include <opencv2/core/types_c.h>
53CV__DNN_INLINE_NS_BEGIN
60 _Range(
int start_,
int size_ = 1) :
cv::Range(start_, start_ + size_) {}
65 Range ranges[CV_MAX_DIM];
66 for (
int i = 1; i < m.
dims; i++)
67 ranges[i] = Range::all();
72static inline Mat slice(
const Mat &m,
const _Range &r0,
const _Range &r1)
75 Range ranges[CV_MAX_DIM];
76 for (
int i = 2; i < m.
dims; i++)
77 ranges[i] = Range::all();
83static inline Mat slice(
const Mat &m,
const _Range &r0,
const _Range &r1,
const _Range &r2)
86 Range ranges[CV_MAX_DIM];
87 for (
int i = 3; i < m.dims; i++)
88 ranges[i] = Range::all();
95static inline Mat slice(
const Mat &m,
const _Range &r0,
const _Range &r1,
const _Range &r2,
const _Range &r3)
98 Range ranges[CV_MAX_DIM];
99 for (
int i = 4; i < m.dims; i++)
100 ranges[i] = Range::all();
105 return m(&ranges[0]);
108static inline Mat getPlane(
const Mat &m,
int n,
int cn)
112 for(
int i = 2; i < m.dims; i++)
114 sz[i-2] = m.size.p[i];
116 return Mat(m.dims - 2, sz, m.type(), (
void*)m.ptr<
float>(n, cn));
119static inline MatShape shape(
const int* dims,
const int n)
122 shape.assign(dims, dims + n);
126static inline MatShape shape(
const Mat& mat)
128 return shape(mat.size.p, mat.dims);
131static inline MatShape shape(
const MatSize& sz)
133 return shape(sz.p, sz.dims());
136static inline MatShape shape(
const UMat& mat)
138 return shape(mat.size.p, mat.dims);
143MatShape shape(InputArray input)
146 int ndims = input.sizend(sz);
147 return shape(sz, ndims);
151namespace {
inline bool is_neg(
int i) {
return i < 0; }}
153static inline MatShape shape(
int a0,
int a1=-1,
int a2=-1,
int a3=-1)
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());
161static inline int total(
const MatShape& shape,
int start = -1,
int end = -1)
163 if (start == -1) start = 0;
164 if (end == -1) end = (int)shape.size();
170 CV_Assert(start <= (
int)shape.size() && end <= (
int)shape.size() &&
172 for(
int i = start; i < end; i++)
179static inline MatShape concat(
const MatShape& a,
const MatShape& b)
182 c.insert(c.end(), b.begin(), b.end());
187static inline std::string toString(
const MatShape& shape,
const String& name =
"")
189 std::ostringstream ss;
193 for(
size_t i = 0, n = shape.size(); i < n; ++i)
194 ss <<
' ' << shape[i];
198static inline void print(
const MatShape& shape,
const String& name =
"")
200 std::cout << toString(shape, name) << std::endl;
202static inline std::ostream& operator<<(std::ostream &out,
const MatShape& shape)
204 out << toString(shape);
210int normalize_axis(
int axis,
int dims)
212 CV_Check(axis, axis >= -dims && axis < dims,
"");
213 axis = (axis < 0) ? (dims + axis) : axis;
214 CV_DbgCheck(axis, axis >= 0 && axis < dims,
"");
219int normalize_axis(
int axis,
const MatShape& shape)
221 return normalize_axis(axis, (
int)shape.size());
225Range normalize_axis_range(
const Range& r,
int axisSize)
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,
"");
239bool isAllOnes(
const MatShape &inputShape,
int startPos,
int endPos)
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(),
"");
248 for (
size_t i = startPos; i < endPos; i++)
250 if (inputShape[i] != 1)
n-dimensional dense array class
Definition: mat.hpp:802
int dims
the matrix dimensionality, >= 2
Definition: mat.hpp:2105
Template class specifying a continuous subsequence (slice) of a sequence.
Definition: core/types.hpp:590
CV_EXPORTS_W void min(InputArray src1, InputArray src2, OutputArray dst)
Calculates per-element minimum of two arrays or an array and a scalar.
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails
Definition: base.hpp:342
"black box" representation of the file storage associated with a file on disk.
Definition: aruco.hpp:75
Definition: shape_utils.hpp:58