From f1055a7e91372448a53fb2ecd4f56f5cd75fbc11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=A5=9A=E6=B4=8B?= Date: Sun, 2 Oct 2022 17:06:07 +0800 Subject: [PATCH 1/2] add test --- modules/imgproc/include/opencv2/imgproc.hpp | 1 + .../misc/python/test/test_matx_converter.py | 19 +++++++++++++++++++ modules/imgproc/src/test_matx_converter.cpp | 9 +++++++++ modules/python/src2/cv2_convert.hpp | 3 +++ 4 files changed, 32 insertions(+) create mode 100644 modules/imgproc/misc/python/test/test_matx_converter.py create mode 100644 modules/imgproc/src/test_matx_converter.cpp diff --git a/modules/imgproc/include/opencv2/imgproc.hpp b/modules/imgproc/include/opencv2/imgproc.hpp index afc2742ab6..073833e8f2 100644 --- a/modules/imgproc/include/opencv2/imgproc.hpp +++ b/modules/imgproc/include/opencv2/imgproc.hpp @@ -4973,6 +4973,7 @@ public: }; //! @cond IGNORED +CV_EXPORTS_W void testMatxPythonConverter(InputArray src, OutputArray dst, const Vec2d& defaultParam = Vec2d(-5, 5)); // === LineIterator implementation === diff --git a/modules/imgproc/misc/python/test/test_matx_converter.py b/modules/imgproc/misc/python/test/test_matx_converter.py new file mode 100644 index 0000000000..8a39031ca7 --- /dev/null +++ b/modules/imgproc/misc/python/test/test_matx_converter.py @@ -0,0 +1,19 @@ +from __future__ import print_function +import cv2 as cv +from cv2 import testMatxPythonConverter +from tests_common import NewOpenCVTests + + +class MatxConverterTest(NewOpenCVTests): + def test_matxconverter(self): + samples = ['samples/data/lena.jpg', 'cv/cascadeandhog/images/mona-lisa.png'] + + for sample in samples: + img = self.get_sample(sample) + out = testMatxPythonConverter(img) + + +if __name__ == '__main__': + NewOpenCVTests.bootstrap() + + diff --git a/modules/imgproc/src/test_matx_converter.cpp b/modules/imgproc/src/test_matx_converter.cpp new file mode 100644 index 0000000000..96e0ecacaf --- /dev/null +++ b/modules/imgproc/src/test_matx_converter.cpp @@ -0,0 +1,9 @@ +#include "precomp.hpp" + +namespace cv{ + void testMatxPythonConverter(InputArray _src, OutputArray _dst, const Vec2d& defaultParam){ + printf("%f %f\n", defaultParam[0], defaultParam[1]); + Mat src = _src.getMat(); + src.copyTo(_dst); + } +} diff --git a/modules/python/src2/cv2_convert.hpp b/modules/python/src2/cv2_convert.hpp index 563f5386b7..7dc0ffb788 100644 --- a/modules/python/src2/cv2_convert.hpp +++ b/modules/python/src2/cv2_convert.hpp @@ -62,6 +62,9 @@ PyObject* pyopencv_from(const T& src) { return PyOpenCV_Converter::from(src); template bool pyopencv_to(PyObject* o, cv::Matx<_Tp, m, n>& mx, const ArgInfo& info) { + if (!o || o == Py_None) + return true; + cv::Mat tmp; if (!pyopencv_to(o, tmp, info)) { return false; From 3a15152be5f4fbd743681ce20bc6fa182547f2aa Mon Sep 17 00:00:00 2001 From: Vadim Levin Date: Wed, 30 Nov 2022 17:40:38 +0300 Subject: [PATCH 2/2] refactor: rework test to be more specific --- .../include/opencv2/core/bindings_utils.hpp | 5 +++++ modules/imgproc/include/opencv2/imgproc.hpp | 1 - .../misc/python/test/test_matx_converter.py | 19 ------------------- modules/imgproc/src/test_matx_converter.cpp | 9 --------- modules/python/src2/cv2_convert.hpp | 3 ++- modules/python/test/test_misc.py | 7 +++++++ 6 files changed, 14 insertions(+), 30 deletions(-) delete mode 100644 modules/imgproc/misc/python/test/test_matx_converter.py delete mode 100644 modules/imgproc/src/test_matx_converter.cpp diff --git a/modules/core/include/opencv2/core/bindings_utils.hpp b/modules/core/include/opencv2/core/bindings_utils.hpp index 4f7eb532b9..76c9437a30 100644 --- a/modules/core/include/opencv2/core/bindings_utils.hpp +++ b/modules/core/include/opencv2/core/bindings_utils.hpp @@ -219,6 +219,11 @@ AsyncArray testAsyncException() return p.getArrayResult(); } +CV_WRAP static inline +String dumpVec2i(const cv::Vec2i value = cv::Vec2i(42, 24)) { + return format("Vec2i(%d, %d)", value[0], value[1]); +} + namespace nested { CV_WRAP static inline bool testEchoBooleanFunction(bool flag) { return flag; diff --git a/modules/imgproc/include/opencv2/imgproc.hpp b/modules/imgproc/include/opencv2/imgproc.hpp index 073833e8f2..afc2742ab6 100644 --- a/modules/imgproc/include/opencv2/imgproc.hpp +++ b/modules/imgproc/include/opencv2/imgproc.hpp @@ -4973,7 +4973,6 @@ public: }; //! @cond IGNORED -CV_EXPORTS_W void testMatxPythonConverter(InputArray src, OutputArray dst, const Vec2d& defaultParam = Vec2d(-5, 5)); // === LineIterator implementation === diff --git a/modules/imgproc/misc/python/test/test_matx_converter.py b/modules/imgproc/misc/python/test/test_matx_converter.py deleted file mode 100644 index 8a39031ca7..0000000000 --- a/modules/imgproc/misc/python/test/test_matx_converter.py +++ /dev/null @@ -1,19 +0,0 @@ -from __future__ import print_function -import cv2 as cv -from cv2 import testMatxPythonConverter -from tests_common import NewOpenCVTests - - -class MatxConverterTest(NewOpenCVTests): - def test_matxconverter(self): - samples = ['samples/data/lena.jpg', 'cv/cascadeandhog/images/mona-lisa.png'] - - for sample in samples: - img = self.get_sample(sample) - out = testMatxPythonConverter(img) - - -if __name__ == '__main__': - NewOpenCVTests.bootstrap() - - diff --git a/modules/imgproc/src/test_matx_converter.cpp b/modules/imgproc/src/test_matx_converter.cpp deleted file mode 100644 index 96e0ecacaf..0000000000 --- a/modules/imgproc/src/test_matx_converter.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "precomp.hpp" - -namespace cv{ - void testMatxPythonConverter(InputArray _src, OutputArray _dst, const Vec2d& defaultParam){ - printf("%f %f\n", defaultParam[0], defaultParam[1]); - Mat src = _src.getMat(); - src.copyTo(_dst); - } -} diff --git a/modules/python/src2/cv2_convert.hpp b/modules/python/src2/cv2_convert.hpp index 7dc0ffb788..700f29e3c5 100644 --- a/modules/python/src2/cv2_convert.hpp +++ b/modules/python/src2/cv2_convert.hpp @@ -62,8 +62,9 @@ PyObject* pyopencv_from(const T& src) { return PyOpenCV_Converter::from(src); template bool pyopencv_to(PyObject* o, cv::Matx<_Tp, m, n>& mx, const ArgInfo& info) { - if (!o || o == Py_None) + if (!o || o == Py_None) { return true; + } cv::Mat tmp; if (!pyopencv_to(o, tmp, info)) { diff --git a/modules/python/test/test_misc.py b/modules/python/test/test_misc.py index fd21656d83..deabbd25aa 100644 --- a/modules/python/test/test_misc.py +++ b/modules/python/test/test_misc.py @@ -736,6 +736,13 @@ class CanUsePurePythonModuleFunction(NewOpenCVTests): res = cv.utils._native.testOverwriteNativeMethod(123) self.assertEqual(res, 123, msg="Failed to call native method implementation") + def test_default_matx_argument(self): + res = cv.utils.dumpVec2i() + self.assertEqual(res, "Vec2i(42, 24)", + msg="Default argument is not properly handled") + res = cv.utils.dumpVec2i((12, 21)) + self.assertEqual(res, "Vec2i(12, 21)") + class SamplesFindFile(NewOpenCVTests):