Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 638
phil0stine Avatar asked Nov 05 '22 20:11

phil0stine


1 Answers

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]))
like image 84
Andrey Sboev Avatar answered Nov 25 '22 07:11

Andrey Sboev