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

问题描述

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



我发现几个对函数 countNonZero()的引用,它只计算非零坐标和 findNonZero()我不确定如何访问或使用,因为它似乎已完全从文档中删除。



是如何 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非零坐标;
findNonZero(img,nonZeroCoordinates);
for(int i = 0; i cout< 零#<< i<< :<<非零坐标。点>(i).x< ,<非零坐标。点>(i).y< endl;
}
return 0;
}

方法2:
$ b

  #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 for(int j = 0; j if(img。 at< uchar>(j,i)== 255){
cout< i<< ,< j<< endl; //你的操作
}
}
}
return 0;
}


I'm atttempting to find the non-zero (x,y) coordinates of a binary image.

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.

This is the closest reference I found, but still not helpful at all. I would appreciate any specific help.

Edit:- To specify, this is using C++

解决方案

Here is an explanation for how findNonZero() saves non-zero elements. The following codes should be useful to access non-zero coordinates of your binary image. Method 1 used findNonZero() in OpenCV, and Method 2 checked every pixels to find the non-zero ones.

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) == 255) {   
                cout << i << ", " << j << endl;     // Do your operations
            }
        }
    }
    return 0;
}

这篇关于OpenCV:找到二进制Mat图像的所有非零坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 00:24