Skip to content

Commit 96aaac1

Browse files
committed
Merge pull request opencv#8616 from vpisarev:dnn4
2 parents fe21487 + dd54f7a commit 96aaac1

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

modules/core/include/opencv2/core/cvstd.inl.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,17 @@ std::ostream& operator << (std::ostream& out, const Rect_<_Tp>& rect)
270270
return out << "[" << rect.width << " x " << rect.height << " from (" << rect.x << ", " << rect.y << ")]";
271271
}
272272

273+
static inline std::ostream& operator << (std::ostream& out, const MatSize& msize)
274+
{
275+
int i, dims = msize.p[-1];
276+
for( i = 0; i < dims; i++ )
277+
{
278+
out << msize.p[i];
279+
if( i < dims-1 )
280+
out << " x ";
281+
}
282+
return out;
283+
}
273284

274285
#endif // OPENCV_NOSTL
275286
} // cv

modules/core/include/opencv2/core/mat.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,9 @@ class CV_EXPORTS Mat
12431243
/** @overload */
12441244
Mat reshape(int cn, int newndims, const int* newsz) const;
12451245

1246+
/** @overload */
1247+
Mat reshape(int cn, const std::vector<int>& newshape) const;
1248+
12461249
/** @brief Transposes a matrix.
12471250
12481251
The method performs matrix transposition by means of matrix expressions. It does not perform the
@@ -1749,6 +1752,12 @@ class CV_EXPORTS Mat
17491752
*/
17501753
size_t total() const;
17511754

1755+
/** @brief Returns the total number of array elements.
1756+
1757+
The method returns the number of elements within a certain sub-array slice with startDim <= dim < endDim
1758+
*/
1759+
size_t total(int startDim, int endDim=INT_MAX) const;
1760+
17521761
//! returns N if the matrix is 1-channel (N x ptdim) or ptdim-channel (1 x N) or (N x 1); negative number otherwise
17531762
int checkVector(int elemChannels, int depth=-1, bool requireContinuous=true) const;
17541763

modules/core/include/opencv2/core/mat.inl.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,17 @@ size_t Mat::total() const
878878
return p;
879879
}
880880

881+
inline
882+
size_t Mat::total(int startDim, int endDim) const
883+
{
884+
CV_Assert( 0 <= startDim && startDim <= endDim);
885+
size_t p = 1;
886+
int endDim_ = endDim <= dims ? endDim : dims;
887+
for( int i = startDim; i < endDim_; i++ )
888+
p *= size[i];
889+
return p;
890+
}
891+
881892
inline
882893
uchar* Mat::ptr(int y)
883894
{

modules/core/src/matrix.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,12 +1052,20 @@ Mat Mat::reshape(int new_cn, int new_rows) const
10521052
int cn = channels();
10531053
Mat hdr = *this;
10541054

1055-
if( dims > 2 && new_rows == 0 && new_cn != 0 && size[dims-1]*cn % new_cn == 0 )
1055+
if( dims > 2 )
10561056
{
1057-
hdr.flags = (hdr.flags & ~CV_MAT_CN_MASK) | ((new_cn-1) << CV_CN_SHIFT);
1058-
hdr.step[dims-1] = CV_ELEM_SIZE(hdr.flags);
1059-
hdr.size[dims-1] = hdr.size[dims-1]*cn / new_cn;
1060-
return hdr;
1057+
if( new_rows == 0 && new_cn != 0 && size[dims-1]*cn % new_cn == 0 )
1058+
{
1059+
hdr.flags = (hdr.flags & ~CV_MAT_CN_MASK) | ((new_cn-1) << CV_CN_SHIFT);
1060+
hdr.step[dims-1] = CV_ELEM_SIZE(hdr.flags);
1061+
hdr.size[dims-1] = hdr.size[dims-1]*cn / new_cn;
1062+
return hdr;
1063+
}
1064+
if( new_rows > 0 )
1065+
{
1066+
int sz[] = { new_rows, (int)(total()/new_rows) };
1067+
return reshape(new_cn, 2, sz);
1068+
}
10611069
}
10621070

10631071
CV_Assert( dims <= 2 );
@@ -4706,6 +4714,18 @@ Mat Mat::reshape(int _cn, int _newndims, const int* _newsz) const
47064714
return Mat();
47074715
}
47084716

4717+
Mat Mat::reshape(int _cn, const std::vector<int>& _newshape) const
4718+
{
4719+
if(_newshape.empty())
4720+
{
4721+
CV_Assert(empty());
4722+
return *this;
4723+
}
4724+
4725+
return reshape(_cn, (int)_newshape.size(), &_newshape[0]);
4726+
}
4727+
4728+
47094729
NAryMatIterator::NAryMatIterator()
47104730
: arrays(0), planes(0), ptrs(0), narrays(0), nplanes(0), size(0), iterdepth(0), idx(0)
47114731
{

0 commit comments

Comments
 (0)