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
This commit is contained in:
Dmitry Kurtaev 2024-11-12 17:51:10 +03:00 committed by GitHub
parent 37c2af63f0
commit c230841105
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 1 deletions

View File

@ -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());

View File

@ -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<const char*> 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_t>{INT64_MAX, INT64_MIN, -1, 1, 0})