How to save SparseMat to disk file? [closed]
For common Mat I can use imwrite()/imread(). How to do it for SparseMat?
For common Mat I can use imwrite()/imread(). How to do it for SparseMat?
Use yml (or xml) format for SparseMat:
const int dims = 2;
int size[2] = { 10, 10 };
SparseMat sparse_mat(dims, size, CV_32F), sparse_mat2;
for (int i = 0; i < 10; i++)
{
int idx[dims];
for (int k = 0; k < dims; k++)
idx[k] = rand() % size[k];
sparse_mat.ref<float>(idx) += 1.f;
}
SparseMatConstIterator_<float> it = sparse_mat.begin<float>(),
it_end = sparse_mat.end<float>();
cout << "****************WRITE \n";
int dim = sparse_mat.dims();
for (; it != it_end; ++it)
{
const SparseMat::Node* n = it.node();
for (int i = 0; i < dim; i++)
cout<< n->idx[i] <<"\t";
cout << " -> " << it.value<float>() << "\n";
}
FileStorage fsp("sparse.yml", FileStorage::WRITE);
fsp << "image";
fsp << sparse_mat;
fsp.release();
FileStorage fsp2("sparse.yml", FileStorage::READ);
fsp2["image"]>> sparse_mat2;
fsp2.release();
cout << "****************READ \n";
SparseMatConstIterator_<float> it2 = sparse_mat2.begin<float>(),
it2_end = sparse_mat2.end<float>();
dim = sparse_mat2.dims();
for (; it2 != it2_end; ++it2)
{
const SparseMat::Node* n = it2.node();
for (int i = 0; i < dim; i++)
cout << n->idx[i] << "\t";
cout << " -> " << it2.value<float>() << "\n";
}
You can use tag in this forum and you will find this answer too
Asked: 2018-03-26 09:36:52 -0600
Seen: 111 times
Last updated: Mar 26 '18