OpenCV - 二进制图像中所有非零像素的位置 [英] OpenCV - locations of all non-zero pixels in binary image

查看:1429
本文介绍了OpenCV - 二进制图像中所有非零像素的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在二进制图像(cv :: Mat)中找到所有非零像素的位置?我必须扫描图像中的每个像素或有一个高级别的OpenCV功能可以使用吗?输出应该是一个点的向量(像素位置)。

How can I find the locations of all non-zero pixels in a binary image (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? The output should be a vector of points (pixel locations).

例如,这可以在Matlab中完成:

For example, this can be done in Matlab simply as:

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

或更简单

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

编辑:换句话说, cv :: Mat?

Edit: In other words, how to find coordinates of all non-zero elements in cv::Mat?

推荐答案

如@AbidRahmanK建议,有一个函数 cv :: findNonZero 在OpenCV版本2.4.4。用法:

As suggested by @AbidRahmanK, there is a function cv::findNonZero in OpenCV version 2.4.4. Usage:

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

这样做。此功能在OpenCV版本2.4.4中引入(例如,它在版本2.4.2中不可用)。此外,由于某些原因, findNonZero 不在文档中。

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). Also, as of now findNonZero is not in the documentation for some reason.

这篇关于OpenCV - 二进制图像中所有非零像素的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆