Get Non-Zero Indexes from Mat OpenCV

I have a binary matrix and would like to get indices of nonzero elements, preferably in the form of a cv :: Points vector. There is a function that counts non-zero elements, but this is not what I need.

In Matlab, an equivalent call will simply find ().

I could search the entire matrix and save indexes, but this is not cool!

+5
source share
1 answer

If you don't mind using the module numpy, see NumPy for Matlab users . There is a function nonzerothat is equivalent to matlab find.

>>> m = cv.CreateMat(2,2,cv.CV_16SC1)
>>> a = numpy.asarray(m)
>>> a.nonzero()
(array([1, 1]), array([0, 1]))
+2
source

All Articles