Skip to content

匹配图片2 - MatchImageFromPtrAll

函数简介

匹配所有符合模板图片的位置信息,源图与模板均通过内存指针传入。

接口名称

MatchImageFromPtrAll

DLL调用

long MatchImageFromPtrAll(long ola, long source, long templ, double matchVal, int type, double angle, double scale);

参数说明

参数名类型说明
ola长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
source长整数型OLAImage对象的地址(源图片)。
templ长整数型OLAImage对象的地址(模板图片),由LoadImage等接口生成。
matchVal双精度浮点数相似度,如0.85,最大为1。
type整数型匹配类型:1-灰度匹配(速度快);2-彩色匹配;3-透明匹配;4-透明彩色权重匹配;5-普通彩色匹配。
angle双精度浮点数旋转角度,每次匹配后旋转指定角度继续匹配,角度越小匹配次数越多时间越长,0为不旋转速度最快。
scale双精度浮点数源图缩放比例,默认为1,可通过GetScaleFromWindows接口获取。

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
long imgPtr = ola.LoadImage("images/scene.png");
long templPtr = ola.LoadImage("img/template.bmp");
if (imgPtr != 0 && templPtr != 0) {
    auto results = ola.MatchImageFromPtrAll(imgPtr, templPtr, 0.9, 1, 0, 1);
    if (!results.empty() && results[0].MatchState) {
        int x = results[0].X;
        int y = results[0].Y;
    }
    // 后续不再使用该图时释放
        // 后续不再使用该模板图时释放
    ola.FreeImage(imgPtr);
    ola.FreeImage(templPtr);
}
csharp
using OLAPlug;

var ola = new OLAPlugServer();
long imgPtr = ola.LoadImage("images/scene.png");
long templPtr = ola.LoadImage("img/template.bmp");
if (imgPtr != 0 && templPtr != 0)
{
    var results = ola.MatchImageFromPtrAll(imgPtr, templPtr, 0.9, 1, 0, 1);
    if (results.Count > 0 && results[0].MatchState)
    {
        int x = results[0].X;
        int y = results[0].Y;
    }
    // 后续不再使用该图时释放
        // 后续不再使用该模板图时释放
    ola.FreeImage(imgPtr);
    ola.FreeImage(templPtr);
}
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
img_ptr = ola.LoadImage("images/scene.png")
templ_ptr = ola.LoadImage("img/template.bmp")
if img_ptr and templ_ptr:
    results = ola.MatchImageFromPtrAll(imgPtr, templPtr, 0.9, 1, 0, 1)
    if results and results[0].get("MatchState"):
        x = results[0]["X"]
        y = results[0]["Y"]
    # 后续不再使用该图时释放
    ola.FreeImage(img_ptr)
        # 后续不再使用该模板图时释放
    ola.FreeImage(templ_ptr)
java
import com.olaplug.OLAPlugServer;
import com.olaplug.model.MatchResult;
import java.util.List;

OLAPlugServer ola = new OLAPlugServer();
long imgPtr = ola.LoadImage("images/scene.png");
long templPtr = ola.LoadImage("img/template.bmp");
if (imgPtr != 0 && templPtr != 0) {
    List<MatchResult> results = ola.MatchImageFromPtrAll(imgPtr, templPtr, 0.9, 1, 0, 1);
    if (results != null && !results.isEmpty() && results.get(0).MatchState) {
        int x = results.get(0).X;
        int y = results.get(0).Y;
    }
    // 后续不再使用该图时释放
        // 后续不再使用该模板图时释放
    ola.FreeImage(imgPtr);
    ola.FreeImage(templPtr);
}
go
import "github.com/ola/olaplug/olaplug"

ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
imgPtr := ola.LoadImage("images/scene.png")
templPtr := ola.LoadImage("img/template.bmp")
if imgPtr != 0 && templPtr != 0 {
    auto results = ola.MatchImageFromPtrAll(imgPtr, templPtr, 0.9, 1, 0, 1)
    if (!results.empty() && results[0].MatchState) {
        x := results[0].X
        y := results[0].Y
    }
    // 后续不再使用该图时释放
        // 后续不再使用该模板图时释放
    ola.FreeImage(imgPtr)
    ola.FreeImage(templPtr)
}
rust
use olaplug::OLAPlugServer;

let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let imgPtr = ola.load_image("images/scene.png");
let templPtr = ola.load_image("img/template.bmp");
if imgPtr != 0 && templPtr != 0 {
    auto results = ola.match_image_from_ptr_all(imgPtr, templPtr, 0.9, 1, 0, 1)
    if (!results.empty() && results[0].MatchState) {
        let x = results[0].X;
        let y = results[0].Y;
    }
    // 后续不再使用该图时释放
        // 后续不再使用该模板图时释放
    ola.free_image(imgPtr);
    ola.free_image(templPtr);
}
cpp
var ola = com("OlaPlug.OlaSoft")
var imgPtr = ola.LoadImage("images/scene.png")
var templPtr = ola.LoadImage("img/template.bmp")
if(imgPtr && templPtr) {
    var results = ola.MatchImageFromPtrAll(imgPtr, templPtr, 0.9, 1, 0, 1)
    // 后续不再使用该图时释放
        // 后续不再使用该模板图时释放
    ola.FreeImage(imgPtr)
    ola.FreeImage(templPtr)
}
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
imgPtr = ola.LoadImage("images/scene.png")
templPtr = ola.LoadImage("img/template.bmp")
If imgPtr <> 0 And templPtr <> 0 Then
    results = ola.MatchImageFromPtrAll(imgPtr, templPtr, 0.9, 1, 0, 1)
    ' 后续不再使用该图时释放
        ' 后续不再使用该模板图时释放
    ola.FreeImage(imgPtr)
    ola.FreeImage(templPtr)
End If
text
.局部变量 ola, OLAPlug
.局部变量 retData, MatchData, , 数组
.局部变量 count, 整数型
.局部变量 rawstring, 文本型
.局部变量 imgPtr, 长整数型
.局部变量 templPtr, 长整数型
ola.创建 ()
imgPtr = ola.LoadImage ("images/scene.png")
templPtr = ola.LoadImage ("img/template.bmp")
.如果真 (imgPtr ≠ 0 且 templPtr ≠ 0)
    count = ola.MatchImageFromPtrAll (imgPtr, templPtr, 0.9, 1, 0, 1, retData, rawstring)
    ' rawstring 为原始 JSON 文本(可省略该参数)
    .如果真 (count > 0)
        ' retData 下标从 1 开始
    .如果真结束
    ola.FreeImage (imgPtr)
    ola.FreeImage (templPtr)
.如果真结束
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var imgPtr = ola.LoadImage("images/scene.png");
var templPtr = ola.LoadImage("img/template.bmp");
if(imgPtr && templPtr){
    var results = ola.MatchImageFromPtrAll(imgPtr, templPtr, 0.9, 1, 0, 1);
    // 后续不再使用该图时释放
        // 后续不再使用该模板图时释放
    ola.FreeImage(imgPtr);
    ola.FreeImage(templPtr);
}
text
变量 ola <类型 = OLAPlugServer>
变量 results <类型 = MatchDataList>
变量 imgPtr <类型 = 长整数>
变量 templPtr <类型 = 长整数>
ola = 新建 OLAPlugServer
imgPtr = ola.LoadImage("images/scene.png")
templPtr = ola.LoadImage("img/template.bmp")
如果真 (imgPtr ≠ 0 并且 templPtr ≠ 0)
    results = ola.MatchImageFromPtrAll(imgPtr, templPtr, 0.9, 1, 0, 1)
    ola.FreeImage(imgPtr)
    ola.FreeImage(templPtr)
结束
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
long imgPtr = ola.LoadImage("images/scene.png");
long templPtr = ola.LoadImage("img/template.bmp");
if (imgPtr != 0 && templPtr != 0) {
    auto results = ola.MatchImageFromPtrAll(imgPtr, templPtr, 0.9, 1, 0, 1);
    if (!results.empty() && results[0].MatchState) {
        int32_t x = results[0].X;
    }
    // 后续不再使用该图时释放
        // 后续不再使用该模板图时释放
    ola.FreeImage(imgPtr);
    ola.FreeImage(templPtr);
}

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long ptr = MatchImageFromPtrAll(instance, imgPtr, templPtr, 0.9, 1, 0, 1);
if (ptr != 0) {
    char json[8192] = {0};
    GetStringFromPtr(ptr, json, sizeof(json));
    int matchState = 0, x = 0, y = 0, width = 0, height = 0, index = 0;
    double matchVal = 0, angle = 0;
    ParseMatchImageAllJson(instance, json, 0, &matchState, &x, &y, &width, &height, &matchVal, &angle, &index);
    FreeStringPtr(instance, ptr);
}
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long MatchImageFromPtrAll(long ola, long source, long templ, double matchVal, int type, double angle, double scale);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int ParseMatchImageAllJson(string str, int parseIndex, out int matchState, out int x, out int y, out int width, out int height, out double matchVal, out double angle, out int index);

long instance = CreateCOLAPlugInterFace();
long ptr = MatchImageFromPtrAll(instance, imgPtr, templPtr, 0.9, 1, 0, 1);
if (ptr != 0) {
    StringBuilder sb = new StringBuilder(GetStringSize(ptr) + 1);
    GetStringFromPtr(ptr, sb, sb.Capacity);
    FreeStringPtr(ptr);
    ParseMatchImageAllJson(sb.ToString(), 0, out int matchState, out int x, out int y, out int w, out int h, out double mv, out double ang, out int idx);
}
python
from ctypes import CDLL, c_int, c_int64

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
ptr = ola.MatchImageFromPtrAll(instance, ...)
# 原生返回 JSON 数组指针,用 ParseMatchImageAllJson(instance, json, 0, ...) 取第一项

返回值

调用方式返回值说明
SDKMatchData/MatchResult 列表(Python 为 list[dict]全部匹配结果;空列表表示未找到
SDK(火山)MatchDataList结构化结果列表
SDK(易语言)整数型 + retDataMatchData 数组)返回找到数量,结果写入 retData;内部为 GetStringFromPtr + OL_获取MatchDataAll
原生 DLL非 0成功,返回 JSON 数组字符串指针,需调用 FreeStringPtr 释放
原生 DLL0失败

返回数据格式:

json
[{
    "MatchVal": 0.85,
    "MatchState": true,
    "Index": 0,
    "Angle": 45.0,
    "X": 100,
    "Y": 200,
    "Width": 100,
    "Height": 100
}]
字段名类型说明
MatchVal浮点数匹配相似度。
MatchState布尔是否匹配成功。
Index整数结果索引(从 0 开始)。
Angle浮点数匹配角度。
X整数X 坐标。
Y整数Y 坐标。
Width整数宽度。
Height整数高度。