Files
better-genshin-impact/BetterGenshinImpact/Core/Recognition/OpenCv/CommonExtension.cs
Takaranoao 20fe152630 尝试修复一些ROI越界 (#2808)
* fix: 修复多处 OpenCV ROI 越界导致的断言失败

在低分辨率(如 1280x720)下,多处 Rect 坐标计算未做边界保护,
直接传入 SubMat / new Mat(mat, rect) 时触发 OpenCV ROI 断言崩溃。

修复位置:
- Behaviours.cs: fishBoxRect 计算结果钳位到图像边界,修复钓鱼任务越界
- GridScreen.cs: PostProcess 中幻影格子(插值生成)越界时直接丢弃
- ImageRegion.cs: DeriveCrop 两个重载统一加入坐标钳位与有效性校验
- GetGridIconsTask.cs: CropResizeArtifactSetFilterGridIcon X/Y 坐标加非负保护
- GeniusInvokationControl.cs: 角色区域扩展和 HP 区域 Y 偏移各加边界保护

* chore: 为 AutoFishingTask 鱼饵图标裁剪补充说明注释

* refactor: 提取 Rect 钳位逻辑为共享扩展方法 ClampTo

将 6 处重复的 ROI 钳位代码统一为 CommonExtension.ClampTo 扩展方法,
采用交集语义(坐标钳位时宽高同步缩减,不会扩大矩形)。
删除 AutoLeyLineOutcropTask 中的私有 ClampRect 方法。
2026-02-20 15:08:19 +08:00

111 lines
3.4 KiB
C#

using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using Vanara.PInvoke;
using Color = System.Windows.Media.Color;
using Point = OpenCvSharp.Point;
namespace BetterGenshinImpact.Core.Recognition.OpenCv;
public static class CommonExtension
{
public static unsafe Point AsCvPoint(this System.Drawing.Point point)
{
return *(Point*)&point;
}
public static unsafe System.Drawing.Point AsDrawingPoint(this Point point)
{
return *(System.Drawing.Point*)&point;
}
public static System.Windows.Point ToWindowsPoint(this Point point)
{
return new System.Windows.Point(point.X, point.Y);
}
public static unsafe Rect AsCvRect(this Rectangle rectangle)
{
return *(Rect*)&rectangle;
}
public static System.Windows.Rect ToWindowsRectangle(this Rect rect)
{
return new System.Windows.Rect(rect.X, rect.Y, rect.Width, rect.Height);
}
public static System.Windows.Rect ToWindowsRectangleOffset(this Rect rect, int offsetX, int offsetY)
{
return new System.Windows.Rect(rect.X + offsetX, rect.Y + offsetY, rect.Width, rect.Height);
}
public static unsafe Rectangle AsDrawingRectangle(this Rect rect)
{
return *(Rectangle*)▭
}
public static System.Drawing.Point GetCenterPoint(this Rectangle rectangle)
{
if (rectangle.IsEmpty) throw new ArgumentException("rectangle is empty");
return new System.Drawing.Point(rectangle.X + rectangle.Width / 2, rectangle.Y + rectangle.Height / 2);
}
public static Point GetCenterPoint(this RECT rectangle)
{
if (rectangle.IsEmpty) throw new ArgumentException("rectangle is empty");
return new Point(rectangle.X + rectangle.Width / 2, rectangle.Y + rectangle.Height / 2);
}
public static Point GetCenterPoint(this Rect rectangle)
{
if (rectangle == default) throw new ArgumentException("rectangle is empty");
return new Point(rectangle.X + rectangle.Width / 2, rectangle.Y + rectangle.Height / 2);
}
public static Rect Multiply(this Rect rect, double assetScale)
{
if (rect == default) throw new ArgumentException("rect is empty");
return new Rect((int)(rect.X * assetScale), (int)(rect.Y * assetScale), (int)(rect.Width * assetScale), (int)(rect.Height * assetScale));
}
public static Color ToWindowsColor(this System.Drawing.Color color)
{
return Color.FromArgb(color.A, color.R, color.G, color.B);
}
public static Point2d ToPoint2d(this Point2f p)
{
return new Point2d(p.X, p.Y);
}
public static List<Point2d> ToPoint2d(this List<Point2f> list)
{
return list.ConvertAll(ToPoint2d);
}
/// <summary>
/// 将矩形钳位到指定尺寸范围内(交集语义),防止 OpenCV ROI 越界
/// </summary>
public static Rect ClampTo(this Rect rect, int maxWidth, int maxHeight)
{
int x1 = Math.Clamp(rect.X, 0, maxWidth);
int y1 = Math.Clamp(rect.Y, 0, maxHeight);
int x2 = Math.Clamp(rect.X + rect.Width, 0, maxWidth);
int y2 = Math.Clamp(rect.Y + rect.Height, 0, maxHeight);
return new Rect(x1, y1, x2 - x1, y2 - y1);
}
/// <summary>
/// 将矩形钳位到 Mat 范围内(交集语义),防止 OpenCV ROI 越界
/// </summary>
public static Rect ClampTo(this Rect rect, Mat mat)
{
return rect.ClampTo(mat.Cols, mat.Rows);
}
}