Files
better-genshin-impact/Test/BetterGenshinImpact.UnitTest/CoreTests/RecognitionTests/RectClampTests.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

39 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using BetterGenshinImpact.Core.Recognition.OpenCv;
using OpenCvSharp;
namespace BetterGenshinImpact.UnitTest.CoreTests.RecognitionTests;
public class RectClampTests
{
[Theory]
[InlineData(10, 10, 50, 50, 200, 200, 10, 10, 50, 50)] // 完全在范围内
[InlineData(-10, 20, 100, 50, 200, 200, 0, 20, 90, 50)] // 左侧越界
[InlineData(20, -15, 50, 100, 200, 200, 20, 0, 50, 85)] // 上方越界
[InlineData(150, 10, 100, 50, 200, 200, 150, 10, 50, 50)] // 右侧越界
[InlineData(10, 150, 50, 100, 200, 200, 10, 150, 50, 50)] // 下方越界
[InlineData(-10, -20, 300, 400, 100, 100, 0, 0, 100, 100)] // 四边都越界
[InlineData(-100, 10, 50, 50, 200, 200, 0, 10, 0, 50)] // 完全在外宽为0
[InlineData(0, 0, 10, 10, 0, 0, 0, 0, 0, 0)] // 零尺寸图像
public void ClampTo_IntOverload_ReturnsExpected(
int x, int y, int w, int h,
int maxW, int maxH,
int ex, int ey, int ew, int eh)
{
var rect = new Rect(x, y, w, h);
var result = rect.ClampTo(maxW, maxH);
Assert.Equal(new Rect(ex, ey, ew, eh), result);
}
[Fact]
public void ClampTo_MatOverload_MatchesIntOverload()
{
var rect = new Rect(-10, -5, 100, 80);
using var mat = new Mat(200, 200, MatType.CV_8UC3);
var result = rect.ClampTo(mat);
Assert.Equal(rect.ClampTo(mat.Cols, mat.Rows), result);
Assert.Equal(new Rect(0, 0, 90, 75), result);
}
}