emm
懒了很久,还有最后一点儿,全部整理完~
Record
1、SIFI算子使用流程:
- 初始化SIFT向量,调用Detect()方法识别两张图片各自的特征点;
- 使用SIFT的Compute()方法计算计算两张图片各自特征点的特征描述符;
- 使用匹配器Matcher进行特征描述符匹配(BF与Flann),以其中一幅图片为模板,在另一幅图片中寻找匹配的特征点;
- 匹配结果保存在DMatch结构体变量中,设置距离阈值对匹配结果进行筛选,得到好的匹配点;
- 使用DrawMatch()方法进行绘制,显示匹配结果;
2、BF暴力匹配器在EmguCV中通过BFMatcher类进行实现,使用Add()
方法添加模板的特征描述符,使用 KnnMatch() 方法寻找计算另一幅图像中的k个匹配特征点;
3、可以参考:https://www.cnblogs.com/Jessica-jie/p/8622449.html
4、PPT:
Code
BF匹配:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.Util;
using System.Drawing;
using Emgu.CV.XFeatures2D; //包含SIFT类
using Emgu.CV.Features2D; //包含Features2DToolBox类
namespace lesson33
{
class Program
{
static void Main(string[] args)
{
Mat srcImg1 = CvInvoke.Imread("1.jpg");
Mat srcImg2 = CvInvoke.Imread("2.jpg");
CvInvoke.Imshow("src1", srcImg1);
CvInvoke.Imshow("src2", srcImg2);
SIFT sift = new SIFT(1000);
//计算特征点
MKeyPoint[] keyPoints1 = sift.Detect(srcImg1);
MKeyPoint[] keyPoints2 = sift.Detect(srcImg2);
//绘制特征点
Mat sift_feature1 = new Mat();
Mat sift_feature2 = new Mat();
VectorOfKeyPoint vkeyPoint1 = new VectorOfKeyPoint(keyPoints1);
VectorOfKeyPoint vkeyPoint2 = new VectorOfKeyPoint(keyPoints2);
Features2DToolbox.DrawKeypoints(srcImg1, vkeyPoint1, sift_feature1, new Bgr(0, 255, 0),
Features2DToolbox.KeypointDrawType.Default);
Features2DToolbox.DrawKeypoints(srcImg2, vkeyPoint2, sift_feature2, new Bgr(0, 255, 0));
//显示绘制结果
CvInvoke.Imshow("sift_feature1", sift_feature1);
CvInvoke.Imshow("sift_feature2", sift_feature2);
//计算特征描述符
Mat descriptors1 = new Mat();
Mat descriptors2 = new Mat();
sift.Compute(srcImg1, vkeyPoint1, descriptors1);
sift.Compute(srcImg2, vkeyPoint2, descriptors2);
//使用BF匹配器进行暴力匹配
BFMatcher bFMatcher = new BFMatcher(DistanceType.L2);
VectorOfVectorOfDMatch matches = new VectorOfVectorOfDMatch();
//添加特征描述符
bFMatcher.Add(descriptors1);
//k最邻近匹配
bFMatcher.KnnMatch(descriptors2, matches, 2, null);
//寻找匹配结果中距离的最值
double min_dist = 100, max_dist = 0;
for(int i = 0; i < descriptors1.Rows;i++)
{
if(matches[i][0].Distance > max_dist)
{
max_dist = matches[i][0].Distance;
}
if(matches[i][0].Distance < min_dist)
{
min_dist = matches[i][0].Distance;
}
}
//对BF匹配结果进行筛选
VectorOfVectorOfDMatch good_matches = new VectorOfVectorOfDMatch();
for(int i = 0; i < matches.Size;i++)
{
//符合条件的匹配点进行存储
if(matches[i][0].Distance < 1.5 * min_dist)
{
good_matches.Push(matches[i]);
}
}
//绘制匹配点
Mat result = new Mat();
Features2DToolbox.DrawMatches(srcImg1, vkeyPoint1, srcImg2, vkeyPoint2, good_matches,
result, new MCvScalar(0, 255, 0), new MCvScalar(0, 0, 255));
//显示匹配结果
CvInvoke.Imshow("match-result", result);
CvInvoke.WaitKey(0);
}
}
}
FLANN匹配:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.Util;
using System.Drawing;
using Emgu.CV.XFeatures2D; //包含SIFT类
using Emgu.CV.Features2D; //包含Features2DToolBox类
using Emgu.CV.Flann; //包含快速最邻近匹配
namespace lesson33
{
class Program
{
static void Main(string[] args)
{
Mat srcImg1 = CvInvoke.Imread("11.jpg");
Mat srcImg2 = CvInvoke.Imread("22.jpg");
CvInvoke.Imshow("src1", srcImg1);
CvInvoke.Imshow("src2", srcImg2);
SIFT sift = new SIFT(1000);
//计算特征点
MKeyPoint[] keyPoints1 = sift.Detect(srcImg1);
MKeyPoint[] keyPoints2 = sift.Detect(srcImg2);
//绘制特征点
Mat sift_feature1 = new Mat();
Mat sift_feature2 = new Mat();
VectorOfKeyPoint vkeyPoint1 = new VectorOfKeyPoint(keyPoints1);
VectorOfKeyPoint vkeyPoint2 = new VectorOfKeyPoint(keyPoints2);
Features2DToolbox.DrawKeypoints(srcImg1, vkeyPoint1, sift_feature1, new Bgr(0, 255, 0),
Features2DToolbox.KeypointDrawType.Default);
Features2DToolbox.DrawKeypoints(srcImg2, vkeyPoint2, sift_feature2, new Bgr(0, 255, 0));
//显示绘制结果
CvInvoke.Imshow("sift_feature1", sift_feature1);
CvInvoke.Imshow("sift_feature2", sift_feature2);
//计算特征描述符
Mat descriptors1 = new Mat();
Mat descriptors2 = new Mat();
sift.Compute(srcImg1, vkeyPoint1, descriptors1);
sift.Compute(srcImg2, vkeyPoint2, descriptors2);
//快速最邻近匹配Flann
IIndexParams id = new LinearIndexParams();
SearchParams sp = new SearchParams();
FlannBasedMatcher flannBasedMatcher = new FlannBasedMatcher(id, sp);
//添加模板
flannBasedMatcher.Add(descriptors1);
VectorOfVectorOfDMatch matches = new VectorOfVectorOfDMatch();
flannBasedMatcher.KnnMatch(descriptors2, matches, 2, null);
//筛选匹配结果
Mat mask = new Mat(matches.Size, 1, DepthType.Cv8U, 1);
mask.SetTo(new MCvScalar(255));
Features2DToolbox.VoteForUniqueness(matches, 0.3, mask); //去除重复匹配
Mat result = new Mat();
Features2DToolbox.DrawMatches(srcImg1, vkeyPoint1, srcImg2, vkeyPoint2, matches, result, new MCvScalar(0, 255, 0),
new MCvScalar(0, 0, 255), mask);
CvInvoke.Imshow("match-result", result);
CvInvoke.WaitKey(0);
}
}
}
效果
BF匹配:
Flann匹配: