From c230841105876d19cf35d1b824d81f46333e30af Mon Sep 17 00:00:00 2001 From: Dmitry Kurtaev Date: Tue, 12 Nov 2024 17:51:10 +0300 Subject: [PATCH] Merge pull request #26446 from dkurt:file_storage_empty_and_1d_mat * Change style of empty and 1d Mat in FileStorage * Remove misleading 1d Mat test --- modules/core/src/persistence_types.cpp | 7 ++++++- modules/core/test/test_io.cpp | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/modules/core/src/persistence_types.cpp b/modules/core/src/persistence_types.cpp index cd4fad05fb..8aa929cc1d 100644 --- a/modules/core/src/persistence_types.cpp +++ b/modules/core/src/persistence_types.cpp @@ -12,7 +12,7 @@ void write( FileStorage& fs, const String& name, const Mat& m ) { char dt[22]; - if( m.dims <= 2 ) + if( m.dims == 2 || m.empty() ) { fs.startWriteStruct(name, FileNode::MAP, String("opencv-matrix")); fs << "rows" << m.rows; @@ -151,6 +151,11 @@ void read(const FileNode& node, Mat& m, const Mat& default_mat) CV_Assert(!data_node.empty()); size_t nelems = data_node.size(); + if (nelems == 0) + { + m = Mat(); + return; + } CV_Assert(nelems == m.total()*m.channels()); data_node.readRaw(dt, (uchar*)m.ptr(), m.total()*m.elemSize()); diff --git a/modules/core/test/test_io.cpp b/modules/core/test/test_io.cpp index 88b72cd3f1..99972b964c 100644 --- a/modules/core/test/test_io.cpp +++ b/modules/core/test/test_io.cpp @@ -2018,7 +2018,25 @@ T fsWriteRead(const T& expectedValue, const char* ext) return value; } +void testExactMat(const Mat& src, const char* ext) +{ + bool srcIsEmpty = src.empty(); + Mat dst = fsWriteRead(src, ext); + EXPECT_EQ(dst.empty(), srcIsEmpty); + EXPECT_EQ(src.dims, dst.dims); + EXPECT_EQ(src.size, dst.size); + if (!srcIsEmpty) + { + EXPECT_EQ(0.0, cv::norm(src, dst, NORM_INF)); + } +} + typedef testing::TestWithParam FileStorage_exact_type; +TEST_P(FileStorage_exact_type, empty_mat) +{ + testExactMat(Mat(), GetParam()); +} + TEST_P(FileStorage_exact_type, long_int) { for (const int64_t expected : std::vector{INT64_MAX, INT64_MIN, -1, 1, 0})