Ask Your Question

Revision history [back]

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";

}

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