diff --git a/modules/core/src/arithm.cpp b/modules/core/src/arithm.cpp index 604b13101d..fbc4959253 100644 --- a/modules/core/src/arithm.cpp +++ b/modules/core/src/arithm.cpp @@ -168,7 +168,7 @@ static void binary_op( InputArray _src1, InputArray _src2, OutputArray _dst, if( dims1 <= 2 && dims2 <= 2 && kind1 == kind2 && sz1 == sz2 && type1 == type2 && !haveMask ) { - _dst.create(sz1, type1); + _dst.createSameSize(*psrc1, type1); CV_OCL_RUN(use_opencl, ocl_binary_op(*psrc1, *psrc2, _dst, _mask, bitwise, oclop, false)) @@ -1225,7 +1225,7 @@ void cv::compare(InputArray _src1, InputArray _src2, OutputArray _dst, int op) if( kind1 == kind2 && src1.dims <= 2 && src2.dims <= 2 && src1.size() == src2.size() && src1.type() == src2.type() ) { int cn = src1.channels(); - _dst.create(src1.size(), CV_8UC(cn)); + _dst.createSameSize(src1, CV_8UC(cn)); Mat dst = _dst.getMat(); Size sz = getContinuousSize2D(src1, src2, dst, src1.channels()); BinaryFuncC cmpFn = getCmpFunc(depth1); diff --git a/modules/core/src/minmax.cpp b/modules/core/src/minmax.cpp index 96a7d027ad..d1ef797cda 100644 --- a/modules/core/src/minmax.cpp +++ b/modules/core/src/minmax.cpp @@ -1565,13 +1565,24 @@ void cv::minMaxLoc( InputArray _img, double* minVal, double* maxVal, { CV_INSTRUMENT_REGION(); - CV_Assert(_img.dims() <= 2); + int dims = _img.dims(); + CV_CheckLE(dims, 2, ""); minMaxIdx(_img, minVal, maxVal, (int*)minLoc, (int*)maxLoc, mask); if( minLoc ) - std::swap(minLoc->x, minLoc->y); + { + if (dims == 2) + std::swap(minLoc->x, minLoc->y); + else + minLoc->y = 0; + } if( maxLoc ) - std::swap(maxLoc->x, maxLoc->y); + { + if (dims == 2) + std::swap(maxLoc->x, maxLoc->y); + else + maxLoc->y = 0; + } } enum class ReduceMode diff --git a/modules/core/test/test_mat.cpp b/modules/core/test/test_mat.cpp index 68de691260..9f4711a816 100644 --- a/modules/core/test/test_mat.cpp +++ b/modules/core/test/test_mat.cpp @@ -2354,6 +2354,96 @@ TEST(Mat, regression_18473) EXPECT_EQ((int)5, (int)m.at(19, 49, 99)); } +// FITIT: remove DISABLE_ when 1D Mat is supported +TEST(Mat1D, DISABLED_basic) +{ + std::vector sizes { 100 }; + Mat m1(sizes, CV_8UC1, Scalar::all(5)); + m1.at(50) = 10; + EXPECT_FALSE(m1.empty()); + ASSERT_EQ(1, m1.dims); + ASSERT_EQ(1, m1.size.dims()); // hack map on .rows + EXPECT_EQ(Size(100, 1), m1.size()); + + { + SCOPED_TRACE("clone"); + Mat m = m1.clone(); + EXPECT_EQ(1, m.dims); + EXPECT_EQ(Size(100, 1), m.size()); + } + + { + SCOPED_TRACE("colRange()"); + Mat m = m1.colRange(Range(10, 30)); + EXPECT_EQ(1, m.dims); + EXPECT_EQ(Size(20, 1), m.size()); + } + + { + SCOPED_TRACE("reshape(1, 1)"); + Mat m = m1.reshape(1, 1); + EXPECT_EQ(1, m.dims); + EXPECT_EQ(Size(100, 1), m.size()); + } + + { + SCOPED_TRACE("reshape(1, 100)"); + Mat m = m1.reshape(1, 100); + EXPECT_EQ(2, m.dims); + EXPECT_EQ(Size(1, 100), m.size()); + } + + { + SCOPED_TRACE("reshape(1, {1, 100})"); + Mat m = m1.reshape(1, {1, 100}); + EXPECT_EQ(2, m.dims); + EXPECT_EQ(Size(100, 1), m.size()); + } + + { + SCOPED_TRACE("copyTo(std::vector)"); + std::vector dst; + m1.copyTo(dst); + EXPECT_EQ(100u, dst.size()); + } + + { + SCOPED_TRACE("copyTo(row2D)"); + Mat m(5, 100, CV_8UC1, Scalar::all(0)); + const Mat row2D = m.row(2); + EXPECT_NO_THROW(m1.copyTo(row2D)); + } + + { + SCOPED_TRACE("convertTo(row2D)"); + Mat m(5, 100, CV_32FC1, Scalar::all(0)); + const Mat row2D = m.row(2); + EXPECT_NO_THROW(m1.convertTo(row2D, CV_32FC1)); + } + + { + SCOPED_TRACE("CvMat"); + CvMat c_mat = cvMat(m1); + EXPECT_EQ(100, c_mat.cols); + EXPECT_EQ(1, c_mat.rows); + } + + { + SCOPED_TRACE("CvMatND"); + CvMatND c_mat = cvMatND(m1); + EXPECT_EQ(2, c_mat.dims); + EXPECT_EQ(100, c_mat.dim[0].size); + EXPECT_EQ(1, c_mat.dim[1].size); + } + + { + SCOPED_TRACE("minMaxLoc"); + Point pt; + minMaxLoc(m1, 0, 0, 0, &pt); + EXPECT_EQ(50, pt.x); + EXPECT_EQ(0, pt.y); + } +} TEST(Mat, ptrVecni_20044) {