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开发者_JAVA技巧 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]))
精彩评论