Ask Your Question
0

How to save SparseMat to disk file? [closed]

asked 2018-03-26 09:36:52 -0600

For common Mat I can use imwrite()/imread(). How to do it for SparseMat?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by ya_ocv_user
close date 2018-03-26 10:43:15.035925

1 answer

Sort by ยป oldest newest most voted
2

answered 2018-03-26 10:28:12 -0600

LBerger gravatar image

updated 2018-03-26 10:41:00 -0600

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

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-03-26 09:36:52 -0600

Seen: 119 times

Last updated: Mar 26 '18