SparseMat constructed from Mat also storing zero values

I'm trying to store detected edge points of canny filter algorithm in a sparse matrix ( SparseMat ). This should make it easy to iterate through the detected edge points.
But if I construct the sparse matrix from the edge map ( Mat ) it contains as much elements as specified by width and height of the image (see code below).
So it's also storing the zero elements of the edge map. In my understanding a sparse matrix is intended to only store non zero values and the edge map contains zero values to a high extent, that should not be stored.
What am I missing? Do I construct the SparseMat in the correct way?

Regards Knight123

Mat input, tmp;

Canny(input, tmp, 35, 70);

SparseMat edgePoints(tmp);

edgePoints.nzcount(); // returns 360960 = width x height
countNonZero(tmp); // returns 7878

edit retag close merge delete

Sort by » oldest newest most voted

I guess the explanation says it all:

Sparse means that only non-zero elements are stored (though, as a result of operations on a sparse matrix, some of its stored elements can actually become 0. It is up to you to detect such elements and delete them using SparseMat::erase ).

Loop over the sparsematrix and delete the zeros manually before proceeding?

more