牙叔教程 简单易学
使用场景
orb适用于简单的图片查找, 比如一些纯色和形状比较单一的图片, 即使缩放旋转都可以找到;
如果是游戏这种, 色彩丰富的图片, 并且有山水天, 等等复杂地形的图片, 就不能用了
简单的图片, 效果尚可
复杂的图片, 特征点都找到四面八方去了
这个是找中间顶部的女生头像
这个是找右侧的点赞按钮
这个是找点赞按钮
autojs版本
8.8.10-0
代码讲解
1. 初始化opencv, 不加这个会报错
runtime.images.initOpenCvIfNeeded();
2. 导入用到的类
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.DescriptorExtractor); importClass(org.opencv.features2d.DescriptorMatcher); importClass(org.opencv.features2d.FeatureDetector); importClass(org.opencv.features2d.Features2d);
3. 设置图片路径
// let bigImgPath1 = "./图片素材/测试_缩小一半.png"; // let smallImgPath = "./图片素材/测试小图.png"; // let bigImgPath1 = "./图片素材/测试小图.png"; // let smallImgPath = "./图片素材/测试小图.png"; // let bigImgPath1 = "./图片素材/game001.png"; // let smallImgPath = "./图片素材/game001_女生头像.png"; // let bigImgPath1 = "./图片素材/dy1.png"; // let smallImgPath = "./图片素材/dy1_心_带背景.png"; let bigImgPath1 = "./图片素材/dy1_右侧.png"; let smallImgPath = "./图片素材/dy1_心_带背景.png";
4. 对比两张图片
compareImages(bigImgPath1, smallImgPath);
自定义函数compareImages
1. 保证是绝对路径
bigImgPath1 = files.path(bigImgPath1); smallImgPath = files.path(smallImgPath);
2. 初始化一些变量
var detector = FeatureDetector.create(FeatureDetector.ORB); var descriptor = DescriptorExtractor.create(DescriptorExtractor.ORB); var matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
3. 计算图片特征
// 处理大图 var bigImg = Imgcodecs.imread(bigImgPath1); var descriptors1 = new Mat(); var keyPoints1 = new MatOfKeyPoint(); detector.detect(bigImg, keyPoints1); descriptor.compute(bigImg, keyPoints1, descriptors1); // 处理小图 var smallImg = Imgcodecs.imread(smallImgPath); var descriptors2 = new Mat(); var keyPoints2 = new MatOfKeyPoint(); detector.detect(smallImg, keyPoints2); descriptor.compute(smallImg, keyPoints2, descriptors2);
4. 匹配特征点
var matches = new MatOfDMatch(); matcher.match(descriptors1, descriptors2, matches);
5. 提取前200个特征点
// 排序 var mats = matches.toArray(); function compareNumbers(a, b) { return a.distance - b.distance; } mats.sort(compareNumbers); // 提取最佳匹配 let num = 200; var goodMatch = new LinkedList(); let tempArr = mats.slice(0, mats.length >= num ? num : mats.length); tempArr.map((item) => { goodMatch.add(item); }); matches.fromList(goodMatch);
6. 画特征点
var dst = new Mat(); Features2d.drawMatches(bigImg, keyPoints1, smallImg, keyPoints2, matches, dst); let tempFilePath = files.join(files.getSdcardPath(), "脚本", "orb.png"); Imgcodecs.imwrite(tempFilePath, dst);
7. 释放资源
bigImg.release(); descriptors1.release(); smallImg.release(); descriptors2.release(); dst.release();
8. 查看图片
app.viewFile(tempFilePath);
完整源码
公众号回复 obr
感谢大佬分享ORB例子
- @Tsing Yi
声明
部分内容来自网络
本教程仅用于学习, 禁止用于其他用途