主题
识别图片排除指定区域 - ExcludePos
函数简介
排除掉指定区域的识别结果,用于颜色识别结果及图像识别结果的过滤。
接口名称
ExcludePosDLL调用
c
long ExcludePos(long ola, char* json, int type, int x1, int y1, int x2, int y2);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| ola | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| json | 字符串 | 识别返回的结果。 |
| type | 整数型 | 识别类型:1 颜色识别,2 图像识别。 |
| x1 | 整数型 | 排除区域左上角的X坐标。 |
| y1 | 整数型 | 排除区域左上角的Y坐标。 |
| x2 | 整数型 | 排除区域右下角的X坐标。 |
| y2 | 整数型 | 排除区域右下角的Y坐标。 |
输入 JSON 格式
json 参数需传入识别接口返回的 原始 JSON 字符串,格式须与 type 对应:
| type | 来源接口示例 | JSON 格式 |
|---|---|---|
1 颜色识别 | FindColorListEx | [{"X":100,"Y":200},{"X":150,"Y":220}] |
2 图像识别 | FindImageFromPtrAll 等 | [{"MatchVal":0.95,"MatchState":true,"Index":0,"Angle":0,"X":100,"Y":200,"Width":32,"Height":32}] |
SDK 注意:上述识别接口在 SDK 中会将 JSON 自动反序列化 为模型对象(如
Point/MatchResult、Point2D/MatchData等)。调用本接口前须 再序列化回 JSON 字符串,推荐使用 SDK 模型上的ToJSON()/toJSON()方法(列表可逐元素序列化后拼接为 JSON 数组)。原生 DLL 调用则可直接使用识别接口返回的 JSON,无需转换。
示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
std::string filtered = ola.ExcludePos("[{\"MatchVal\":0.95,\"MatchState\":true,\"Index\":0,\"Angle\":0,\"X\":100,\"Y\":200,\"Width\":32,\"Height\":32},{\"MatchVal\":0.88,\"MatchState\":true,\"Index\":1,\"Angle\":0,\"X\":150,\"Y\":220,\"Width\":32,\"Height\":32},{\"MatchVal\":0.80,\"MatchState\":true,\"Index\":2,\"Angle\":0,\"X\":300,\"Y\":100,\"Width\":32,\"Height\":32}]", 2, 90, 190, 160, 230);csharp
using OLAPlug;
var ola = new OLAPlugServer();
string filtered = ola.ExcludePos(@"[{"MatchVal":0.95,"MatchState":true,"Index":0,"Angle":0,"X":100,"Y":200,"Width":32,"Height":32},{"MatchVal":0.88,"MatchState":true,"Index":1,"Angle":0,"X":150,"Y":220,"Width":32,"Height":32},{"MatchVal":0.80,"MatchState":true,"Index":2,"Angle":0,"X":300,"Y":100,"Width":32,"Height":32}]", 2, 90, 190, 160, 230);python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
filtered = ola.ExcludePos(r'[{"MatchVal":0.95,"MatchState":true,"Index":0,"Angle":0,"X":100,"Y":200,"Width":32,"Height":32},{"MatchVal":0.88,"MatchState":true,"Index":1,"Angle":0,"X":150,"Y":220,"Width":32,"Height":32},{"MatchVal":0.80,"MatchState":true,"Index":2,"Angle":0,"X":300,"Y":100,"Width":32,"Height":32}]', 2, 90, 190, 160, 230)java
import com.olaplug.OLAPlugServer;
OLAPlugServer ola = new OLAPlugServer();
String filtered = ola.ExcludePos("[{\"MatchVal\":0.95,\"MatchState\":true,\"Index\":0,\"Angle\":0,\"X\":100,\"Y\":200,\"Width\":32,\"Height\":32},{\"MatchVal\":0.88,\"MatchState\":true,\"Index\":1,\"Angle\":0,\"X\":150,\"Y\":220,\"Width\":32,\"Height\":32},{\"MatchVal\":0.80,\"MatchState\":true,\"Index\":2,\"Angle\":0,\"X\":300,\"Y\":100,\"Width\":32,\"Height\":32}]", 2, 90, 190, 160, 230);go
import "github.com/ola/olaplug/olaplug"
ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
filtered := ola.ExcludePos("[{\"MatchVal\":0.95,\"MatchState\":true,\"Index\":0,\"Angle\":0,\"X\":100,\"Y\":200,\"Width\":32,\"Height\":32},{\"MatchVal\":0.88,\"MatchState\":true,\"Index\":1,\"Angle\":0,\"X\":150,\"Y\":220,\"Width\":32,\"Height\":32},{\"MatchVal\":0.80,\"MatchState\":true,\"Index\":2,\"Angle\":0,\"X\":300,\"Y\":100,\"Width\":32,\"Height\":32}]", 2, 90, 190, 160, 230)rust
use olaplug::OLAPlugServer;
let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let filtered = ola.exclude_pos("[{\"MatchVal\":0.95, \"MatchState\":true, \"Index\":0, \"Angle\":0, \"X\":100, \"Y\":200, \"Width\":32, \"Height\":32}, {\"MatchVal\":0.88, \"MatchState\":true, \"Index\":1, \"Angle\":0, \"X\":150, \"Y\":220, \"Width\":32, \"Height\":32}, {\"MatchVal\":0.80, \"MatchState\":true, \"Index\":2, \"Angle\":0, \"X\":300, \"Y\":100, \"Width\":32, \"Height\":32}]", 2, 90, 190, 160, 230);cpp
var ola = com("OlaPlug.OlaSoft")
var filtered = ola.ExcludePos("[{\"MatchVal\":0.95,\"MatchState\":true,\"Index\":0,\"Angle\":0,\"X\":100,\"Y\":200,\"Width\":32,\"Height\":32},{\"MatchVal\":0.88,\"MatchState\":true,\"Index\":1,\"Angle\":0,\"X\":150,\"Y\":220,\"Width\":32,\"Height\":32},{\"MatchVal\":0.80,\"MatchState\":true,\"Index\":2,\"Angle\":0,\"X\":300,\"Y\":100,\"Width\":32,\"Height\":32}]", 2, 90, 190, 160, 230)vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
filtered = ola.ExcludePos("[{\"MatchVal\":0.95,\"MatchState\":true,\"Index\":0,\"Angle\":0,\"X\":100,\"Y\":200,\"Width\":32,\"Height\":32},{\"MatchVal\":0.88,\"MatchState\":true,\"Index\":1,\"Angle\":0,\"X\":150,\"Y\":220,\"Width\":32,\"Height\":32},{\"MatchVal\":0.80,\"MatchState\":true,\"Index\":2,\"Angle\":0,\"X\":300,\"Y\":100,\"Width\":32,\"Height\":32}]", 2, 90, 190, 160, 230)text
.局部变量 ola, OLAPlug
ola.创建 ()
filtered = ola.ExcludePos (“[{\“MatchVal\“:0.95,\“MatchState\“:true,\“Index\“:0,\“Angle\“:0,\“X\“:100,\“Y\“:200,\“Width\“:32,\“Height\“:32},{\“MatchVal\“:0.88,\“MatchState\“:true,\“Index\“:1,\“Angle\“:0,\“X\“:150,\“Y\“:220,\“Width\“:32,\“Height\“:32},{\“MatchVal\“:0.80,\“MatchState\“:true,\“Index\“:2,\“Angle\“:0,\“X\“:300,\“Y\“:100,\“Width\“:32,\“Height\“:32}]“, 2, 90, 190, 160, 230)aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var filtered = ola.ExcludePos("[{\"MatchVal\":0.95,\"MatchState\":true,\"Index\":0,\"Angle\":0,\"X\":100,\"Y\":200,\"Width\":32,\"Height\":32},{\"MatchVal\":0.88,\"MatchState\":true,\"Index\":1,\"Angle\":0,\"X\":150,\"Y\":220,\"Width\":32,\"Height\":32},{\"MatchVal\":0.80,\"MatchState\":true,\"Index\":2,\"Angle\":0,\"X\":300,\"Y\":100,\"Width\":32,\"Height\":32}]", 2, 90, 190, 160, 230);text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
文本型 filtered = ola.ExcludePos("[{\"MatchVal\":0.95,\"MatchState\":true,\"Index\":0,\"Angle\":0,\"X\":100,\"Y\":200,\"Width\":32,\"Height\":32},{\"MatchVal\":0.88,\"MatchState\":true,\"Index\":1,\"Angle\":0,\"X\":150,\"Y\":220,\"Width\":32,\"Height\":32},{\"MatchVal\":0.80,\"MatchState\":true,\"Index\":2,\"Angle\":0,\"X\":300,\"Y\":100,\"Width\":32,\"Height\":32}]", 2, 90, 190, 160, 230)cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
std::string filtered = ola.ExcludePos("[{\"MatchVal\":0.95,\"MatchState\":true,\"Index\":0,\"Angle\":0,\"X\":100,\"Y\":200,\"Width\":32,\"Height\":32},{\"MatchVal\":0.88,\"MatchState\":true,\"Index\":1,\"Angle\":0,\"X\":150,\"Y\":220,\"Width\":32,\"Height\":32},{\"MatchVal\":0.80,\"MatchState\":true,\"Index\":2,\"Angle\":0,\"X\":300,\"Y\":100,\"Width\":32,\"Height\":32}]", 2, 90, 190, 160, 230);原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
std::string filtered = ExcludePos("[{\"MatchVal\":0.95,\"MatchState\":true,\"Index\":0,\"Angle\":0,\"X\":100,\"Y\":200,\"Width\":32,\"Height\":32},{\"MatchVal\":0.88,\"MatchState\":true,\"Index\":1,\"Angle\":0,\"X\":150,\"Y\":220,\"Width\":32,\"Height\":32},{\"MatchVal\":0.80,\"MatchState\":true,\"Index\":2,\"Angle\":0,\"X\":300,\"Y\":100,\"Width\":32,\"Height\":32}]", 2, 90, 190, 160, 230);csharp
using System.Runtime.InteropServices;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
long instance = CreateCOLAPlugInterFace();
string filtered = ExcludePos(@"[{"MatchVal":0.95,"MatchState":true,"Index":0,"Angle":0,"X":100,"Y":200,"Width":32,"Height":32},{"MatchVal":0.88,"MatchState":true,"Index":1,"Angle":0,"X":150,"Y":220,"Width":32,"Height":32},{"MatchVal":0.80,"MatchState":true,"Index":2,"Angle":0,"X":300,"Y":100,"Width":32,"Height":32}]", 2, 90, 190, 160, 230);python
from ctypes import CDLL, c_int, c_int64
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
filtered = ola.ExcludePos(r'[{"MatchVal":0.95,"MatchState":true,"Index":0,"Angle":0,"X":100,"Y":200,"Width":32,"Height":32},{"MatchVal":0.88,"MatchState":true,"Index":1,"Angle":0,"X":150,"Y":220,"Width":32,"Height":32},{"MatchVal":0.80,"MatchState":true,"Index":2,"Angle":0,"X":300,"Y":100,"Width":32,"Height":32}]', 2, 90, 190, 160, 230)完整示例
以下示例演示:识别接口获取结果 → 转换为 JSON → 传入 ExcludePos → 解析过滤后的结果。
颜色识别:排除 HUD 区域
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
// 1. 找色:SDK 返回 vector<Point2D>
auto colorPoints = ola.FindColorListEx(0, 0, 0, 0, "[{\"StartColor\":\"FF0000\",\"EndColor\":\"FF0000\"}]");
if (colorPoints.empty()) return;
// 2. 使用 toJSON 序列化为 JSON 数组
std::string colorJson = "[";
for (size_t i = 0; i < colorPoints.size(); ++i) {
if (i > 0) colorJson += ",";
colorJson += ola.toJSON(colorPoints[i]);
}
colorJson += "]";
// 3. 排除左上角 (0,0)-(200,100) 区域内的点,type=1 表示颜色识别
std::string filteredJson = ola.ExcludePos(colorJson.c_str(), 1, 0, 0, 200, 100);
// 4. 解析过滤结果(格式与 FindColorListEx 相同)
int64_t filteredArr = ola.JsonParse(filteredJson.c_str(), &err);
if (filteredArr != 0 && err == 0) {
int32_t size = ola.JsonGetSize(filteredArr, &err);
for (int32_t i = 0; i < size; i++) {
int32_t itemErr = 0;
int64_t item = ola.JsonGetArrayItem(filteredArr, i, &itemErr);
int32_t getErr = 0;
int32_t x = (int32_t)ola.JsonGetNumber(item, "X", &getErr);
int32_t y = (int32_t)ola.JsonGetNumber(item, "Y", &getErr);
}
ola.JsonFree(filteredArr);
}csharp
using OLAPlug;
using Newtonsoft.Json;
using System.Linq;
var ola = new OLAPlugServer();
// 1. 找色:SDK 返回 List<Point>
var colorPoints = ola.FindColorListEx(0, 0, 0, 0, @"[{""StartColor"":""FF0000"",""EndColor"":""FF0000""}]");
if (colorPoints.Count == 0) return;
// 2. 使用 ToJSON 序列化回 JSON 数组
string colorJson = "[" + string.Join(",", colorPoints.Select(p => p.ToJSON())) + "]";
// 3. 排除指定区域
string filteredJson = ola.ExcludePos(colorJson, 1, 0, 0, 200, 100);
// 4. 解析过滤结果
var filteredPoints = JsonConvert.DeserializeObject<List<Point>>(filteredJson);
if (filteredPoints != null && filteredPoints.Count > 0)
{
int x = filteredPoints[0].X;
int y = filteredPoints[0].Y;
}python
import json
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
# 1. 找色:SDK 返回 List[dict],元素形如 {"X": 100, "Y": 200}
color_points = ola.FindColorListEx(0, 0, 0, 0, '[{"StartColor":"FF0000","EndColor":"FF0000"}]')
if not color_points:
return
# 2. 序列化回 JSON
color_json = json.dumps(color_points, ensure_ascii=False)
# 3. 排除指定区域
filtered_json = ola.ExcludePos(color_json, 1, 0, 0, 200, 100)
# 4. 解析过滤结果
filtered_points = json.loads(filtered_json)
if filtered_points:
x = filtered_points[0]["X"]
y = filtered_points[0]["Y"]java
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.olaplug.OLAPlugServer;
import com.olaplug.model.Point;
import java.util.List;
OLAPlugServer ola = new OLAPlugServer();
ObjectMapper mapper = new ObjectMapper();
// 1. 找色:SDK 返回 List<Point>
List<Point> colorPoints = ola.FindColorListEx(0, 0, 0, 0, "[{\"StartColor\":\"FF0000\",\"EndColor\":\"FF0000\"}]");
if (colorPoints == null || colorPoints.isEmpty()) return;
// 2. 使用 toJSON 序列化回 JSON 数组
String colorJson = colorPoints.stream()
.map(Point::toJSON)
.collect(java.util.stream.Collectors.joining(",", "[", "]"));
// 3. 排除指定区域
String filteredJson = ola.ExcludePos(colorJson, 1, 0, 0, 200, 100);
// 4. 解析过滤结果
List<Point> filteredPoints = mapper.readValue(filteredJson, new TypeReference<List<Point>>() {});
if (filteredPoints != null && !filteredPoints.isEmpty()) {
int x = filteredPoints.get(0).X;
int y = filteredPoints.get(0).Y;
}go
import (
"encoding/json"
"github.com/ola/olaplug/olaplug"
)
ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
colorPoints := ola.FindColorListEx(0, 0, 0, 0, `[{"StartColor":"FF0000","EndColor":"FF0000"}]`)
if len(colorPoints) == 0 {
return
}
colorJson, _ := json.Marshal(colorPoints)
filteredJson := ola.ExcludePos(string(colorJson), 1, 0, 0, 200, 100)
var filteredPoints []olaplug.Point2D
if json.Unmarshal([]byte(filteredJson), &filteredPoints) == nil && len(filteredPoints) > 0 {
x := filteredPoints[0].X
y := filteredPoints[0].Y
_ = x
_ = y
}rust
use olaplug::{OLAPlugServer, Point2D};
let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let color_points = ola.find_color_list_ex(0, 0, 0, 0, r#"[{"StartColor":"FF0000","EndColor":"FF0000"}]"#);
if color_points.is_empty() {
return;
}
let color_json = format!(
"[{}]",
color_points.iter().map(|p| p.toJSON()).collect::<Vec<_>>().join(",")
);
let filtered_json = ola.exclude_pos(&color_json, 1, 0, 0, 200, 100);
if let Ok(filtered_points) = serde_json::from_str::<Vec<Point2D>>(&filtered_json) {
if let Some(first) = filtered_points.first() {
let _ = (first.x, first.y);
}
}图像识别:排除顶部工具栏区域
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
long imgPtr = ola.LoadImage("scene.png");
long templPtr = ola.LoadImage("button.bmp");
if (imgPtr == 0 || templPtr == 0) return;
// 1. 找图:SDK 返回 vector<MatchData>
auto matchResults = ola.FindImageFromPtrAll(imgPtr, templPtr, "101010", 0.9);
if (matchResults.empty()) { ola.FreeImage(imgPtr); ola.FreeImage(templPtr); return; }
// 2. 使用 toJSON 序列化为 JSON 数组
std::string matchJson = "[";
for (size_t i = 0; i < matchResults.size(); ++i) {
if (i > 0) matchJson += ",";
matchJson += ola.toJSON(matchResults[i]);
}
matchJson += "]";
// 3. 排除顶部 (0,0)-(800,80),type=2 表示图像识别
std::string filteredJson = ola.ExcludePos(matchJson.c_str(), 2, 0, 0, 800, 80);
// 4. 解析:格式与 FindImageFromPtrAll 相同,可用 ParseMatchImageAllJson 逐条读取
int32_t count = ola.GetMatchImageAllCount(filteredJson.c_str());
for (int32_t i = 0; i < count; i++) {
int32_t matchState = 0, x = 0, y = 0, width = 0, height = 0, index = 0;
double matchVal = 0, angle = 0;
ola.ParseMatchImageAllJson(filteredJson.c_str(), i, &matchState, &x, &y, &width, &height, &matchVal, &angle, &index);
}
ola.FreeImage(imgPtr);
ola.FreeImage(templPtr);csharp
using OLAPlug;
using Newtonsoft.Json;
using System.Linq;
var ola = new OLAPlugServer();
long imgPtr = ola.LoadImage("scene.png");
long templPtr = ola.LoadImage("button.bmp");
if (imgPtr == 0 || templPtr == 0) return;
// 1. 找图:SDK 返回 List<MatchResult>
var matchResults = ola.FindImageFromPtrAll(imgPtr, templPtr, "101010", 0.9);
if (matchResults.Count == 0) { ola.FreeImage(imgPtr); ola.FreeImage(templPtr); return; }
// 2. 使用 ToJSON 序列化回 JSON 数组
string matchJson = "[" + string.Join(",", matchResults.Select(m => m.ToJSON())) + "]";
// 3. 排除顶部工具栏
string filteredJson = ola.ExcludePos(matchJson, 2, 0, 0, 800, 80);
// 4. 解析过滤结果
var filteredMatches = JsonConvert.DeserializeObject<List<MatchResult>>(filteredJson);
if (filteredMatches != null && filteredMatches.Count > 0 && filteredMatches[0].MatchState)
{
int x = filteredMatches[0].X;
int y = filteredMatches[0].Y;
}
ola.FreeImage(imgPtr);
ola.FreeImage(templPtr);python
import json
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
img_ptr = ola.LoadImage("scene.png")
templ_ptr = ola.LoadImage("button.bmp")
if not img_ptr or not templ_ptr:
return
# 1. 找图:SDK 返回 List[dict]
match_results = ola.FindImageFromPtrAll(img_ptr, templ_ptr, "101010", 0.9)
if not match_results:
ola.FreeImage(img_ptr); ola.FreeImage(templ_ptr); return
# 2. 序列化回 JSON
match_json = json.dumps(match_results, ensure_ascii=False)
# 3. 排除顶部工具栏
filtered_json = ola.ExcludePos(match_json, 2, 0, 0, 800, 80)
# 4. 解析过滤结果
filtered_matches = json.loads(filtered_json)
if filtered_matches and filtered_matches[0].get("MatchState"):
x = filtered_matches[0]["X"]
y = filtered_matches[0]["Y"]
ola.FreeImage(img_ptr)
ola.FreeImage(templ_ptr)java
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.olaplug.OLAPlugServer;
import com.olaplug.model.MatchResult;
import java.util.List;
OLAPlugServer ola = new OLAPlugServer();
ObjectMapper mapper = new ObjectMapper();
long imgPtr = ola.LoadImage("scene.png");
long templPtr = ola.LoadImage("button.bmp");
if (imgPtr == 0 || templPtr == 0) return;
// 1. 找图
List<MatchResult> matchResults = ola.FindImageFromPtrAll(imgPtr, templPtr, "101010", 0.9);
if (matchResults == null || matchResults.isEmpty()) {
ola.FreeImage(imgPtr); ola.FreeImage(templPtr); return;
}
// 2. 使用 toJSON 序列化回 JSON 数组
String matchJson = matchResults.stream()
.map(MatchResult::toJSON)
.collect(java.util.stream.Collectors.joining(",", "[", "]"));
// 3. 排除顶部工具栏
String filteredJson = ola.ExcludePos(matchJson, 2, 0, 0, 800, 80);
// 4. 解析过滤结果
List<MatchResult> filteredMatches = mapper.readValue(filteredJson, new TypeReference<List<MatchResult>>() {});
if (filteredMatches != null && !filteredMatches.isEmpty() && filteredMatches.get(0).MatchState) {
int x = filteredMatches.get(0).X;
int y = filteredMatches.get(0).Y;
}
ola.FreeImage(imgPtr);
ola.FreeImage(templPtr);go
import (
"encoding/json"
"github.com/ola/olaplug/olaplug"
)
ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
imgPtr := ola.LoadImage("scene.png")
templPtr := ola.LoadImage("button.bmp")
if imgPtr == 0 || templPtr == 0 {
return
}
matchResults := ola.FindImageFromPtrAll(imgPtr, templPtr, "101010", 0.9)
if len(matchResults) == 0 {
ola.FreeImage(imgPtr)
ola.FreeImage(templPtr)
return
}
matchJson, _ := json.Marshal(matchResults)
filteredJson := ola.ExcludePos(string(matchJson), 2, 0, 0, 800, 80)
var filteredMatches []olaplug.MatchData
if json.Unmarshal([]byte(filteredJson), &filteredMatches) == nil && len(filteredMatches) > 0 && filteredMatches[0].MatchState {
x := filteredMatches[0].X
y := filteredMatches[0].Y
_ = x
_ = y
}
ola.FreeImage(imgPtr)
ola.FreeImage(templPtr)rust
use olaplug::{MatchData, OLAPlugServer};
let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let img_ptr = ola.load_image("scene.png");
let templ_ptr = ola.load_image("button.bmp");
if img_ptr == 0 || templ_ptr == 0 {
return;
}
let match_results = ola.find_image_from_ptr_all(img_ptr, templ_ptr, "101010", 0.9);
if match_results.is_empty() {
ola.free_image_ptr(img_ptr);
ola.free_image_ptr(templ_ptr);
return;
}
let match_json = format!(
"[{}]",
match_results.iter().map(|m| m.toJSON()).collect::<Vec<_>>().join(",")
);
let filtered_json = ola.exclude_pos(&match_json, 2, 0, 0, 800, 80);
if let Ok(filtered_matches) = serde_json::from_str::<Vec<MatchData>>(&filtered_json) {
if let Some(first) = filtered_matches.first() {
if first.match_state {
let _ = (first.x, first.y);
}
}
}
ola.free_image_ptr(img_ptr);
ola.free_image_ptr(templ_ptr);原生 DLL:识别 JSON 直接传入
cpp
long instance = CreateCOLAPlugInterFace();
// --- 颜色识别 ---
long colorPtr = FindColorListEx(instance, 0, 0, 0, 0, "[{\"StartColor\":\"FF0000\",\"EndColor\":\"FF0000\"}]");
if (colorPtr != 0) {
char colorJson[65536] = {0};
GetStringFromPtr(colorPtr, colorJson, sizeof(colorJson));
FreeStringPtr(colorPtr);
// 原生调用可直接使用识别 JSON,无需 SDK 序列化
long filteredPtr = ExcludePos(instance, colorJson, 1, 0, 0, 200, 100);
if (filteredPtr != 0) {
char filteredJson[65536] = {0};
GetStringFromPtr(filteredPtr, filteredJson, sizeof(filteredJson));
FreeStringPtr(filteredPtr);
int err = 0;
long arr = JsonParse(instance, filteredJson, &err);
if (arr != 0 && err == 0) {
int size = JsonGetSize(instance, arr, &err);
for (int i = 0; i < size; i++) {
long item = JsonGetArrayItem(instance, arr, i, &err);
int x = (int)JsonGetNumber(instance, item, "X", &err);
int y = (int)JsonGetNumber(instance, item, "Y", &err);
}
JsonFree(instance, arr);
}
}
}
// --- 图像识别 ---
long imgPtr = LoadImage(instance, "scene.png");
long templPtr = LoadImage(instance, "button.bmp");
if (imgPtr != 0 && templPtr != 0) {
long matchPtr = FindImageFromPtrAll(instance, imgPtr, templPtr, "101010", 0.9);
if (matchPtr != 0) {
char matchJson[65536] = {0};
GetStringFromPtr(matchPtr, matchJson, sizeof(matchJson));
FreeStringPtr(matchPtr);
long filteredPtr = ExcludePos(instance, matchJson, 2, 0, 0, 800, 80);
if (filteredPtr != 0) {
char filteredJson[65536] = {0};
GetStringFromPtr(filteredPtr, filteredJson, sizeof(filteredJson));
FreeStringPtr(filteredPtr);
int count = GetMatchImageAllCount(instance, filteredJson);
for (int i = 0; i < count; i++) {
int matchState, x, y, width, height, index;
double matchVal, angle;
ParseMatchImageAllJson(instance, filteredJson, i, &matchState, &x, &y, &width, &height, &matchVal, &angle, &index);
}
}
}
FreeImage(instance, imgPtr);
FreeImage(instance, templPtr);
}csharp
using System.Runtime.InteropServices;
using System.Text;
using Newtonsoft.Json;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
// ... 声明 FindColorListEx、ExcludePos、GetStringFromPtr、FreeStringPtr 等
long instance = CreateCOLAPlugInterFace();
long colorPtr = FindColorListEx(instance, 0, 0, 0, 0, "[{\"StartColor\":\"FF0000\",\"EndColor\":\"FF0000\"}]");
if (colorPtr != 0)
{
var sb = new StringBuilder(GetStringSize(colorPtr) + 1);
GetStringFromPtr(colorPtr, sb, sb.Capacity);
FreeStringPtr(colorPtr);
string colorJson = sb.ToString();
long filteredPtr = ExcludePos(instance, colorJson, 1, 0, 0, 200, 100);
if (filteredPtr != 0)
{
var filteredSb = new StringBuilder(GetStringSize(filteredPtr) + 1);
GetStringFromPtr(filteredPtr, filteredSb, filteredSb.Capacity);
FreeStringPtr(filteredPtr);
var filteredPoints = JsonConvert.DeserializeObject<List<Point>>(filteredSb.ToString());
}
}python
import json
from ctypes import CDLL, c_int64, create_string_buffer
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
color_ptr = ola.FindColorListEx(instance, 0, 0, 0, 0, b'[{"StartColor":"FF0000","EndColor":"FF0000"}]')
if color_ptr:
buf = create_string_buffer(65536)
ola.GetStringFromPtr(color_ptr, buf, 65536)
ola.FreeStringPtr(color_ptr)
color_json = buf.value.decode("utf-8")
filtered_ptr = ola.ExcludePos(instance, color_json.encode("utf-8"), 1, 0, 0, 200, 100)
if filtered_ptr:
filtered_buf = create_string_buffer(65536)
ola.GetStringFromPtr(filtered_ptr, filtered_buf, 65536)
ola.FreeStringPtr(filtered_ptr)
filtered_points = json.loads(filtered_buf.value.decode("utf-8"))返回值
| 返回值 | 说明 |
|---|---|
| (返回值) | 返回排除掉指定区域后的 JSON 字符串。格式与输入 json 相同(颜色为 [{"X":..,"Y":..}] 数组,图像为 MatchResult 对象数组)。SDK 封装已自动拷贝字符串;原生 DLL 返回的字符串指针需调用 FreeStringPtr 释放内存。 |
