这是有关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;
}
0
我正在尝试查找二进制图像的非零(x,y)坐标。
我发现了对函数
countNonZero()
的一些引用,该函数仅计算非零坐标和findNonZero()
,我不确定如何访问或使用它,因为它似乎已完全从文档中删除。这是我找到的最接近的参考,但仍然没有帮助。我将不胜感激。
编辑:-要指定,这是使用C ++