c++
#include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/features2d/features2d.hpp> #include <opencv2/calib3d/calib3d.hpp> #include <opencv2/xfeatures2d/nonfree.hpp> #include <iostream> using namespace cv; using namespace std; int main(void) { int idx[2000]; int x,y,i = 0; int x2,y2,i2 = 0; int x3,y3 = 0; int x4,y4 = 0; int i5,i6 = 0; // 画像の読み込み Mat img_src1 = imread( "left.jpg" ); Mat img_src2 = imread( "right.jpg" ); // 画像の表示 imshow( "leftcamera", img_src1); imshow( "rightcamera", img_src2); waitKey( 0 ); auto akaze = AKAZE::create(); vector<KeyPoint> keypoints1,keypoints2; akaze->detect(img_src1,keypoints1); akaze->detect(img_src2,keypoints2); // 特徴点の座標、大きさを表示keypoint1 vector<KeyPoint>::iterator it = keypoints1.begin(), it_end = keypoints1.end(); for(;it != it_end;it++) { i++; x = it->pt.x; y = it->pt.y; cout << "keypoint1 " << i << " x=" << x << " y=" << y << "\n"; } // 特徴点の座標、大きさを表示keypoint2 vector<KeyPoint>::iterator it2 = keypoints2.begin(), it2_end = keypoints2.end(); for(;it2 != it2_end;it2++) { i2++; x2 = it2->pt.x; y2 = it2->pt.y; cout << "keypoint2 " << i2 << " x=" << x2 << " y=" << y2 << "\n"; } Mat descriptor1, descriptor2; akaze->compute(img_src1, keypoints1, descriptor1); akaze->compute(img_src2, keypoints2, descriptor2); //DescriptorMatcherオブジェクトの生成 Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce"); // 特徴点のマッチング情報を格納する変数 vector<DMatch> dmatch, match; // 特徴点マッチングの実行 matcher->match(descriptor1, descriptor2, match); // 最小距離の取得 double min_dist = DBL_MAX;//double の最大値 for (int i = 0; i < (int)match.size(); i++){ double dist = match[i].distance;//画像間の特徴点距離 //特徴点間の最小距離を求める if (dist < min_dist){ min_dist = dist; } } if (min_dist < 1.0) { min_dist = 1.0; } // しきい値を設定する const double threshold = 5.0 * min_dist; for (int i = 0; i < (int)match.size(); i++){ if (match[i].distance < threshold){ idx[i] = 1; dmatch.push_back(match[i]); }else{ idx[i] = -1; } } vector<DMatch>::iterator it5 = dmatch.begin(), it5_end = dmatch.end(); for (int i = 0; i < (int)match.size(); i++){ if(idx[i] != -1){ i5++; cout << "dmatch " << i5 << " "; /* 画像1の対応点座標 */ x3 = keypoints1[i].pt.x; y3 = keypoints1[i].pt.y; for(;it5 != it5_end;it5++) { i6++; /* 画像2の対応点座標 */ x4 = keypoints2[dmatch[i6]].pt.x; y4 = keypoints2[dmatch[i6]].pt.y; //座標表示 cout << "keypoint1 " << " x=" << x3 << " y=" << y3 << " " << "keypoint2 " << " x=" << x4 << " y=" << y4 << "\n"; } } } Mat dest; drawMatches(img_src1, keypoints1, img_src2, keypoints2, dmatch, dest); imshow("result",dest); waitKey( 0 ); return 0; }
左右の対応した画像の座標値を書き出したいのですが
/* 画像1の対応点座標 /
x3 = keypoints1[i].pt.x;
y3 = keypoints1[i].pt.y;
の箇所で画像1の座標値は取り出すことができましたが、
/ 画像2の対応点座標 */
x4 = keypoints2[dmatch[i6]].pt.x;
y4 = keypoints2[dmatch[i6]].pt.y;
の箇所で画像2の座標値を取り出すことができません、keypoint1[i]に対応するkeypoint2[i]の位置はmatch[i]の中に格納されていると思うのですがどのようにかけばいいのかがわかりません
まだ回答がついていません
会員登録して回答してみよう