Ask Your Question

Revision history [back]

I don't understand your problem If change code (answer) :

const int dims = 2;
int size[2] = { pow(2,31)-1,pow(2,31)-1 };
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] = size[k] - 10*k-1-i;
    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";

}

answer is

****************WRITE
2147483639      2147483629       -> 1
2147483643      2147483633       -> 1
2147483638      2147483628       -> 1
2147483642      2147483632       -> 1
2147483646      2147483636       -> 1
2147483637      2147483627       -> 1
2147483641      2147483631       -> 1
2147483645      2147483635       -> 1
2147483640      2147483630       -> 1
2147483644      2147483634       -> 1
****************READ
2147483639      2147483629       -> 1
2147483643      2147483633       -> 1
2147483642      2147483632       -> 1
2147483646      2147483636       -> 1
2147483638      2147483628       -> 1
2147483645      2147483635       -> 1
2147483637      2147483627       -> 1
2147483641      2147483631       -> 1
2147483640      2147483630       -> 1
2147483644      2147483634       -> 1

Now inside code hash value is an int64 (MSVC 2017 Win64) and index is an unsigned int

yml file is :

%YAML:1.0

image: !!opencv-sparse-matrix sizes: [ 2147483647, 2147483647 ] dt: f data: [ 2147483637, 2147483627, 1., -1, 2147483638, 2147483628, 1., -1, 2147483639, 2147483629, 1., -1, 2147483640, 2147483630, 1., -1, 2147483641, 2147483631, 1., -1, 2147483642, 2147483632, 1., -1, 2147483643, 2147483633, 1., -1, 2147483644, 2147483634, 1., -1, 2147483645, 2147483635, 1., -1, 2147483646, 2147483636, 1. ]

I don't understand your problem If change code (answer) :

const int dims = 2;
int size[2] = { pow(2,31)-1,pow(2,31)-1 };
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] = size[k] - 10*k-1-i;
    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";

}

answer is

****************WRITE
2147483639      2147483629       -> 1
2147483643      2147483633       -> 1
2147483638      2147483628       -> 1
2147483642      2147483632       -> 1
2147483646      2147483636       -> 1
2147483637      2147483627       -> 1
2147483641      2147483631       -> 1
2147483645      2147483635       -> 1
2147483640      2147483630       -> 1
2147483644      2147483634       -> 1
****************READ
2147483639      2147483629       -> 1
2147483643      2147483633       -> 1
2147483642      2147483632       -> 1
2147483646      2147483636       -> 1
2147483638      2147483628       -> 1
2147483645      2147483635       -> 1
2147483637      2147483627       -> 1
2147483641      2147483631       -> 1
2147483640      2147483630       -> 1
2147483644      2147483634       -> 1

Now inside code hash value is an int64 (MSVC 2017 Win64) and index is an unsigned int

yml file is :

%YAML:1.0

image: !!opencv-sparse-matrix sizes: [ 2147483647, 2147483647 ] dt: f data: [ 2147483637, 2147483627, 1., -1, 2147483638, 2147483628, 1., -1, 2147483639, 2147483629, 1., -1, 2147483640, 2147483630, 1., -1, 2147483641, 2147483631, 1., -1, 2147483642, 2147483632, 1., -1, 2147483643, 2147483633, 1., -1, 2147483644, 2147483634, 1., -1, 2147483645, 2147483635, 1., -1, 2147483646, 2147483636, 1. ]

About -1 between data it's here Value between data is equal to -dims+1 for dim=2 it is -1 and dim=3 -2 ...

Why a separator? I don't know!