マッチングソース

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using OpenCvSharp;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//画像読み出し
Mat src1 = new Mat(@"Lenna0.png");
//Mat src2 = new Mat(@"Lenna1.png");
Mat src2 = new Mat(@"l_hires.jpg");

//Descripter:
KeyPoint[] keypoints1, keypoints2;
MatOfFloat descriptors1 = new MatOfFloat();
MatOfFloat descriptors2 = new MatOfFloat();

AKAZE akaze = AKAZE.Create();
ORB orb = ORB.Create();

var detector = orb;

detector.DetectAndCompute(src1, null, out keypoints1, descriptors1);
detector.DetectAndCompute(src2, null, out keypoints2, descriptors2);

//マッチングを行う
BFMatcher matcher = new BFMatcher();
DMatch[] matches = matcher.Match(descriptors1, descriptors2);

//特徴点のばらつきから抽出
//距離による抽出
// 最小距離
double min_dist = double.MaxValue;
for (int i = 0; i < (int)matches.Count(); i++)
{
double dist = matches[i].Distance;
if (dist < min_dist) min_dist = dist;
}

// 良いペアのみ残す
var good_matches = new List<DMatch>();
for (int i = 0; i < (int)matches.Count(); i++)
{
//min_distに近いものだけを残す
if (matches[i].Distance < 3.0 * min_dist)
{
good_matches.Add(matches[i]);
}
}
//角度による抽出
//平均角度を計算
//平均角度に近いものだけを残す

//画像を並べて特徴点ペアを描画
Mat outImg = new Mat();
Cv2.DrawMatches(src1, keypoints1, src2, keypoints2, matches, outImg);

using (new Window("outImg", outImg))
{
Cv2.WaitKey();
}
}
}
}

この記事が気に入ったらサポートをしてみませんか?