匹配绑定窗口图片6 - MatchWindowsThresholdFromPath
函数简介
在绑定窗口的指定区域内,通过二值化处理后匹配符合模板图片的坐标。此函数支持多模板匹配、旋转匹配和缩放匹配,返回相对绑定窗口的坐标位置。适用于需要精确图像识别的场景,如游戏自动化、界面测试等。
当x1, y1, x2, y2参数都传0时,将匹配窗口整个客户区。
接口名称
MatchWindowsThresholdFromPath
DLL调用
long MatchWindowsThresholdFromPath(long ola, int x1, int y1, int x2, int y2, string colorJson, string templ, double matchVal, double angle, double scale)
参数定义:
ola
(长整型数): OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。x1
(整型数): 查找区域的左上角X坐标y1
(整型数): 查找区域的左上角Y坐标x2
(整型数): 查找区域的右下角X坐标y2
(整型数): 查找区域的右下角Y坐标colorJson
(字符串): 颜色列表的JSON字符串,格式如下:[ { "StartColor": "3278FA", // 起始颜色(RRGGBB格式) "EndColor": "6496FF", // 结束颜色(RRGGBB格式) "Type": 0 // 匹配类型:0-普通模式取合集,1-反色模式取合集,2-普通模式取交集,3-反色模式取交集 } ]
templ
(字符串): 模板图片的路径,支持多个图片,用"|"分隔,如"test.bmp|test2.bmp|test3.bmp"matchVal
(双精度浮点数): 相似度阈值,范围0-1,如0.85表示85%相似度angle
(双精度浮点数): 旋转角度,每次匹配后旋转指定角度继续匹配,直到匹配成功。角度越小匹配次数越多,时间越长。0表示不旋转,速度最快scale
(双精度浮点数): 窗口缩放比例,默认为1。可通过 GetScaleFromWindows 接口获取当前窗口缩放比例
示例:
// 定义颜色范围
string colorJson = R"([
{
"StartColor": "3278FA",
"EndColor": "6496FF",
"Type": 0
},
{
"StartColor": "FF0000",
"EndColor": "FF3333",
"Type": 1
}
])";
// 匹配图片
long resultPtr = MatchWindowsThresholdFromPath(ola, 0, 0, 800, 600, colorJson, "templates/button.bmp", 0.85, 45.0, 1.0);
if (resultPtr != 0) {
string result = GetStringFromPtr(resultPtr);
printf("匹配结果: %s\n", result.c_str());
// 解析JSON结果
// {
// "MatchVal": 0.85, // 数据相似度
// "MatchState": true, // 是否大于指定精度
// "Index": 0, // 多图识别时的返回索引
// "Angle": 45.0, // 识别结果角度
// "MatchPoint": { // 匹配点坐标
// "x": 100,
// "y": 200
// }
// }
// 释放字符串内存
FreeStringPtr(ola, resultPtr);
}
COM调用
string MatchWindowsThresholdFromPath(int x1, int y1, int x2, int y2, string colorJson, string templ, double matchVal, double angle, double scale)
参数定义:
参数定义同DLL调用
示例:
# 定义颜色范围
colorJson = '''[
{
"StartColor": "3278FA",
"EndColor": "6496FF",
"Type": 0
},
{
"StartColor": "FF0000",
"EndColor": "FF3333",
"Type": 1
}
]'''
# 匹配图片
result = ola.MatchWindowsThresholdFromPath(0, 0, 800, 600, colorJson, "templates/button.bmp", 0.85, 45.0, 1.0)
print(f"匹配结果: {result}")
# 解析JSON结果
# {
# "MatchVal": 0.85, # 数据相似度
# "MatchState": true, # 是否大于指定精度
# "Index": 0, # 多图识别时的返回索引
# "Angle": 45.0, # 识别结果角度
# "MatchPoint": { # 匹配点坐标
# "x": 100,
# "y": 200
# }
# }
返回值
字符串: 返回JSON格式的匹配结果,包含以下字段:
MatchVal
: 数据相似度,范围0-1MatchState
: 是否大于指定精度,用于快速判断识别结果Index
: 多图识别时的返回索引Angle
: 识别结果角度MatchPoint
: 匹配点坐标,包含x和y值
注意事项
- 颜色值必须使用RRGGBB格式的十六进制字符串
- 支持多个颜色范围的指定,每个范围可以设置不同的匹配类型
- 颜色匹配类型说明:
- 0: 正常匹配,保留在颜色范围内的像素
- 1: 反色匹配,保留在颜色范围外的像素
- 2: 正常交集匹配,保留在颜色范围内的像素取交集
- 3: 反色交集匹配,保留在颜色范围外的像素取交集
- 模板图片路径支持多个图片,用"|"分隔
- 相似度阈值范围必须在0-1之间
- 旋转角度越小,匹配次数越多,耗时越长
- 窗口缩放比例必须与实际情况相符
- DLL调用时,返回的字符串指针需要调用 FreeStringPtr 接口释放内存
- 建议在使用前检查参数的有效性
- 处理大图片时注意性能影响
- 返回的坐标是相对于绑定窗口的坐标
- 如果匹配失败,MatchState将为false