From c7a21dc5bf5c0f48709419bab4a7a1ed1b236731 Mon Sep 17 00:00:00 2001 From: Dmitry Kurtaev Date: Mon, 11 Nov 2024 22:54:57 +0300 Subject: [PATCH] Merge pull request #26444 from dkurt:fix_empty_mat_assert Fix empty mat debug assertion #26444 ### 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 | 3 +++ modules/core/test/test_io.cpp | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/modules/core/src/persistence_types.cpp b/modules/core/src/persistence_types.cpp index 5bf419ec06..8aa929cc1d 100644 --- a/modules/core/src/persistence_types.cpp +++ b/modules/core/src/persistence_types.cpp @@ -152,7 +152,10 @@ void read(const FileNode& node, Mat& m, const Mat& default_mat) 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 77f918df1f..e6fffa2594 100644 --- a/modules/core/test/test_io.cpp +++ b/modules/core/test/test_io.cpp @@ -2043,7 +2043,10 @@ void testExactMat(const Mat& src, const char* 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); + if (!srcIsEmpty) + { + EXPECT_EQ(0.0, cv::norm(src, dst, NORM_INF)); + } } typedef testing::TestWithParam FileStorage_exact_type;