当前位置: 首页 > news >正文

【教程】Autojs使用OpenCV进行SIFT/BRISK等算法进行图像匹配

转载请注明出处:小锋学长生活大爆炸[xfxuezhang.cn]

        此代码可以替代内置的images.findImage函数使用,但可能会误匹配,如果是对匹配结果要求比较高的,还是得谨慎使用。


runtime.images.initOpenCvIfNeeded();
importClass(java.util.ArrayList);
importClass(java.util.List);
importClass(java.util.LinkedList);
importClass(org.opencv.imgproc.Imgproc);
importClass(org.opencv.imgcodecs.Imgcodecs);
importClass(org.opencv.core.Core);
importClass(org.opencv.core.Mat);
importClass(org.opencv.core.MatOfDMatch);
importClass(org.opencv.core.MatOfKeyPoint);
importClass(org.opencv.core.MatOfRect);
importClass(org.opencv.core.Size);
importClass(org.opencv.features2d.DescriptorMatcher);
importClass(org.opencv.features2d.Features2d);
importClass(org.opencv.features2d.SIFT);
importClass(org.opencv.features2d.ORB);
importClass(org.opencv.features2d.BRISK);
importClass(org.opencv.features2d.AKAZE);
importClass(org.opencv.features2d.BFMatcher);
importClass(org.opencv.core.MatOfPoint2f);
importClass(org.opencv.calib3d.Calib3d);
importClass(org.opencv.core.CvType);
importClass(org.opencv.core.Point);
importClass(org.opencv.core.Scalar);
importClass(org.opencv.core.MatOfByte);/** 用法示例:* var image1 = captureScreen();* var image2 = images.read('xxxx');* match(image1, image2);*/function match(img1, img2, method) {console.time("匹配耗时");// 指定特征点算法SIFTvar match_alg = null;if(method == 'sift') {match_alg = SIFT.create();}else if(method == 'orb') {match_alg = ORB.create();}else if(method == 'brisk') {match_alg = BRISK.create();}else {match_alg = AKAZE.create();}var bigTrainImage = Imgcodecs.imdecode(new MatOfByte(images.toBytes(img1)), Imgcodecs.IMREAD_UNCHANGED);var smallTrainImage = Imgcodecs.imdecode(new MatOfByte(images.toBytes(img2)), Imgcodecs.IMREAD_UNCHANGED);// 转灰度图// console.log("转灰度图");var big_trainImage_gray = new Mat(bigTrainImage.rows(), bigTrainImage.cols(), CvType.CV_8UC1);var small_trainImage_gray = new Mat(smallTrainImage.rows(), smallTrainImage.cols(), CvType.CV_8UC1);Imgproc.cvtColor(bigTrainImage, big_trainImage_gray, Imgproc.COLOR_BGR2GRAY);Imgproc.cvtColor(smallTrainImage, small_trainImage_gray, Imgproc.COLOR_BGR2GRAY);// 获取图片的特征点// console.log("detect");var big_keyPoints = new MatOfKeyPoint();var small_keyPoints = new MatOfKeyPoint();match_alg.detect(bigTrainImage, big_keyPoints);match_alg.detect(smallTrainImage, small_keyPoints);// 提取图片的特征点// console.log("compute");var big_trainDescription = new Mat(big_keyPoints.rows(), 128, CvType.CV_32FC1);var small_trainDescription = new Mat(small_keyPoints.rows(), 128, CvType.CV_32FC1);match_alg.compute(big_trainImage_gray, big_keyPoints, big_trainDescription);match_alg.compute(small_trainImage_gray, small_keyPoints, small_trainDescription);// console.log("matcher.train");var matcher = new BFMatcher();matcher.clear();var train_desc_collection = new ArrayList();train_desc_collection.add(big_trainDescription);// vector<Mat>train_desc_collection(1, trainDescription);matcher.add(train_desc_collection);matcher.train();// console.log("knnMatch");var matches = new ArrayList();matcher.knnMatch(small_trainDescription, matches, 2);//对匹配结果进行筛选,依据distance进行筛选// console.log("对匹配结果进行筛选");var goodMatches = new ArrayList();var nndrRatio = 0.8;var len = matches.size();for (var i = 0; i < len; i++) {var matchObj = matches.get(i);var dmatcharray = matchObj.toArray();var m1 = dmatcharray[0];var m2 = dmatcharray[1];if (m1.distance <= m2.distance * nndrRatio) {goodMatches.add(m1);}}var matchesPointCount = goodMatches.size();//当匹配后的特征点大于等于 4 个,则认为模板图在原图中,该值可以自行调整if (matchesPointCount >= 4) {log("模板图在原图匹配成功!");var templateKeyPoints = small_keyPoints;var originalKeyPoints = big_keyPoints;var templateKeyPointList = templateKeyPoints.toList();var originalKeyPointList = originalKeyPoints.toList();var objectPoints = new LinkedList();var scenePoints = new LinkedList();var goodMatchesList = goodMatches;var len = goodMatches.size();for (var i = 0; i < len; i++) {var goodMatch = goodMatches.get(i);objectPoints.addLast(templateKeyPointList.get(goodMatch.queryIdx).pt);scenePoints.addLast(originalKeyPointList.get(goodMatch.trainIdx).pt);}var objMatOfPoint2f = new MatOfPoint2f();objMatOfPoint2f.fromList(objectPoints);var scnMatOfPoint2f = new MatOfPoint2f();scnMatOfPoint2f.fromList(scenePoints);//使用 findHomography 寻找匹配上的关键点的变换var homography = Calib3d.findHomography(objMatOfPoint2f, scnMatOfPoint2f, Calib3d.RANSAC, 3);/*** 透视变换(Perspective Transformation)是将图片投影到一个新的视平面(Viewing Plane),也称作投影映射(Projective Mapping)。*/var templateCorners = new Mat(4, 1, CvType.CV_32FC2);var templateTransformResult = new Mat(4, 1, CvType.CV_32FC2);var templateImage = smallTrainImage;var doubleArr = util.java.array("double", 2);doubleArr[0] = 0;doubleArr[1] = 0;templateCorners.put(0, 0, doubleArr);doubleArr[0] = templateImage.cols();doubleArr[1] = 0;templateCorners.put(1, 0, doubleArr);doubleArr[0] = templateImage.cols();doubleArr[1] = templateImage.rows();templateCorners.put(2, 0, doubleArr);doubleArr[0] = 0;doubleArr[1] = templateImage.rows();templateCorners.put(3, 0, doubleArr);//使用 perspectiveTransform 将模板图进行透视变以矫正图象得到标准图片Core.perspectiveTransform(templateCorners, templateTransformResult, homography);//矩形四个顶点var pointA = templateTransformResult.get(0, 0);var pointB = templateTransformResult.get(1, 0);var pointC = templateTransformResult.get(2, 0);var pointD = templateTransformResult.get(3, 0);var y0 = Math.round(pointA[1])>0?Math.round(pointA[1]):0;var y1 = Math.round(pointC[1])>0?Math.round(pointC[1]):0;var x0 = Math.round(pointD[0])>0?Math.round(pointD[0]):0;var x1 = Math.round(pointB[0])>0?Math.round(pointB[0]):0;console.timeEnd("匹配耗时");return {x: x0, y: y0};} else {console.timeEnd("匹配耗时");log("模板图不在原图中!");return null;}
}

相关文章:

【教程】Autojs使用OpenCV进行SIFT/BRISK等算法进行图像匹配

转载请注明出处&#xff1a;小锋学长生活大爆炸[xfxuezhang.cn] 此代码可以替代内置的images.findImage函数使用&#xff0c;但可能会误匹配&#xff0c;如果是对匹配结果要求比较高的&#xff0c;还是得谨慎使用。 runtime.images.initOpenCvIfNeeded(); importClass(java.uti…...

[庆国庆 迎国庆 发文]云计算的概念

庆国庆 迎国庆 国庆发文100%可得专属勋章 一年仅有一次哦 不要错过啦 去发布 https://activity.csdn.net/creatActivity?id10567&spm1011.2480.3001.6900 https://mp.csdn.net/edit?activity_id10567&spm1057.2600.3001.9674 云计算&#xff08;cloud computing&…...

计算机网络-计算机网络体系结构-概述,模型

目录 一、计算机网络概述 二、性能指标 速率 带宽 吞吐量 时延 往返时延RTT 利用率 三、计算机网络体系结构 分层结构 IOS模型 应用层-> 表示层-> 会话层-> 传输层-> 网络层-> 数据链路层-> 物理层-> TCP/IP模型 一、计算机网络概述 计…...

对示例程序spinner_asyncio.py进行修改使其能运行

学习《流畅的python》第18章 使用asyncio包处理并发&#xff0c;运行示例18-2 spinner_asyncio.py的时候&#xff0c;程序报错如下&#xff1a; D:\fluentPy\chapter17>python spinner_asyncio.py File "D:\fluentPy\chapter17\spinner_asyncio.py", line 30 …...

Linux命令(93)之head

linux命令之head 1.head介绍 linux命令head用来查看文件的前N行内容&#xff1b;默认head查看前10行 2.head用法 head [参数] 文件 head常用参数 参数说明-n从头显示N行&#xff0c;默认显示10行&#xff0c;可以不写-q隐藏文件名&#xff0c;在查看两个及以上文件名的情况…...

使用Visual Studio调试排查Windows系统程序audiodg.exe频繁弹出报错

VC常用功能开发汇总&#xff08;专栏文章列表&#xff0c;欢迎订阅&#xff0c;持续更新...&#xff09;https://blog.csdn.net/chenlycly/article/details/124272585C软件异常排查从入门到精通系列教程&#xff08;专栏文章列表&#xff0c;欢迎订阅&#xff0c;持续更新...&a…...

WebSocket实战之六心跳重连机制

一、前言 WebSocket应用部署到生产环境&#xff0c;我们除了会碰到因为经过代理服务器无法连接的问题&#xff08;注&#xff1a;该问题可以通过搭建WSS来解决&#xff0c;具体配置请看 WebSocket实战之四WSS配置 &#xff09;&#xff0c;另外一个问题就是外网环境不稳定经常…...

Webpack 基础入门以及接入 CSS、Typescript、Babel

一、什么是 Webpack Webpack 是一款 JS 模块化开发的技术框架&#xff0c;其运作原理是将多个 JS 文件关联起来构成可运行的应用程序。 Webpack 拥有丰富的 plugins / loaders 插件生态圈&#xff0c;可以让 js 识别不同的语言如 .css, .scss, .sass, .json, .xml, .ts, .vue…...

postgresql-自增字段

postgresql-自增字段 标识列IdentitySerial类型Sequence序列 标识列Identity -- 测试表 create table t_user( -- 标识列自增字段user_id integer generated always as identity primary key,user_name varchar(50) not null unique );-- 自动生成序列 CREATE SEQUENCE public…...

SpringBoot中使用Servlet和Filter

为什么要把Servlet和Filter写在一起,因为使用方式很相似 两种方式 第一种,使用Servlet和Filter 使用Servlet 继承HttpServlet 注册Servlet 使用Filter 1.自定义过滤器 2.注册过滤器 这里注意一点 使用/**无效 至少我这2.4.5版本是这样 过滤所有请求用/* 那么其实还有…...

Monkey命令

shell, monkey, system, Android, 文件系统Monkey, 示例, 简介 一、Monkey测试简介 Monkey测试是Android平台自动化测试的一种手段&#xff0c;通过Monkey程序模拟用户触摸屏幕、滑动Trackball、按键等操作来对设备上的程序进行压 力测试&#xff0c;检测程序多久的时间会发生…...

力扣 -- 279. 完全平方数(完全背包问题)

解题步骤&#xff1a; 参考代码&#xff1a; 未优化代码&#xff1a; class Solution { public:int numSquares(int n) {const int INF0x3f3f3f3f;int msqrt(n);//多开一行&#xff0c;多开一列vector<vector<int>> dp(m1,vector<int>(n1));//初始化第一行…...

在将对象 => JSON格式时,无法序列化部分属性

问题现象: 在ssm项目中&#xff0c;一个controller返回Msg对象&#xff08;自定义对象&#xff0c;包含三个属性&#xff0c;int code&#xff1b;String msg&#xff1b;HashMap map&#xff1b;同时这三个属性都有对应的get和set方法&#xff09;&#xff0c;我的map属性里面…...

用python表格初级尝试

Excel&#xff0c;我的野心 当我输入3,2 就表示在第3行第2列。的单元格输入数据input输入表头 &#xff08;input内除了/&#xff0c;空格 回车 标点符号等 全部作为单元格分隔符&#xff09;由我设置input输入的是行or列 给选项 1. 行 2. 列默认回车或没输入值是列由我设置起…...

【单片机】16-LCD1602和12864显示器

1.LCD显示器相关背景 1.LCD简介 &#xff08;1&#xff09;显示器&#xff0c;常见显示器&#xff1a;电视&#xff0c;电脑 &#xff08;2&#xff09;LCD&#xff08;Liquid Crystal Display&#xff09;&#xff0c;液晶显示器&#xff0c;原理介绍 &#xff08;3&#xff…...

AUTOSAR从入门到精通-基于 CAN 总线的汽车发电机智能调节器(下)

目录 4.4.3 CAN 通信软件实现 汽车发电机智能调节器试验与结果分析 5.1 试验方案设计...

Windows下Tensorflow docker python开发环境搭建

前置条件 windows10 更新到较新的版本&#xff0c;硬件支持Hyper-V。 参考&#xff1a;https://learn.microsoft.com/zh-cn/windows/wsl/install 启用WSL 在Powershell中输入如下指令&#xff1a; dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsys…...

idea常用快捷键 idea搜索快捷键

常用快捷键 IntelliJ IDEA 是一款流行的 Java 集成开发环境&#xff08;IDE&#xff09;&#xff0c;有许多常用的快捷键可以帮助开发者提高效率。以下是一些常用的 IntelliJ IDEA 快捷键&#xff1a; CtrlSpace&#xff1a;基本代码补全&#xff0c;用于输入任何东西&#x…...

Redis Cluster Gossip Protocol: MEET

返回目录 CLUSTER MEET 过程说明 #mermaid-svg-dp95n6LRjBO1mCKE {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-dp95n6LRjBO1mCKE .error-icon{fill:#552222;}#mermaid-svg-dp95n6LRjBO1mCKE .error-text{fill:#…...

TcpConnection的读写操作【深度剖析】

文章目录 前言一、TcpConnection的读二、TcpConnection的写三、TcpConnection的关闭 前言 今天总结TcpConnection类的读写事件。 一、TcpConnection的读 当Poller检测到套接字的Channel处于可读状态时&#xff0c;会调用Channel的回调函数&#xff0c;回调函数中根据不同激活…...

调用支付宝接口响应40004 SYSTEM_ERROR问题排查

在对接支付宝API的时候&#xff0c;遇到了一些问题&#xff0c;记录一下排查过程。 Body:{"datadigital_fincloud_generalsaas_face_certify_initialize_response":{"msg":"Business Failed","code":"40004","sub_msg…...

【OSG学习笔记】Day 18: 碰撞检测与物理交互

物理引擎&#xff08;Physics Engine&#xff09; 物理引擎 是一种通过计算机模拟物理规律&#xff08;如力学、碰撞、重力、流体动力学等&#xff09;的软件工具或库。 它的核心目标是在虚拟环境中逼真地模拟物体的运动和交互&#xff0c;广泛应用于 游戏开发、动画制作、虚…...

Unity3D中Gfx.WaitForPresent优化方案

前言 在Unity中&#xff0c;Gfx.WaitForPresent占用CPU过高通常表示主线程在等待GPU完成渲染&#xff08;即CPU被阻塞&#xff09;&#xff0c;这表明存在GPU瓶颈或垂直同步/帧率设置问题。以下是系统的优化方案&#xff1a; 对惹&#xff0c;这里有一个游戏开发交流小组&…...

DAY 47

三、通道注意力 3.1 通道注意力的定义 # 新增&#xff1a;通道注意力模块&#xff08;SE模块&#xff09; class ChannelAttention(nn.Module):"""通道注意力模块(Squeeze-and-Excitation)"""def __init__(self, in_channels, reduction_rat…...

【磁盘】每天掌握一个Linux命令 - iostat

目录 【磁盘】每天掌握一个Linux命令 - iostat工具概述安装方式核心功能基础用法进阶操作实战案例面试题场景生产场景 注意事项 【磁盘】每天掌握一个Linux命令 - iostat 工具概述 iostat&#xff08;I/O Statistics&#xff09;是Linux系统下用于监视系统输入输出设备和CPU使…...

智能在线客服平台:数字化时代企业连接用户的 AI 中枢

随着互联网技术的飞速发展&#xff0c;消费者期望能够随时随地与企业进行交流。在线客服平台作为连接企业与客户的重要桥梁&#xff0c;不仅优化了客户体验&#xff0c;还提升了企业的服务效率和市场竞争力。本文将探讨在线客服平台的重要性、技术进展、实际应用&#xff0c;并…...

多模态商品数据接口:融合图像、语音与文字的下一代商品详情体验

一、多模态商品数据接口的技术架构 &#xff08;一&#xff09;多模态数据融合引擎 跨模态语义对齐 通过Transformer架构实现图像、语音、文字的语义关联。例如&#xff0c;当用户上传一张“蓝色连衣裙”的图片时&#xff0c;接口可自动提取图像中的颜色&#xff08;RGB值&…...

《通信之道——从微积分到 5G》读书总结

第1章 绪 论 1.1 这是一本什么样的书 通信技术&#xff0c;说到底就是数学。 那些最基础、最本质的部分。 1.2 什么是通信 通信 发送方 接收方 承载信息的信号 解调出其中承载的信息 信息在发送方那里被加工成信号&#xff08;调制&#xff09; 把信息从信号中抽取出来&am…...

EtherNet/IP转DeviceNet协议网关详解

一&#xff0c;设备主要功能 疆鸿智能JH-DVN-EIP本产品是自主研发的一款EtherNet/IP从站功能的通讯网关。该产品主要功能是连接DeviceNet总线和EtherNet/IP网络&#xff0c;本网关连接到EtherNet/IP总线中做为从站使用&#xff0c;连接到DeviceNet总线中做为从站使用。 在自动…...

爬虫基础学习day2

# 爬虫设计领域 工商&#xff1a;企查查、天眼查短视频&#xff1a;抖音、快手、西瓜 ---> 飞瓜电商&#xff1a;京东、淘宝、聚美优品、亚马逊 ---> 分析店铺经营决策标题、排名航空&#xff1a;抓取所有航空公司价格 ---> 去哪儿自媒体&#xff1a;采集自媒体数据进…...