バイナリ イメージの非ゼロ (x,y) 座標を見つけようとしています。
countNonZero()
ゼロ以外の座標のみをカウントする関数への参照をいくつか見つけましたがfindNonZero()
、ドキュメントから完全に削除されたように見えるため、アクセス方法や使用方法がわかりません。
これは私が見つけた最も近い参考文献ですが、それでもまったく役に立ちません。具体的な助けをいただければ幸いです。
編集:-指定するには、これはC ++を使用しています
バイナリ イメージの非ゼロ (x,y) 座標を見つけようとしています。
countNonZero()
ゼロ以外の座標のみをカウントする関数への参照をいくつか見つけましたがfindNonZero()
、ドキュメントから完全に削除されたように見えるため、アクセス方法や使用方法がわかりません。
これは私が見つけた最も近い参考文献ですが、それでもまったく役に立ちません。具体的な助けをいただければ幸いです。
編集:-指定するには、これはC ++を使用しています
findNonZero()
が非ゼロ要素を保存する方法について説明します。次のコードは、バイナリイメージのゼロ以外の座標にアクセスするのに役立ちます。方法 1findNonZero()
は OpenCV で使用され、方法 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;
}
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
を参照渡しに変更しました。