Skip to content

Commit c230841

Browse files
authored
Merge pull request opencv#26446 from dkurt:file_storage_empty_and_1d_mat
* Change style of empty and 1d Mat in FileStorage * Remove misleading 1d Mat test
1 parent 37c2af6 commit c230841

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

modules/core/src/persistence_types.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void write( FileStorage& fs, const String& name, const Mat& m )
1212
{
1313
char dt[22];
1414

15-
if( m.dims <= 2 )
15+
if( m.dims == 2 || m.empty() )
1616
{
1717
fs.startWriteStruct(name, FileNode::MAP, String("opencv-matrix"));
1818
fs << "rows" << m.rows;
@@ -151,6 +151,11 @@ void read(const FileNode& node, Mat& m, const Mat& default_mat)
151151
CV_Assert(!data_node.empty());
152152

153153
size_t nelems = data_node.size();
154+
if (nelems == 0)
155+
{
156+
m = Mat();
157+
return;
158+
}
154159
CV_Assert(nelems == m.total()*m.channels());
155160

156161
data_node.readRaw(dt, (uchar*)m.ptr(), m.total()*m.elemSize());

modules/core/test/test_io.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,7 +2018,25 @@ T fsWriteRead(const T& expectedValue, const char* ext)
20182018
return value;
20192019
}
20202020

2021+
void testExactMat(const Mat& src, const char* ext)
2022+
{
2023+
bool srcIsEmpty = src.empty();
2024+
Mat dst = fsWriteRead(src, ext);
2025+
EXPECT_EQ(dst.empty(), srcIsEmpty);
2026+
EXPECT_EQ(src.dims, dst.dims);
2027+
EXPECT_EQ(src.size, dst.size);
2028+
if (!srcIsEmpty)
2029+
{
2030+
EXPECT_EQ(0.0, cv::norm(src, dst, NORM_INF));
2031+
}
2032+
}
2033+
20212034
typedef testing::TestWithParam<const char*> FileStorage_exact_type;
2035+
TEST_P(FileStorage_exact_type, empty_mat)
2036+
{
2037+
testExactMat(Mat(), GetParam());
2038+
}
2039+
20222040
TEST_P(FileStorage_exact_type, long_int)
20232041
{
20242042
for (const int64_t expected : std::vector<int64_t>{INT64_MAX, INT64_MIN, -1, 1, 0})

0 commit comments

Comments
 (0)