From 7c475d2785b4845229394f3f164ebc6c0d6bbbf9 Mon Sep 17 00:00:00 2001 From: Shiven Solanki Date: Tue, 17 Nov 2026 12:09:11 +0800 Subject: [PATCH] core: support N-D Mats in cv::magnitude and cv::phase --- modules/core/src/mathfuncs.cpp | 4 ++-- modules/core/test/test_math.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/modules/core/src/mathfuncs.cpp b/modules/core/src/mathfuncs.cpp index 747f374d5d..f98d11029e 100644 --- a/modules/core/src/mathfuncs.cpp +++ b/modules/core/src/mathfuncs.cpp @@ -152,7 +152,7 @@ void magnitude( InputArray src1, InputArray src2, OutputArray dst ) CV_INSTRUMENT_REGION(); int type = src1.type(), depth = src1.depth(), cn = src1.channels(); - CV_Assert( src1.size() == src2.size() && type == src2.type() && (depth == CV_32F || depth == CV_64F)); + CV_Assert( src1.sameSize(src2) && type == src2.type() && (depth == CV_32F || depth == CV_64F)); CV_OCL_RUN(dst.isUMat() && src1.dims() <= 2 && src2.dims() <= 2, ocl_math_op(src1, src2, dst, OCL_OP_MAG)) @@ -188,7 +188,7 @@ void phase( InputArray src1, InputArray src2, OutputArray dst, bool angleInDegre CV_INSTRUMENT_REGION(); int type = src1.type(), depth = src1.depth(), cn = src1.channels(); - CV_Assert( src1.size() == src2.size() && type == src2.type() && (depth == CV_32F || depth == CV_64F)); + CV_Assert( src1.sameSize(src2) && type == src2.type() && (depth == CV_32F || depth == CV_64F)); CV_OCL_RUN(dst.isUMat() && src1.dims() <= 2 && src2.dims() <= 2, ocl_math_op(src1, src2, dst, angleInDegrees ? OCL_OP_PHASE_DEGREES : OCL_OP_PHASE_RADIANS)) diff --git a/modules/core/test/test_math.cpp b/modules/core/test/test_math.cpp index 3325f65268..bd5117dd59 100644 --- a/modules/core/test/test_math.cpp +++ b/modules/core/test/test_math.cpp @@ -2198,6 +2198,31 @@ TEST(Core_CheckRange_INT_MAX, accuracy) ASSERT_TRUE( cv::checkRange(m) ); } +TEST(Core_MagnitudePhase, nd_input) +{ + const int sz[] = {2, 3, 4}; + for (int depth : {CV_32F, CV_64F}) + { + SCOPED_TRACE(depth); + Mat x(3, sz, depth), y(3, sz, depth); + randu(x, -10, 10); + randu(y, -10, 10); + Mat x2d = x.reshape(1, 6), y2d = y.reshape(1, 6); + + Mat mag, mag2d; + ASSERT_NO_THROW(magnitude(x, y, mag)); + magnitude(x2d, y2d, mag2d); + EXPECT_EQ(x.size, mag.size); + EXPECT_EQ(0, cvtest::norm(mag.reshape(1, 6), mag2d, NORM_INF)); + + Mat angle, angle2d; + ASSERT_NO_THROW(phase(x, y, angle, true)); + phase(x2d, y2d, angle2d, true); + EXPECT_EQ(x.size, angle.size); + EXPECT_EQ(0, cvtest::norm(angle.reshape(1, 6), angle2d, NORM_INF)); + } +} + TEST(Core_CheckRange_INT_MAX1, accuracy) { cv::Mat m(3, 3, CV_32SC1, cv::Scalar(INT_MAX));