From 286f7524bb497658052f656b7e082460424184f6 Mon Sep 17 00:00:00 2001 From: Dmitry Kurtaev Date: Wed, 6 Nov 2024 08:11:59 +0300 Subject: [PATCH] Merge pull request #26420 from dkurt:fs_mat_0d_1d Support 0d/1d Mat in FileStorage #26420 ### 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/src/persistence_types.cpp | 4 ++- modules/core/test/test_io.cpp | 45 ++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/modules/core/src/persistence_types.cpp b/modules/core/src/persistence_types.cpp index cd4fad05fb..5bf419ec06 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,8 @@ 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(); 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 fd6cbace2d..be56329068 100644 --- a/modules/core/test/test_io.cpp +++ b/modules/core/test/test_io.cpp @@ -2025,4 +2025,49 @@ TEST(Core_InputOutput, FileStorage_invalid_attribute_value_regression_25946) ASSERT_EQ(0, std::remove(fileName.c_str())); } +template +T fsWriteRead(const T& expectedValue, const char* ext) +{ + std::string fname = cv::tempfile(ext); + FileStorage fs_w(fname, FileStorage::WRITE); + fs_w << "value" << expectedValue; + fs_w.release(); + + FileStorage fs_r(fname, FileStorage::READ); + + T value; + fs_r["value"] >> value; + 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); + EXPECT_EQ(cv::norm(src, dst, NORM_INF), 0.0); +} + +typedef testing::TestWithParam FileStorage_exact_type; +TEST_P(FileStorage_exact_type, empty_mat) +{ + testExactMat(Mat(), GetParam()); +} + +TEST_P(FileStorage_exact_type, mat_0d) +{ + testExactMat(Mat({}, CV_32S, Scalar(8)), GetParam()); +} + +TEST_P(FileStorage_exact_type, mat_1d) +{ + testExactMat(Mat({1}, CV_32S, Scalar(8)), GetParam()); +} + +INSTANTIATE_TEST_CASE_P(Core_InputOutput, + FileStorage_exact_type, Values(".yml", ".xml", ".json") +); + }} // namespace