From ae4c67e3a0467e211d4a17485f4a76c110507156 Mon Sep 17 00:00:00 2001 From: Vincent Rabaud Date: Tue, 30 Jul 2024 11:44:12 +0200 Subject: [PATCH] Merge pull request #25945 from vrabaud:02_fix Fix size() for 0d matrix #25945 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake --- modules/core/include/opencv2/core/mat.inl.hpp | 2 +- modules/core/include/opencv2/core/types.hpp | 7 +++++-- modules/core/test/test_mat.cpp | 10 +++++++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/modules/core/include/opencv2/core/mat.inl.hpp b/modules/core/include/opencv2/core/mat.inl.hpp index 3c1ac9fe8a..8f82fd80c0 100644 --- a/modules/core/include/opencv2/core/mat.inl.hpp +++ b/modules/core/include/opencv2/core/mat.inl.hpp @@ -1275,7 +1275,7 @@ Size MatSize::operator()() const { int d = dims(); CV_DbgAssert(d <= 2); - return d == 2 ? Size(p[1], p[0]) : Size(p[0], 1); + return d == 2 ? Size(p[1], p[0]) : d == 1 ? Size(p[0], 1) : Size(0, 0); } inline diff --git a/modules/core/include/opencv2/core/types.hpp b/modules/core/include/opencv2/core/types.hpp index 06efd280ce..096348cc3c 100644 --- a/modules/core/include/opencv2/core/types.hpp +++ b/modules/core/include/opencv2/core/types.hpp @@ -320,10 +320,13 @@ struct Type< Point3_<_Tp> > { enum { value = CV_MAKETYPE(Depth<_Tp>::value, 3) } /** @brief Template class for specifying the size of an image or rectangle. -The class includes two members called width and height. The structure can be converted to and from -the old OpenCV structures CvSize and CvSize2D32f . The same set of arithmetic and comparison +The class includes two members called width and height. The same set of arithmetic and comparison operations as for Point_ is available. +For a 1d matrix, the size is (width, 1) and for a 0d matrix, it is (1, 1). + +For an empty matrix, it is (0, 0). + OpenCV defines the following Size_\<\> aliases: @code typedef Size_ Size2i; diff --git a/modules/core/test/test_mat.cpp b/modules/core/test/test_mat.cpp index 8877481f63..653caf8cbd 100644 --- a/modules/core/test/test_mat.cpp +++ b/modules/core/test/test_mat.cpp @@ -2414,7 +2414,15 @@ TEST(Mat, regression_18473) EXPECT_EQ((int)5, (int)m.at(19, 49, 99)); } -// FITIT: remove DISABLE_ when 1D Mat is supported +TEST(Mat0D, basic) +{ + Mat1b m1, m2(0, nullptr); + ASSERT_EQ(0, m1.size().width); + ASSERT_EQ(0, m1.size().height); + ASSERT_EQ(1, m2.size().width); + ASSERT_EQ(1, m2.size().height); +} + TEST(Mat1D, basic) { std::vector sizes { 100 };