From 442999dcdb0e51fe48661fe92803a568a52eeec9 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Fri, 12 Jun 2020 10:07:58 +0000 Subject: [PATCH] core: fix handling of ND-arrays in dumpInputArray() helpers --- modules/core/src/bindings_utils.cpp | 94 ++++++++++++++++++++++++----- modules/python/test/test_misc.py | 21 +++++-- 2 files changed, 97 insertions(+), 18 deletions(-) diff --git a/modules/core/src/bindings_utils.cpp b/modules/core/src/bindings_utils.cpp index 432f280e52..050b7247f8 100644 --- a/modules/core/src/bindings_utils.cpp +++ b/modules/core/src/bindings_utils.cpp @@ -25,9 +25,26 @@ String dumpInputArray(InputArray argument) break; // done } ss << cv::format(" total(-1)=%lld", (long long int)argument.total(-1)); - ss << cv::format(" dims(-1)=%d", argument.dims(-1)); - Size size = argument.size(-1); - ss << cv::format(" size(-1)=%dx%d", size.width, size.height); + int dims = argument.dims(-1); + ss << cv::format(" dims(-1)=%d", dims); + if (dims <= 2) + { + Size size = argument.size(-1); + ss << cv::format(" size(-1)=%dx%d", size.width, size.height); + } + else + { + int sz[CV_MAX_DIM] = {0}; + argument.sizend(sz, -1); + ss << " size(-1)=["; + for (int i = 0; i < dims; i++) + { + if (i > 0) + ss << ' '; + ss << sz[i]; + } + ss << "]"; + } ss << " type(-1)=" << cv::typeToString(argument.type(-1)); } while (0); } @@ -61,10 +78,26 @@ CV_EXPORTS_W String dumpInputArrayOfArrays(InputArrayOfArrays argument) if (argument.total(-1) > 0) { ss << " type(0)=" << cv::typeToString(argument.type(0)); - ss << cv::format(" dims(0)=%d", argument.dims(0)); - size = argument.size(0); - ss << cv::format(" size(0)=%dx%d", size.width, size.height); - ss << " type(0)=" << cv::typeToString(argument.type(0)); + int dims = argument.dims(0); + ss << cv::format(" dims(0)=%d", dims); + if (dims <= 2) + { + Size size0 = argument.size(0); + ss << cv::format(" size(0)=%dx%d", size0.width, size0.height); + } + else + { + int sz[CV_MAX_DIM] = {0}; + argument.sizend(sz, 0); + ss << " size(0)=["; + for (int i = 0; i < dims; i++) + { + if (i > 0) + ss << ' '; + ss << sz[i]; + } + ss << "]"; + } } } while (0); } @@ -92,9 +125,26 @@ CV_EXPORTS_W String dumpInputOutputArray(InputOutputArray argument) break; // done } ss << cv::format(" total(-1)=%lld", (long long int)argument.total(-1)); - ss << cv::format(" dims(-1)=%d", argument.dims(-1)); - Size size = argument.size(-1); - ss << cv::format(" size(-1)=%dx%d", size.width, size.height); + int dims = argument.dims(-1); + ss << cv::format(" dims(-1)=%d", dims); + if (dims <= 2) + { + Size size = argument.size(-1); + ss << cv::format(" size(-1)=%dx%d", size.width, size.height); + } + else + { + int sz[CV_MAX_DIM] = {0}; + argument.sizend(sz, -1); + ss << " size(-1)=["; + for (int i = 0; i < dims; i++) + { + if (i > 0) + ss << ' '; + ss << sz[i]; + } + ss << "]"; + } ss << " type(-1)=" << cv::typeToString(argument.type(-1)); } while (0); } @@ -128,10 +178,26 @@ CV_EXPORTS_W String dumpInputOutputArrayOfArrays(InputOutputArrayOfArrays argume if (argument.total(-1) > 0) { ss << " type(0)=" << cv::typeToString(argument.type(0)); - ss << cv::format(" dims(0)=%d", argument.dims(0)); - size = argument.size(0); - ss << cv::format(" size(0)=%dx%d", size.width, size.height); - ss << " type(0)=" << cv::typeToString(argument.type(0)); + int dims = argument.dims(0); + ss << cv::format(" dims(0)=%d", dims); + if (dims <= 2) + { + Size size0 = argument.size(0); + ss << cv::format(" size(0)=%dx%d", size0.width, size0.height); + } + else + { + int sz[CV_MAX_DIM] = {0}; + argument.sizend(sz, 0); + ss << " size(0)=["; + for (int i = 0; i < dims; i++) + { + if (i > 0) + ss << ' '; + ss << sz[i]; + } + ss << "]"; + } } } while (0); } diff --git a/modules/python/test/test_misc.py b/modules/python/test/test_misc.py index b25ef7efbb..f9a350d160 100644 --- a/modules/python/test/test_misc.py +++ b/modules/python/test/test_misc.py @@ -105,22 +105,35 @@ class Arguments(NewOpenCVTests): a = np.array([[[1, 2]], [[3, 4]], [[5, 6]]], dtype=float) res5 = cv.utils.dumpInputArray(a) # 64FC2 self.assertEqual(res5, "InputArray: empty()=false kind=0x00010000 flags=0x01010000 total(-1)=3 dims(-1)=2 size(-1)=1x3 type(-1)=CV_64FC2") + a = np.zeros((2,3,4), dtype='f') + res6 = cv.utils.dumpInputArray(a) + self.assertEqual(res6, "InputArray: empty()=false kind=0x00010000 flags=0x01010000 total(-1)=6 dims(-1)=2 size(-1)=3x2 type(-1)=CV_32FC4") + a = np.zeros((2,3,4,5), dtype='f') + res7 = cv.utils.dumpInputArray(a) + self.assertEqual(res7, "InputArray: empty()=false kind=0x00010000 flags=0x01010000 total(-1)=120 dims(-1)=4 size(-1)=[2 3 4 5] type(-1)=CV_32FC1") def test_InputArrayOfArrays(self): res1 = cv.utils.dumpInputArrayOfArrays(None) # self.assertEqual(res1, "InputArray: noArray()") # not supported self.assertEqual(res1, "InputArrayOfArrays: empty()=true kind=0x00050000 flags=0x01050000 total(-1)=0 dims(-1)=1 size(-1)=0x0") res2_1 = cv.utils.dumpInputArrayOfArrays((1, 2)) # { Scalar:all(1), Scalar::all(2) } - self.assertEqual(res2_1, "InputArrayOfArrays: empty()=false kind=0x00050000 flags=0x01050000 total(-1)=2 dims(-1)=1 size(-1)=2x1 type(0)=CV_64FC1 dims(0)=2 size(0)=1x4 type(0)=CV_64FC1") + self.assertEqual(res2_1, "InputArrayOfArrays: empty()=false kind=0x00050000 flags=0x01050000 total(-1)=2 dims(-1)=1 size(-1)=2x1 type(0)=CV_64FC1 dims(0)=2 size(0)=1x4") res2_2 = cv.utils.dumpInputArrayOfArrays([1.5]) - self.assertEqual(res2_2, "InputArrayOfArrays: empty()=false kind=0x00050000 flags=0x01050000 total(-1)=1 dims(-1)=1 size(-1)=1x1 type(0)=CV_64FC1 dims(0)=2 size(0)=1x4 type(0)=CV_64FC1") + self.assertEqual(res2_2, "InputArrayOfArrays: empty()=false kind=0x00050000 flags=0x01050000 total(-1)=1 dims(-1)=1 size(-1)=1x1 type(0)=CV_64FC1 dims(0)=2 size(0)=1x4") a = np.array([[1, 2], [3, 4], [5, 6]]) b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) res3 = cv.utils.dumpInputArrayOfArrays([a, b]) - self.assertEqual(res3, "InputArrayOfArrays: empty()=false kind=0x00050000 flags=0x01050000 total(-1)=2 dims(-1)=1 size(-1)=2x1 type(0)=CV_32SC1 dims(0)=2 size(0)=2x3 type(0)=CV_32SC1") + self.assertEqual(res3, "InputArrayOfArrays: empty()=false kind=0x00050000 flags=0x01050000 total(-1)=2 dims(-1)=1 size(-1)=2x1 type(0)=CV_32SC1 dims(0)=2 size(0)=2x3") c = np.array([[[1, 2], [3, 4], [5, 6]]], dtype='f') res4 = cv.utils.dumpInputArrayOfArrays([c, a, b]) - self.assertEqual(res4, "InputArrayOfArrays: empty()=false kind=0x00050000 flags=0x01050000 total(-1)=3 dims(-1)=1 size(-1)=3x1 type(0)=CV_32FC2 dims(0)=2 size(0)=3x1 type(0)=CV_32FC2") + self.assertEqual(res4, "InputArrayOfArrays: empty()=false kind=0x00050000 flags=0x01050000 total(-1)=3 dims(-1)=1 size(-1)=3x1 type(0)=CV_32FC2 dims(0)=2 size(0)=3x1") + a = np.zeros((2,3,4), dtype='f') + res5 = cv.utils.dumpInputArrayOfArrays([a, b]) + self.assertEqual(res5, "InputArrayOfArrays: empty()=false kind=0x00050000 flags=0x01050000 total(-1)=2 dims(-1)=1 size(-1)=2x1 type(0)=CV_32FC4 dims(0)=2 size(0)=3x2") + # TODO: fix conversion error + #a = np.zeros((2,3,4,5), dtype='f') + #res6 = cv.utils.dumpInputArray([a, b]) + #self.assertEqual(res6, "InputArrayOfArrays: empty()=false kind=0x00050000 flags=0x01050000 total(-1)=2 dims(-1)=1 size(-1)=2x1 type(0)=CV_32FC1 dims(0)=4 size(0)=[2 3 4 5]") def test_parse_to_bool_convertible(self): try_to_convert = partial(self._try_to_convert, cv.utils.dumpBool)