OpenCV:查找二进制Mat图像的所有非零坐标

时间:2013-10-08 08:25:08

标签: c++ opencv image-processing computer-vision computer-science

我正试图找到二进制图像的非零(x,y)坐标。

我找到了一些函数countNonZero()的引用,它只计算非零坐标和findNonZero(),我不确定如何访问或使用它,因为它似乎已被删除完整的文档。

This是我找到的最接近的参考,但仍然没有任何帮助。我将不胜感激。

编辑: - 要指定,这是使用C ++

3 个答案:

答案 0 :(得分:32)

Here解释了findNonZero()如何保存非零元素。以下代码对于访问二进制图像的非零坐标非常有用。方法1在OpenCV中使用findNonZero(),方法2检查每个像素以找到非零(正)的。

方法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;
}

方法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;
}

答案 1 :(得分:5)

以下源代码为supplied for 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));
        }
    }
}

注意 - 看起来函数签名错误,所以我将输出vector更改为传递引用。

答案 2 :(得分:-3)

你可以在不使用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.
                }
            }

        }

**