SIFT(Scale-Invariant Feature Transform)特征,即尺度不变特征变换,是一种计算机视觉的特征提取算法,用来侦测与描述图像中的局部性特征。
实质上,它是在不同的尺度空间上查找关键点(特征点),并计算出关键点的方向。SIFT所查找到的关键点是一些十分突出、不会因光照、仿射变换和噪音等因素而变化的点,如角点、边缘点、暗区的亮点及亮区的暗点等。
SURF特征简介SURF(Speeded Up Robust Features, 加速稳健特征) 是一种稳健的图像识别和描述算法。它是SIFT的高效变种,也是提取尺度不变特征,算法步骤与SIFT算法大致相同,但采用的方法不一样,要比SIFT算法更高效(正如其名)。SURF使用海森(Hesseian)矩阵的行列式值作特征点检测并用积分图加速运算;SURF 的描述子基于 2D 离散小波变换响应并且有效地利用了积分图。
SIFT匹配效果图片源自网络侵删
SURF匹配效果图片源自网络侵删
代码using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
namespace OpenCvSharp_Demo
{
public partial class frmMain : Form
{
publicfrmMain
{
InitializeComponent;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Mat matSrc = new Mat("1.jpg");
Mat matTo = new Mat("2.jpg");
var outMat = MatchPicBySift(matSrc, matTo);
pictureBox2.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(outMat);
}
private void button1_Click(object sender, EventArgs e)
{
Mat matSrc = new Mat("1.jpg");
Mat matTo = new Mat("2.jpg");
var outMat = MatchPicBySurf(matSrc, matTo, 10);
pictureBox2.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(outMat);
}
public Point2d Point2fToPoint2d(Point2f point) => new Point2d((double)point.X, (double)point.Y);
public Mat MatchPicBySift(Mat matSrc, Mat matTo)
{
using (Mat matSrcRet = new Mat)
using (Mat matToRet = new Mat)
{
KeyPoint keyPointsSrc, keyPointsTo;
using (var sift = OpenCvSharp.Features2D.SIFT.Create)
{
sift.DetectAndCompute(matSrc, , out keyPointsSrc, matSrcRet);
sift.DetectAndCompute(matTo, , out keyPointsTo, matToRet);
}
using (var bfMatcher = new OpenCvSharp.BFMatcher)
{
var matches = bfMatcher.KnnMatch(matSrcRet, matToRet, k: 2);
var pointsSrc = new List<Point2f>;
var pointsDst = new List<Point2f>;
var goodMatches = new List<DMatch>;
foreach (DMatch items in matches.Where(x => x.Length > 1))
{
if (items[0].Distance < 0.5 * items[1].Distance)
{
pointsSrc.Add(keyPointsSrc[items[0].QueryIdx].Pt);
pointsDst.Add(keyPointsTo[items[0].TrainIdx].Pt);
goodMatches.Add(items[0]);
Console.WriteLine($"{keyPointsSrc[items[0].QueryIdx].Pt.X}, {keyPointsSrc[items[0].QueryIdx].Pt.Y}");
}
}
var outMat = new Mat;
// 算法RANSAC对匹配的结果做过滤
var pSrc = pointsSrc.ConvertAll(Point2fToPoint2d);
var pDst = pointsDst.ConvertAll(Point2fToPoint2d);
var outMask = new Mat;
// 如果原始的匹配结果为空, 则跳过过滤步骤
if (pSrc.Count > 0 && pDst.Count > 0)
Cv2.FindHomography(pSrc, pDst, HomographyMethods.Ransac, mask: outMask);
// 如果通过RANSAC处理后的匹配点大于10个,才应用过滤. 否则使用原始的匹配点结果(匹配点过少的时候通过RANSAC处理后,可能会得到0个匹配点的结果).
if (outMask.Rows > 10)
{
byte maskBytes = new byte[outMask.Rows * outMask.Cols];
outMask.GetArray(out maskBytes);
Cv2.DrawMatches(matSrc, keyPointsSrc, matTo, keyPointsTo, goodMatches, outMat, matchesMask: maskBytes, flags: DrawMatchesFlags.NotDrawSinglePoints);
}
else
Cv2.DrawMatches(matSrc, keyPointsSrc, matTo, keyPointsTo, goodMatches, outMat, flags: DrawMatchesFlags.NotDrawSinglePoints);
return outMat;
}
}
}
public Mat MatchPicBySurf(Mat matSrc, Mat matTo, double threshold = 400)
{
using (Mat matSrcRet = new Mat)
using (Mat matToRet = new Mat)
{
KeyPoint keyPointsSrc, keyPointsTo;
using (var surf = OpenCvSharp.XFeatures2D.SURF.Create(threshold, 4, 3, true, true))
{
surf.DetectAndCompute(matSrc, , out keyPointsSrc, matSrcRet);
surf.DetectAndCompute(matTo, , out keyPointsTo, matToRet);
}
using (var flnMatcher = new OpenCvSharp.FlannBasedMatcher)
{
var matches = flnMatcher.Match(matSrcRet, matToRet);
//求最小最大距离
double minDistance = 1000;//反向逼近
double maxDistance = 0;
for (int i = 0; i < matSrcRet.Rows; i )
{
double distance = matches[i].Distance;
if (distance > maxDistance)
{
maxDistance = distance;
}
if (distance < minDistance)
{
minDistance = distance;
}
}
Console.WriteLine($"max distance : {maxDistance}");
Console.WriteLine($"min distance : {minDistance}");
var pointsSrc = new List<Point2f>;
var pointsDst = new List<Point2f>;
//筛选较好的匹配点
var goodMatches = new List<DMatch>;
for (int i = 0; i < matSrcRet.Rows; i )
{
double distance = matches[i].Distance;
if (distance < Math.Max(minDistance * 2, 0.02))
{
pointsSrc.Add(keyPointsSrc[matches[i].QueryIdx].Pt);
pointsDst.Add(keyPointsTo[matches[i].TrainIdx].Pt);
//距离小于范围的压入新的DMatch
goodMatches.Add(matches[i]);
}
}
var outMat = new Mat;
// 算法RANSAC对匹配的结果做过滤
var pSrc = pointsSrc.ConvertAll(Point2fToPoint2d);
var pDst = pointsDst.ConvertAll(Point2fToPoint2d);
var outMask = new Mat;
// 如果原始的匹配结果为空, 则跳过过滤步骤
if (pSrc.Count > 0 && pDst.Count > 0)
Cv2.FindHomography(pSrc, pDst, HomographyMethods.Ransac, mask: outMask);
// 如果通过RANSAC处理后的匹配点大于10个,才应用过滤. 否则使用原始的匹配点结果(匹配点过少的时候通过RANSAC处理后,可能会得到0个匹配点的结果).
if (outMask.Rows > 10)
{
byte maskBytes = new byte[outMask.Rows * outMask.Cols];
outMask.GetArray(out maskBytes);
Cv2.DrawMatches(matSrc, keyPointsSrc, matTo, keyPointsTo, goodMatches, outMat, matchesMask: maskBytes, flags: DrawMatchesFlags.NotDrawSinglePoints);
}
else
Cv2.DrawMatches(matSrc, keyPointsSrc, matTo, keyPointsTo, goodMatches, outMat, flags: DrawMatchesFlags.NotDrawSinglePoints);
return outMat;
}
}
}
}
}
Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved