c++
1#include <opencv2/highgui/highgui.hpp> 2#include <opencv2/imgproc/imgproc.hpp> 3#include <opencv2/features2d/features2d.hpp> 4#include <opencv2/calib3d/calib3d.hpp> 5#include <opencv2/xfeatures2d/nonfree.hpp> 6#include <iostream> 7 8using namespace cv; 9using namespace std; 10 11int main(void) 12{ 13 14 int idx[2000]; 15 int x,y,i = 0; 16 int x2,y2,i2 = 0; 17 int x3,y3 = 0; 18 int x4,y4 = 0; 19 int i5 = 0; 20 int dis,ave = 0; 21 int distance = 0; 22 int i6 = 0; 23 int x5,y5 = 0; 24 int x6,y6 = 0; 25 26 // 画像の読み込み 27 Mat img_src1 = imread( "left20.jpg" ); 28 Mat img_src2 = imread( "right20.jpg" ); 29 30 // 画像の表示 31 imshow( "leftcamera", img_src1); 32 imshow( "rightcamera", img_src2); 33 waitKey( 0 ); 34 35 auto akaze = AKAZE::create(); 36 37 vector<KeyPoint> keypoints1,keypoints2; 38 akaze->detect(img_src1,keypoints1); 39 akaze->detect(img_src2,keypoints2); 40 41 // 特徴点の座標、大きさを表示keypoint1 42 vector<KeyPoint>::iterator it = keypoints1.begin(), it_end = keypoints1.end(); 43 for(;it != it_end;it++) { 44 i++; 45 x = it->pt.x; 46 y = it->pt.y; 47 cout << "keypoint1 " << i << " x=" << x << " y=" << y << "\n"; 48 } 49 // 特徴点の座標、大きさを表示keypoint2 50 vector<KeyPoint>::iterator it2 = keypoints2.begin(), it2_end = keypoints2.end(); 51 for(;it2 != it2_end;it2++) { 52 i2++; 53 x2 = it2->pt.x; 54 y2 = it2->pt.y; 55 cout << "keypoint2 " << i2 << " x=" << x2 << " y=" << y2 << "\n"; 56 } 57 58 Mat descriptor1, descriptor2; 59 akaze->compute(img_src1, keypoints1, descriptor1); 60 akaze->compute(img_src2, keypoints2, descriptor2); 61 62 //DescriptorMatcherオブジェクトの生成 63 Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce"); 64 // 特徴点のマッチング情報を格納する変数 65 vector<DMatch> dmatch, match; 66 // 特徴点マッチングの実行 67 matcher->match(descriptor1, descriptor2, match); 68 69 // 最小距離の取得 70 double min_dist = DBL_MAX;//double の最大値 71 for (int i = 0; i < (int)match.size(); i++){ 72 int kpp1_idx = match[i].queryIdx; // keypoint1 のインデックス 73 int kpp2_idx = match[i].trainIdx; // keypoint2 のインデックス 74 /* 画像1の対応点座標 */ 75 x5 = keypoints1[kpp1_idx].pt.x; 76 y5 = keypoints1[kpp1_idx].pt.y; 77 /* 画像2の対応点座標 */ 78 x6 = keypoints2[kpp2_idx].pt.x; 79 y6 = keypoints2[kpp2_idx].pt.y; 80 //座標表示 81 cout << "keypoint1 " << " x=" << x5 << " y=" << y5 << " " << "keypoint2 " << " x=" << x6<< " y=" << y6 << "\n"; 82 83 double dist = match[i].distance;//画像間の特徴点距離 84 cout << "distance"<< " "<< i << " " << dist << "\n"; 85 86 //特徴点間の最小距離を求める 87 if (dist < min_dist){ 88 min_dist = dist; 89 cout << "dist"<< " "<< i << " " << min_dist << "\n"; 90 } 91 } 92 // if (min_dist < 1.0){ 93 // min_dist = 1.0; 94 //} 95 96 // しきい値を設定する 97 const double threshold = 5.0 * min_dist; 98 cout << "threshold"<< " "<< threshold << "\n"; 99 100 for (int i = 0; i < (int)match.size(); i++){ 101 if (match[i].distance < threshold){ 102 idx[i] = 1; 103 dmatch.push_back(match[i]); 104 }else{ 105 idx[i] = -1; 106 } 107 } 108 109 for (int i = 0; i < (int)match.size(); i++){ 110 int kp1_idx = match[i].queryIdx; // keypoint1 のインデックス 111 int kp2_idx = match[i].trainIdx; // keypoint2 のインデックス 112 113 if(idx[i] != -1){ 114 i5++; 115 cout << "dmatch " << i5 << " "; 116 /* 画像1の対応点座標 */ 117 x3 = keypoints1[kp1_idx].pt.x; 118 y3 = keypoints1[kp1_idx].pt.y; 119 /* 画像2の対応点座標 */ 120 x4 = keypoints2[kp2_idx].pt.x; 121 y4 = keypoints2[kp2_idx].pt.y; 122 //座標表示 123 cout << "keypoint1 " << " x=" << x3 << " y=" << y3 << " " << "keypoint2 " << " x=" << x4 << " y=" << y4 << "\n"; 124 dis += (x3-x4); 125 ave = dis/i5; 126 } 127 } 128 cout << "x軸の特徴点座標差異の平均値は" << ave << "です\n"; 129 130 131 132 Mat dest; 133 drawMatches(img_src1, keypoints1, img_src2, keypoints2, dmatch, dest); 134 135 imshow("result",dest); 136 waitKey( 0 ); 137 138 return 0; 139} 140 141
左右画像のkeypoint1,keypoint2の特徴点検出数を指定したいと考えています。
auto akaze = AKAZE::create();の部分でパラメータを決定するようですがパラメータ指定の方法がわかりません。または他の部分で指定するのでしょうか??
またdouble dist = match[i].distance;//画像間の特徴点距離の部分でも止まる距離はどのように求めているのでしょうか?ご教授お願いいたします
回答1件
あなたの回答
tips
プレビュー