C#中OpenCvSharp 通过特征点匹配图片的方法是一种流行的计算机视觉技术,可以用于图像识别和图像匹配。下面是详细的攻略步骤:
1. 安装OpenCvSharp
首先需要在C#项目中安装OpenCvSharp库,可以使用nuget包管理器进行安装,或下载并手动添加OpenCvSharp.dll和OpenCvSharpExtern.dll的引用。
2. 加载图片
使用Cv2.ImRead加载需要匹配的两张图片,并转化为灰度图像。
Mat img1 = Cv2.ImRead("img1.jpg", ImreadModes.GrayScale);
Mat img2 = Cv2.ImRead("img2.jpg", ImreadModes.GrayScale);
3. 计算特征点
通过SURF算法计算图片中的特征点,并画出特征点位置。
SURF surf = SURF.Create(500, 4, 2, false, false);
KeyPoint[] kp1, kp2;
Mat des1 = new Mat(), des2 = new Mat();
surf.DetectAndCompute(img1, null, out kp1, des1);
surf.DetectAndCompute(img2, null, out kp2, des2);
Mat imgKp1 = new Mat(), imgKp2 = new Mat();
Cv2.DrawKeypoints(img1, kp1, imgKp1);
Cv2.DrawKeypoints(img2, kp2, imgKp2);
4. 特征点匹配
使用BruteForceMatcher进行特征点匹配
var matcher = new BFMatcher(NormTypes.L2, false);
DMatch[] rawMatches = matcher.Match(des1, des2);
5. 进行筛选
为了提高匹配精度,需要对匹配结果进行筛选。可以使用Ratio Test算法或其他方法进行筛选,在这里我们使用Ratio Test算法。
List<DMatch> goodMatches = new List<DMatch>();
double threshold = 0.7;
for (int i = 0; i < rawMatches.Length; i++)
{
if (rawMatches[i].Distance < threshold * rawMatches[i+1].Distance)
{
goodMatches.Add(rawMatches[i]);
}
}
6. 显示匹配结果
将匹配结果绘制在一张图像上,可以使用Cv2.DrawMatches函数。
Mat imgMatches = new Mat();
Cv2.DrawMatches(img1, kp1, img2, kp2, goodMatches, imgMatches);
至此一个简单的特征点匹配的代码实现就完成了。下面是一个完整的示例代码:
using OpenCvSharp;
using System.Collections.Generic;
//加载图片
Mat img1 = Cv2.ImRead("img1.jpg", ImreadModes.GrayScale);
Mat img2 = Cv2.ImRead("img2.jpg", ImreadModes.GrayScale);
//计算特征点
SURF surf = SURF.Create(500, 4, 2, false, false);
KeyPoint[] kp1, kp2;
Mat des1 = new Mat(), des2 = new Mat();
surf.DetectAndCompute(img1, null, out kp1, des1);
surf.DetectAndCompute(img2, null, out kp2, des2);
Mat imgKp1 = new Mat(), imgKp2 = new Mat();
Cv2.DrawKeypoints(img1, kp1, imgKp1);
Cv2.DrawKeypoints(img2, kp2, imgKp2);
//特征点匹配
var matcher = new BFMatcher(NormTypes.L2, false);
DMatch[] rawMatches = matcher.Match(des1, des2);
//筛选
List<DMatch> goodMatches = new List<DMatch>();
double threshold = 0.7;
for (int i = 0; i < rawMatches.Length; i++)
{
if (rawMatches[i].Distance < threshold * rawMatches[i + 1].Distance)
{
goodMatches.Add(rawMatches[i]);
}
}
//显示匹配结果
Mat imgMatches = new Mat();
Cv2.DrawMatches(img1, kp1, img2, kp2, goodMatches, imgMatches);
下面是另一个示例,用SURF算法识别物体,实现简单的物体检测:
using OpenCvSharp;
//加载模板和待检测图片
Mat tmpl = Cv2.ImRead("template.jpg", ImreadModes.GrayScale);
Mat img = Cv2.ImRead("image.jpg", ImreadModes.GrayScale);
//计算特征点和描述子
SURF detector = SURF.Create(1000, 4, 2, false, false);
KeyPoint[] tmplKpts, imgKpts;
Mat tmplDesc, imgDesc;
detector.DetectAndCompute(tmpl, null, out tmplKpts, tmplDesc);
detector.DetectAndCompute(img, null, out imgKpts, imgDesc);
//特征点匹配
var matcher = new BFMatcher(NormTypes.L2, false);
DMatch[] rawMatches = matcher.Match(tmplDesc, imgDesc);
//筛选匹配点
List<KeyPoint> matchedKeypoints1 = new List<KeyPoint>();
List<KeyPoint> matchedKeypoints2 = new List<KeyPoint>();
double minDistance = 150;
for (int i = 0; i < rawMatches.Length; i++)
{
DMatch rawMatch = rawMatches[i];
if (rawMatch.Distance <= minDistance)
{
matchedKeypoints1.Add(tmplKpts[rawMatch.QueryIdx]);
matchedKeypoints2.Add(imgKpts[rawMatch.TrainIdx]);
}
}
//绘制匹配的点
Mat imgMatches = new Mat();
Cv2.DrawMatches(tmpl, tmplKpts, img, imgKpts, rawMatches, imgMatches);
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#中OpenCvSharp 通过特征点匹配图片的方法 - Python技术站