phil0stine
phil0stine

Reputation: 303

Get non zero indices from Mat OpenCV

I have a binary Matrix and would like to get the indices of the non-zero elements, preferably as a vector of cv::Points. There is a function that counts non zero elements, but that's not what I need.

In Matlab, the equivalent call would be simply find().

I could search through the entire matrix and save the indices, but that is not classy!

Upvotes: 5

Views: 2286

Answers (1)

Andrey Sboev
Andrey Sboev

Reputation: 7682

If you don't mind using numpy module see NumPy For Matlab Users. There is nonzero function which is eqivalent to matlab find.

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

Upvotes: 2

Related Questions