diff --git a/modules/core/src/mathfuncs.cpp b/modules/core/src/mathfuncs.cpp index 9fdf7d7702..f463539679 100644 --- a/modules/core/src/mathfuncs.cpp +++ b/modules/core/src/mathfuncs.cpp @@ -269,6 +269,9 @@ void cartToPolar( InputArray src1, InputArray src2, { CV_INSTRUMENT_REGION(); + CV_Assert(src1.getObj() != dst1.getObj() && src1.getObj() != dst2.getObj() && + src2.getObj() != dst1.getObj() && src2.getObj() != dst2.getObj()); + CV_OCL_RUN(dst1.isUMat() && dst2.isUMat(), ocl_cartToPolar(src1, src2, dst1, dst2, angleInDegrees)) @@ -563,6 +566,9 @@ void polarToCart( InputArray src1, InputArray src2, { CV_INSTRUMENT_REGION(); + CV_Assert(src1.getObj() != dst1.getObj() && src1.getObj() != dst2.getObj() && + src2.getObj() != dst1.getObj() && src2.getObj() != dst2.getObj()); + int type = src2.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); CV_Assert((depth == CV_32F || depth == CV_64F) && (src1.empty() || src1.type() == type)); diff --git a/modules/core/test/test_arithm.cpp b/modules/core/test/test_arithm.cpp index 74bf39fbc7..fa48877e3d 100644 --- a/modules/core/test/test_arithm.cpp +++ b/modules/core/test/test_arithm.cpp @@ -2396,5 +2396,36 @@ TEST(Core_Magnitude, regression_19506) } } +TEST(Core_CartPolar, inplace) +{ + RNG& rng = TS::ptr()->get_rng(); + cv::Mat1d A[2] = {cv::Mat1d(10, 10), cv::Mat1d(10, 10)}; + cv::Mat1d B[2], C[2]; + cv::UMat uA[2]; + + for(int i = 0; i < 2; ++i) + { + cvtest::randUni(rng, A[i], Scalar::all(-1000), Scalar::all(1000)); + A[i].copyTo(uA[i]); + } + + // Reverse + cv::cartToPolar(A[0], A[1], B[0], B[1], false); + cv::polarToCart(B[0], B[1], C[0], C[1], false); + EXPECT_MAT_NEAR(A[0], C[0], 2); + EXPECT_MAT_NEAR(A[1], C[1], 2); + + // Inplace + EXPECT_THROW(cv::polarToCart(B[0], B[1], B[0], B[1], false), cv::Exception); + EXPECT_THROW(cv::polarToCart(B[0], B[1], B[1], B[0], false), cv::Exception); + EXPECT_THROW(cv::cartToPolar(A[0], A[1], A[0], A[1], false), cv::Exception); + EXPECT_THROW(cv::cartToPolar(A[0], A[1], A[1], A[0], false), cv::Exception); + // Inplace OCL + EXPECT_THROW(cv::polarToCart(uA[0], uA[1], uA[0], uA[1]), cv::Exception); + EXPECT_THROW(cv::polarToCart(uA[0], uA[1], uA[1], uA[0]), cv::Exception); + EXPECT_THROW(cv::cartToPolar(uA[0], uA[1], uA[0], uA[1]), cv::Exception); + EXPECT_THROW(cv::cartToPolar(uA[0], uA[1], uA[0], uA[1]), cv::Exception); + +} }} // namespace