mirror of
https://github.com/opencv/opencv.git
synced 2026-12-02 21:22:43 -06:00
Merge pull request #26254 from vpisarev:extra_tests_for_reshape
Added extra tests for reshape #26254 Attempt to reproduce problems described in #25174. No success; everything works as expected. Probably, the function has been used improperly. Slightly modified the code of Mat::reshape() to provide better diagnostic. - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
@@ -1725,27 +1725,26 @@ Mat Mat::reshape(int new_cn, int new_rows) const
|
||||
int cn = channels();
|
||||
Mat hdr = *this;
|
||||
|
||||
if( new_cn == 0 )
|
||||
new_cn = cn;
|
||||
|
||||
if( dims > 2 )
|
||||
{
|
||||
if( new_rows == 0 && new_cn != 0 && size[dims-1]*cn % new_cn == 0 )
|
||||
if( new_rows == 0 )
|
||||
{
|
||||
// special case: just change the number of channnels; retain the same shape,
|
||||
// except for the last, innermost dimension
|
||||
CV_Assert(size[dims-1]*cn % new_cn == 0);
|
||||
hdr.flags = (hdr.flags & ~CV_MAT_CN_MASK) | ((new_cn-1) << CV_CN_SHIFT);
|
||||
hdr.step[dims-1] = CV_ELEM_SIZE(hdr.flags);
|
||||
hdr.size[dims-1] = hdr.size[dims-1]*cn / new_cn;
|
||||
return hdr;
|
||||
}
|
||||
if( new_rows > 0 )
|
||||
{
|
||||
int sz[] = { new_rows, (int)(total()*cn/new_rows) };
|
||||
return reshape(new_cn, 2, sz);
|
||||
}
|
||||
CV_Assert( new_rows > 0 );
|
||||
int sz[] = { new_rows, (int)(total()*cn/new_rows) };
|
||||
return reshape(new_cn, 2, sz);
|
||||
}
|
||||
|
||||
CV_Assert( dims <= 2 );
|
||||
|
||||
if( new_cn == 0 )
|
||||
new_cn = cn;
|
||||
|
||||
int total_width = cols * cn;
|
||||
|
||||
if( (new_cn > total_width || total_width % new_cn != 0) && new_rows == 0 )
|
||||
@@ -1758,9 +1757,6 @@ Mat Mat::reshape(int new_cn, int new_rows) const
|
||||
CV_Error( cv::Error::BadStep,
|
||||
"The matrix is not continuous, thus its number of rows can not be changed" );
|
||||
|
||||
if( (unsigned)new_rows > (unsigned)total_size )
|
||||
CV_Error( cv::Error::StsOutOfRange, "Bad new number of rows" );
|
||||
|
||||
total_width = total_size / new_rows;
|
||||
|
||||
if( total_width * new_rows != total_size )
|
||||
@@ -1810,22 +1806,33 @@ Mat Mat::reshape(int _cn, int _newndims, const int* _newsz) const
|
||||
|
||||
AutoBuffer<int, 4> newsz_buf( (size_t)_newndims );
|
||||
|
||||
int m1_idx = -1;
|
||||
|
||||
for (int i = 0; i < _newndims; i++)
|
||||
{
|
||||
CV_Assert(_newsz[i] >= 0);
|
||||
|
||||
if (_newsz[i] > 0)
|
||||
newsz_buf[i] = _newsz[i];
|
||||
else if (i < dims)
|
||||
newsz_buf[i] = this->size[i];
|
||||
else
|
||||
CV_Error(cv::Error::StsOutOfRange, "Copy dimension (which has zero size) is not present in source matrix");
|
||||
|
||||
total_elem1 *= (size_t)newsz_buf[i];
|
||||
if (_newsz[i] >= 0) {
|
||||
if (_newsz[i] == 0 && i < dims)
|
||||
newsz_buf[i] = size.p[i];
|
||||
else
|
||||
newsz_buf[i] = _newsz[i];
|
||||
total_elem1 *= (size_t)newsz_buf[i];
|
||||
} else {
|
||||
if (m1_idx >= 0)
|
||||
CV_Error(cv::Error::StsBadSize, "More than one '-1' occured in the new shape");
|
||||
m1_idx = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (total_elem1 != total_elem1_ref)
|
||||
if (m1_idx >= 0) {
|
||||
if (total_elem1 == 0) {
|
||||
CV_Assert(total_elem1_ref == 0);
|
||||
total_elem1 = 1;
|
||||
}
|
||||
CV_Assert(total_elem1_ref % total_elem1 == 0);
|
||||
newsz_buf[m1_idx] = (int)(total_elem1_ref / total_elem1);
|
||||
} else if (total_elem1 != total_elem1_ref) {
|
||||
CV_Error(cv::Error::StsUnmatchedSizes, "Requested and source matrices have different count of elements");
|
||||
}
|
||||
|
||||
Mat hdr = *this;
|
||||
hdr.flags = (hdr.flags & ~CV_MAT_CN_MASK) | ((_cn-1) << CV_CN_SHIFT);
|
||||
|
||||
Reference in New Issue
Block a user