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!
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]))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With