C++ OpenCV:查找二进制 Mat 图像的所有非零坐标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19242662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV: Find all non-zero coordinates of a binary Mat image
提问by DMor
I'm atttempting to find the non-zero (x,y) coordinates of a binary image.
我试图找到二进制图像的非零 (x,y) 坐标。
I've found a few references to the function countNonZero()
which only counts the non-zero coordinates and findNonZero()
which I'm unsure how to access or use since it seems to have been removed from the documentation completely.
我发现了一些对该函数的引用countNonZero()
,它只计算非零坐标findNonZero()
,我不确定如何访问或使用它,因为它似乎已从文档中完全删除。
Thisis the closest reference I found, but still not helpful at all. I would appreciate any specific help.
这是我找到的最接近的参考,但仍然没有帮助。我将不胜感激任何具体的帮助。
Edit: - To specify, this is using C++
编辑: - 要指定,这是使用 C++
回答by WangYudong
Hereis an explanation for how findNonZero()
saves non-zero elements. The following codes should be useful to access non-zero coordinates of your binaryimage. Method 1 used findNonZero()
in OpenCV, and Method 2 checked every pixels to find the non-zero (positive) ones.
这里解释了如何findNonZero()
保存非零元素。以下代码对于访问二进制图像的非零坐标应该很有用。findNonZero()
OpenCV中使用的方法1和方法2检查每个像素以找到非零(正)像素。
Method 1:
方法一:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv) {
Mat img = imread("binary image");
Mat nonZeroCoordinates;
findNonZero(img, nonZeroCoordinates);
for (int i = 0; i < nonZeroCoordinates.total(); i++ ) {
cout << "Zero#" << i << ": " << nonZeroCoordinates.at<Point>(i).x << ", " << nonZeroCoordinates.at<Point>(i).y << endl;
}
return 0;
}
Method 2:
方法二:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv) {
Mat img = imread("binary image");
for (int i = 0; i < img.cols; i++ ) {
for (int j = 0; j < img.rows; j++) {
if (img.at<uchar>(j, i) > 0) {
cout << i << ", " << j << endl; // Do your operations
}
}
}
return 0;
}
回答by Roger Rowland
There is the following source code that was supplied for OpenCV 2.4.3, which may be helpful:
为 OpenCV 2.4.3 提供了以下源代码,可能会有所帮助:
#include <opencv2/core/core.hpp>
#include <vector>
/*! @brief find non-zero elements in a Matrix
*
* Given a binary matrix (likely returned from a comparison
* operation such as compare(), >, ==, etc, return all of
* the non-zero indices as a std::vector<cv::Point> (x,y)
*
* This function aims to replicate the functionality of
* Matlab's command of the same name
*
* Example:
* \code
* // find the edges in an image
* Mat edges, thresh;
* sobel(image, edges);
* // theshold the edges
* thresh = edges > 0.1;
* // find the non-zero components so we can do something useful with them later
* vector<Point> idx;
* find(thresh, idx);
* \endcode
*
* @param binary the input image (type CV_8UC1)
* @param idx the output vector of Points corresponding to non-zero indices in the input
*/
void find(const cv::Mat& binary, std::vector<cv::Point> &idx) {
assert(binary.cols > 0 && binary.rows > 0 && binary.channels() == 1 && binary.depth() == CV_8U);
const int M = binary.rows;
const int N = binary.cols;
for (int m = 0; m < M; ++m) {
const char* bin_ptr = binary.ptr<char>(m);
for (int n = 0; n < N; ++n) {
if (bin_ptr[n] > 0) idx.push_back(cv::Point(n,m));
}
}
}
Note - it looks like the function signature was wrong so I've changed the output vector
to pass-by-reference.
注意 - 看起来函数签名是错误的,所以我已将输出更改vector
为传递引用。
回答by Tanusree
you can find it without using findNonZero() this opencv method. rather u can get it by simply using 2 for loops. here is the snippet. hope it can help u.
您可以在不使用 findNonZero() 这个 opencv 方法的情况下找到它。相反,您可以通过简单地使用 2 个 for 循环来获得它。这是片段。希望它可以帮助你。
**
**
for(int i = 0 ;i <image.rows() ; i++){// image : the binary image
for(int j = 0; j< image.cols() ; j++){
double[] returned = image.get(i,j);
int value = (int) returned[0];
if(value==255){
System.out.println("x: " +i + "\ty: "+j);// returned the (x,y) //co ordinates of all white pixels.
}
}
}
**
**