using BetterGenshinImpact.Core.Recognition.OpenCv;
using BetterGenshinImpact.Helpers.Extensions;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
namespace BetterGenshinImpact.Core.Recognition;
///
/// 识别对象
///
[Serializable]
public class RecognitionObject
{
public RecognitionTypes RecognitionType { get; set; }
///
/// 感兴趣的区域
///
public Rect RegionOfInterest { get; set; }
public string? Name { get; set; }
#region 模板匹配
///
/// 模板匹配的对象(彩色)
///
public Mat? TemplateImageMat { get; set; }
///
/// 模板匹配的对象(灰色)
///
public Mat? TemplateImageGreyMat { get; set; }
///
/// 模板匹配阈值。可选,默认 0.8 。
///
public double Threshold { get; set; } = 0.8;
///
/// 是否使用 3 通道匹配。可选,默认 false 。
///
public bool Use3Channels { get; set; } = false;
///
/// 模板匹配算法。可选,默认 CCoeffNormed 。
/// https://docs.opencv.org/4.x/df/dfb/group__imgproc__object.html
///
public TemplateMatchModes TemplateMatchMode { get; set; } = TemplateMatchModes.CCoeffNormed;
///
/// 匹配模板遮罩,指定图片中的某种色彩不需要匹配
/// 使用时,需要将模板图片的背景色设置为纯绿色,即 (0, 255, 0)
///
public bool UseMask { get; set; } = false;
///
/// 不需要匹配的颜色,默认绿色
/// UseMask = true 的时候有用
///
public Color MaskColor { get; set; } = Color.FromArgb(0, 255, 0);
public Mat? MaskMat { get; set; }
///
/// 匹配成功时,是否在屏幕上绘制矩形框。可选,默认 false 。
/// true 时 Name 必须有值。
///
public bool DrawOnWindow { get; set; } = false;
///
/// DrawOnWindow 为 true 时,绘制的矩形框的颜色。可选,默认红色。
///
public Pen DrawOnWindowPen = new(Color.Red, 2);
///
/// 一个模板匹配多个结果的时候最大匹配数量。可选,默认 -1,即不限制。
///
public int MaxMatchCount { get; set; } = -1;
public RecognitionObject InitTemplate()
{
if (TemplateImageMat != null && TemplateImageGreyMat == null)
{
TemplateImageGreyMat = new Mat();
Cv2.CvtColor(TemplateImageMat, TemplateImageGreyMat, ColorConversionCodes.BGR2GRAY);
}
if (UseMask && TemplateImageMat != null && MaskMat == null) MaskMat = OpenCvCommonHelper.CreateMask(TemplateImageMat, MaskColor.ToScalar());
return this;
}
#endregion 模板匹配
#region 颜色匹配
///
/// 颜色匹配方式。即 cv::ColorConversionCodes。可选,默认 4 (RGB)。
/// 常用值:4 (RGB, 3 通道), 40 (HSV, 3 通道), 6 (GRAY, 1 通道)。
/// https://docs.opencv.org/4.x/d8/d01/group__imgproc__color__conversions.html
///
public ColorConversionCodes ColorConversionCode { get; set; } = ColorConversionCodes.BGR2RGB;
public Scalar LowerColor { get; set; }
public Scalar UpperColor { get; set; }
///
/// 符合的点的数量要求。可选,默认 1
///
public int MatchCount { get; set; } = 1;
#endregion 颜色匹配
#region OCR文字识别
///
/// OCR 引擎。可选,只有 Paddle。
///
public OcrEngineTypes OcrEngine { get; set; } = OcrEngineTypes.Paddle;
///
/// 部分文字识别结果不准确,进行替换。可选。
///
public Dictionary ReplaceDictionary { get; set; } = [];
///
/// 包含匹配
/// 多个值全匹配的情况下才算成功
/// 复杂情况请用下面的正则匹配
///
public List AllContainMatchText { get; set; } = [];
///
/// 包含匹配
/// 一个值匹配就算成功
///
public List OneContainMatchText { get; set; } = [];
///
/// 正则匹配
/// 多个值全匹配的情况下才算成功
///
public List RegexMatchText { get; set; } = [];
public static RecognitionObject Ocr(double x, double y, double w, double h)
{
return new RecognitionObject
{
RecognitionType = RecognitionTypes.Ocr,
RegionOfInterest = new Rect((int)Math.Round(x), (int)Math.Round(y), (int)Math.Round(w), (int)Math.Round(h))
};
}
public static RecognitionObject Ocr(Rect rect)
{
return new RecognitionObject
{
RecognitionType = RecognitionTypes.Ocr,
RegionOfInterest = rect
};
}
public static RecognitionObject OcrThis = new()
{
RecognitionType = RecognitionTypes.Ocr
};
#endregion OCR文字识别
}