开发者

Get non zero indices from Mat OpenCV

开发者 https://www.devze.com 2023-03-11 08:19 出处:网络
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 n

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]))
0

精彩评论

暂无评论...
验证码 换一张
取 消