Merge pull request #7627 from sovrasov:fs_vec_of_vec

This commit is contained in:
Vadim Pisarevsky 2016-11-22 12:40:22 +00:00
commit 54d215902f
2 changed files with 57 additions and 1 deletions

View File

@ -958,7 +958,6 @@ void write( FileStorage& fs, const std::vector<_Tp>& vec )
w(vec); w(vec);
} }
template<typename _Tp> static inline template<typename _Tp> static inline
void write(FileStorage& fs, const String& name, const Point_<_Tp>& pt ) void write(FileStorage& fs, const String& name, const Point_<_Tp>& pt )
{ {
@ -1022,6 +1021,17 @@ void write( FileStorage& fs, const String& name, const std::vector<_Tp>& vec )
write(fs, vec); write(fs, vec);
} }
template<typename _Tp> static inline
void write( FileStorage& fs, const String& name, const std::vector< std::vector<_Tp> >& vec )
{
cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ);
for(size_t i = 0; i < vec.size(); i++)
{
cv::internal::WriteStructContext ws_(fs, name, FileNode::SEQ+(DataType<_Tp>::fmt != 0 ? FileNode::FLOW : 0));
write(fs, vec[i]);
}
}
//! @} FileStorage //! @} FileStorage
//! @relates cv::FileNode //! @relates cv::FileNode

View File

@ -950,3 +950,49 @@ TEST(Core_InputOutput, filestorage_utf8_bom)
fs.release(); fs.release();
}); });
} }
TEST(Core_InputOutput, filestorage_vec_vec_io)
{
std::vector<std::vector<Mat> > outputMats(3);
for(size_t i = 0; i < outputMats.size(); i++)
{
outputMats[i].resize(i+1);
for(size_t j = 0; j < outputMats[i].size(); j++)
{
outputMats[i][j] = Mat::eye((int)i + 1, (int)i + 1, CV_8U);
}
}
String fileName = "vec_test.";
std::vector<String> formats;
formats.push_back("xml");
formats.push_back("yml");
formats.push_back("json");
for(size_t i = 0; i < formats.size(); i++)
{
FileStorage writer(fileName + formats[i], FileStorage::WRITE);
writer << "vecVecMat" << outputMats;
writer.release();
FileStorage reader(fileName + formats[i], FileStorage::READ);
std::vector<std::vector<Mat> > testMats;
reader["vecVecMat"] >> testMats;
ASSERT_EQ(testMats.size(), testMats.size());
for(size_t j = 0; j < testMats.size(); j++)
{
ASSERT_EQ(testMats[j].size(), outputMats[j].size());
for(size_t k = 0; k < testMats[j].size(); k++)
{
ASSERT_TRUE(norm(outputMats[j][k] - testMats[j][k], NORM_INF) == 0);
}
}
reader.release();
remove((fileName + formats[i]).c_str());
}
}