简体   繁体   English

OpenCV - 二进制图像中所有非零像素的位置

[英]OpenCV - locations of all non-zero pixels in binary image

How can I find the locations of all non-zero pixels in a binary image (cv::Mat)? 如何在二进制图像(cv :: Mat)中找到所有非零像素的位置? Do I have to scan through every pixel in the image or is there a high level OpenCV function(s) that can be used? 我是否必须扫描图像中的每个像素,或者是否有可以使用的高级OpenCV功能? The output should be a vector of points (pixel locations). 输出应该是点矢量(像素位置)。

For example, this can be done in Matlab simply as: 例如,这可以在Matlab中完成,简单如下:

imstats = regionprops(binary_image, 'PixelList');
locations = imstats.PixelList;

or, even simpler 或者,甚至更简单

[x, y] = find(binary_image);
locations = [x, y];

Edit : In other words, how to find coordinates of all non-zero elements in cv::Mat? 编辑 :换句话说,如何在cv :: Mat中找到所有非零元素的坐标?

As suggested by @AbidRahmanK, there is a function cv::findNonZero in OpenCV version 2.4.4. 正如@AbidRahmanK所建议的,在OpenCV 2.4.4版中有一个函数cv::findNonZero Usage: 用法:

cv::Mat binaryImage; // input, binary image
cv::Mat locations;   // output, locations of non-zero pixels 
cv::findNonZero(binaryImage, locations);

It does the job. 它完成了这项工作。 This function was introduced in OpenCV version 2.4.4 (for example, it is not available in the version 2.4.2). 此功能是在OpenCV版本2.4.4中引入的(例如,版本2.4.2中不提供)。 Also, as of now findNonZero is not in the documentation for some reason. 此外,截至目前findNonZero由于某种原因不在文档中。

I placed this as an edit in Alex's answer, it did not get reviewed though so I'll post it here, as it is useful information imho. 我将此作为Alex的答案中的编辑,但它没有得到审查,所以我会在这里发布,因为它是有用的信息imho。

You can also pass a vector of Points, makes it easier to do something with them afterwards: 你也可以传递一个点向量,然后更容易用它们做一些事情:

std::vector<cv::Point2i> locations;   // output, locations of non-zero pixels 
cv::findNonZero(binaryImage, locations);

One note for the cv::findNonZero function in general: if binaryImage contains zero non-zero elements, it will throw because it tries to allocate '1 xn' memory, where n is cv::countNonZero , and n will obviously be 0 then. 一般来说cv::findNonZero函数有一个注释:如果binaryImage包含零个非零元素,它将抛出,因为它试图分配'1 xn'内存,其中n是cv::countNonZero ,n显然是0然后。 I circumvent that by manually calling cv::countNonZero beforehand but I don't really like that solution that much. 我通过事先手动调用cv::countNonZero避免这种情况,但我不太喜欢那个解决方案。

Anyone looking to do this in python. 有人想在python中做这件事。 it is also possible to do it with numpy arrays and therefore you don't need to upgrade your opencv version (or use undocumented functions). 它也可以使用numpy数组,因此您不需要升级opencv版本(或使用未记录的函数)。

mask = np.zeros(imgray.shape,np.uint8)
cv2.drawContours(mask,[cnt],0,255,-1)
pixelpoints = np.transpose(np.nonzero(mask))
#pixelpoints = cv2.findNonZero(mask)

Commented out is the same function using openCV instead. 注释掉的是使用openCV的相同功能。 For more info see: 有关详情,请参阅:

https://github.com/abidrahmank/OpenCV2-Python-Tutorials/blob/master/source/py_tutorials/py_imgproc/py_contours/py_contour_properties/py_contour_properties.rst https://github.com/abidrahmank/OpenCV2-Python-Tutorials/blob/master/source/py_tutorials/py_imgproc/py_contours/py_contour_properties/py_contour_properties.rst

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM