Files
better-genshin-impact/BetterGenshinImpact/Core/Recognition/OpenCv/TemplateMatch/TemplateMatchHelper.cs
Juemin Lin 4b5f229006 分层地图的模板匹配坐标识别以及相机视角识别优化和小地图预处理 (#1730)
* 小地图预处理和视角识别算法优化

* 模板匹配的相关类,包括快速带遮罩的SqDiff模板匹配,模板匹配归一化类,简易亚像素模板匹配实现,小地图匹配相关配置,和小地图匹配上下文。

* 实现小地图的分层地图模板匹配,修改 SceneBaseMap 的 GetMiniMapPosition 为 virtual 以便继承覆盖。
2025-06-24 10:39:16 +08:00

46 lines
1.6 KiB
C#

using OpenCvSharp;
namespace BetterGenshinImpact.Core.Recognition.OpenCv.TemplateMatch;
public static class TemplateMatchHelper
{
public static (Point, double) MatchTemplate(Mat image, Mat template, TemplateMatchModes method, Mat? mask = null)
{
using var result = new Mat();
Cv2.MatchTemplate(image,template, result, method, mask);
Point loc;
double val;
if (method is TemplateMatchModes.SqDiff or TemplateMatchModes.SqDiffNormed)
{
Cv2.MinMaxLoc(result, out val, out _, out loc, out _);
}
else
{
Cv2.MinMaxLoc(result, out _, out val, out _, out loc);
}
return (loc, val);
}
public static (Point, double) MatchTemplateNormalized(Mat image, Mat template, TemplateMatchModes method, Mat? mask = null)
{
var normalizer = new TemplateMatchNormalizer(template, mask, method);
(var loc, normalizer.Value) = MatchTemplate(image, template, method, mask);
return (loc, normalizer.Confidence());
}
public static (Point2f, double) MatchTemplateSubPix(Mat image, Mat template, TemplateMatchModes method, Mat? mask)
{
using var result = new Mat();
Cv2.MatchTemplate(image,template, result, method, mask);
Point loc;
double val;
if (method is TemplateMatchModes.SqDiff or TemplateMatchModes.SqDiffNormed)
{
Cv2.MinMaxLoc(result, out val, out _, out loc, out _);
}
else
{
Cv2.MinMaxLoc(result, out _, out val, out _, out loc);
}
return (SubPixMatch.Fit(result, loc), val);
}
}