查找指定颜色范围坐标 - FindMultiColorList
函数简介
查找指定区域内符合指定颜色范围的所有颜色点。此函数可以在指定区域内搜索所有符合特定颜色范围的像素点,并返回它们的坐标列表。适用于需要批量查找颜色点的场景,如区域分析、图像特征提取等。
ColorModel:
颜色每个通道单独计算范围,如颜色范围位3278FA,6496FF,实际对应R(50~100) G(120 ~150) B(250 ~255)
包含下限(>= color1) 包含上限(<= color2)
支持ARGB模式如#FFFFFFFF
支持反色模式,交集并集查询颜色
- 0: 正常匹配,保留在颜色范围内的像素
- 1: 反色匹配,保留在颜色范围外的像素
- 2: 正常交集匹配,保留在颜色范围内的像素取交集
- 3: 反色交集匹配,保留在颜色范围外的像素取交集 如{"StartColor": "3278FA", "EndColor": "6496FF","Type":0}
x1 , y1, x2, y2传 0, 0, 0, 0 为窗口整个客户区
返回数据为相对窗口坐标
接口名称
FindMultiColorList
DLL调用
long FindMultiColorList(long ola, int x1, int y1, int x2, int y2, string colorList)
参数定义:
ola
(长整型数): OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。x1
(整型数): 查找区域的左上角X坐标y1
(整型数): 查找区域的左上角Y坐标x2
(整型数): 查找区域的右下角X坐标y2
(整型数): 查找区域的右下角Y坐标colorList
(字符串): 颜色列表的json字符串,如: [{"StartColor": "3278FA", "EndColor": "6496FF","Type":0}, {"StartColor": "3278FA", "EndColor": "6496FF","Type":1}]
示例:
// 在窗口客户区查找所有指定颜色点
string colorList = "[{\"StartColor\": \"3278FA\", \"EndColor\": \"6496FF\", \"Type\": 0}]";
long result = FindMultiColorList(ola, 0, 0, 0, 0, colorList);
if (result != 0) {
// 解析返回的JSON字符串
// 注意:使用完后需要调用FreeStringPtr释放内存
printf("找到颜色点列表:%s\n", result);
FreeStringPtr(ola, result);
} else {
printf("未找到颜色点\n");
}
// 在指定区域查找所有白色点
string colorList = "[{\"StartColor\": \"FFFFFF\", \"EndColor\": \"FFFFFF\", \"Type\": 0}]";
long result = FindMultiColorList(ola, 100, 100, 200, 200, colorList);
if (result != 0) {
printf("找到白色点列表:%s\n", result);
FreeStringPtr(ola, result);
} else {
printf("未找到白色点\n");
}
COM调用
string FindMultiColorList(int x1, int y1, int x2, int y2, string colorList)
参数定义:
x1
(整型数): 查找区域的左上角X坐标y1
(整型数): 查找区域的左上角Y坐标x2
(整型数): 查找区域的右下角X坐标y2
(整型数): 查找区域的右下角Y坐标colorList
(字符串): 颜色列表的json字符串,如: [{"StartColor": "3278FA", "EndColor": "6496FF","Type":0}, {"StartColor": "3278FA", "EndColor": "6496FF","Type":1}]
示例:
# 在窗口客户区查找所有指定颜色点
colorList = '[{"StartColor": "3278FA", "EndColor": "6496FF", "Type": 0}]'
result = ola.FindMultiColorList(0, 0, 0, 0, colorList)
if result:
# 解析返回的JSON字符串
points = json.loads(result)
for point in points:
messagebox("找到颜色点,坐标:(" + str(point["x"]) + ", " + str(point["y"]) + ")")
else:
messagebox("未找到颜色点")
# 在指定区域查找所有白色点
colorList = '[{"StartColor": "FFFFFF", "EndColor": "FFFFFF", "Type": 0}]'
result = ola.FindMultiColorList(100, 100, 200, 200, colorList)
if result:
points = json.loads(result)
for point in points:
messagebox("找到白色点,坐标:(" + str(point["x"]) + ", " + str(point["y"]) + ")")
else:
messagebox("未找到白色点")
返回值
字符串: 返回识别到的坐标点列表的JSON字符串,格式如下:
[
{
"x": 1,
"y": 2
},
{
"x": 2,
"y": 1
}
]
注意事项
- 颜色范围使用RRGGBB格式,每个通道单独计算范围
- 支持ARGB模式,如#FFFFFFFF
- 颜色匹配类型说明:
- 0: 正常匹配,保留在颜色范围内的像素
- 1: 反色匹配,保留在颜色范围外的像素
- 2: 正常交集匹配,保留在颜色范围内的像素取交集
- 3: 反色交集匹配,保留在颜色范围外的像素取交集
- 当x1,y1,x2,y2都传0时,会查找整个窗口客户区
- 返回的坐标是相对于窗口客户区的坐标
- 返回的JSON字符串需要解析后才能使用
- DLL调用返回的字符串指针需要调用 FreeStringPtr 接口释放内存
- 颜色范围越大,匹配的像素点越多,但可能会影响查找精度
- 如果区域内符合条件的点较多,返回的JSON字符串可能会很长,请注意内存使用
- 使用反色模式时,会查找不在指定颜色范围内的点
- 使用交集模式时,需要同时满足所有颜色条件
- 使用并集模式时,满足任一颜色条件即可