Ask Your Question

Revision history [back]

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;
input = imread("./test.png", CV_LOAD_IMAGE_GRAYSCALE);

Canny(input, tmp, 35, 70);

SparseMat edgePoints(tmp);

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