找图找色1
## 找图找色
### 申请屏幕截图权限 | requestScreenCapture
| 函数名 | 返回值 | | 备注 |
| --- | --- | --- | --- |
| images.requestScreenCapture() | | | 向系统申请屏幕截图权限,返回是否请求成功。 |
| **参数名** | **类型** | 空 | |
| 截屏是否为横屏 | 布尔值 {boolean} | ✔ | false, 则表示竖屏截图; true为横屏截图。 |
images.requestScreenCapture([landscape])
- `landscape` {boolean} 布尔值, 表示将要执行的截屏是否为横屏。如果landscape为false, 则表示竖屏截图; true为横屏截图。
向系统申请屏幕截图权限,返回是否请求成功。
第一次使用该函数会弹出截图权限请求,建议选择“总是允许”。(某些系统没有总是允许选项)
这个函数只是申请截图权限,并不会真正执行截图,真正的截图函数是`captureScreen()`。
该函数在截图脚本中只需执行一次,而无需每次调用`captureScreen()`都调用一次;若已有截图权限,则抛出异常。
**如果不指定landscape值,则截图方向由当前设备屏幕方向决定**,因此务必注意执行该函数时的屏幕
截图权限无法在脚本引擎之间共享。
建议在本软件界面运行该函数,在其他软件界面运行时容易出现一闪而过的黑屏现象;另外,**某些定制ROM或者高版本Android不允许在后台弹出界面,在后台运行此函数时可能会一直阻塞**。
示例:
// 请求截图 if(!requestScreenCapture()){ toast("请求截图失败"); exit(); } // 连续截图10张图片(间隔1秒)并保存到存储卡目录 for(var i = 0; i < 10; i++){ captureScreen("/sdcard/screen_capture_" + i + ".png"); sleep(1000); }
该函数也可以作为全局函数使用。
> MIUI无法请求截图和launchApp无效说明
> 权限管理里给一下“后台弹出界面”权限即可。
> 最近MIUI似乎远程推送了一个策略,突然后台弹出界面权限默认禁止了。
红米note8高配版:
设置——应用管理——更多应用——Auto.js Pro——权限管理——后台弹出界面——开启
### $images.requestScreenCapture(options)
**[[Pro 8.0新增](https://pro.autojs.org/)]**
- `options` {object} 申请截图选项
- `width` {number} 截图宽度,默认为-1,即自动设置为设备屏幕宽度
- `height` {number} 截图高度,默认为-1,即自动设置为设备屏幕高度
- `orientation` {number} 截图方向,默认为0
- -1:ORIENTATION\_CURRENT, 检测当前的屏幕方向,用该方向作为申请截图的屏幕方向
- 0: ORIENTATION\_AUTO, 自动适应截图方向(转屏时自动切换方向)
- 1: ORIENTATION\_PORTRAIT, 竖屏截图
- 2: ORIENTATION\_LANDSCAPE, 横屏截图
- `async` {boolean} 是否为异步截图。默认为false
向系统申请屏幕截图权限,返回是否请求成功。对于width和height参数,系统只会匹配相邻的合适的宽高。截图宽高不一定和指定的宽高完全一致。
requestScreenCapture({orientation: 0});
更多参数和说明参见上面的`images.requestScreenCapture([landscape])`函数,这里只特别解释`async`参数。
当`async`为true时,申请截图将为异步截图,也即无法通过`captureScreen()`来截图,而是通过事件`screen_capture`来监听截图。
该事件将在屏幕变化时自动触发,对于屏幕刷新少的软件界面更加节能省电,对于游戏界面则可能无法达到省电效果。
// 请求截图权限, 注意参数 async: true requestScreenCapture({async: true}); let target = $images.read('./test.png'); $events.on('exit', () => target.recycle()); // 监听屏幕截图 $images.on("screen_capture", capture => { // 找图 let pos = $images.findImage(capture, target); // 打印 console.log(pos); });
### $images.getScreenCaptureOptions()
**[[Pro 8.8.12新增](https://pro.autojs.org/)]**
- 返回 {object | null}
获取当前截图配置选项。如果并未申请截图权限,则返回`null`。返回的对象有以下字段:
\* `width` {number} 截图宽度
\* `height` {number} 截图高度
\* `orientation` {number} 截图方向
\* 0: ORIENTATION\_AUTO, 自动适应截图方向
\* 1: ORIENTATION\_PORTRAIT, 竖屏截图
\* 2: ORIENTATION\_LANDSCAPE, 横屏截图
\* `density` {number} 截图像素密度
\* `async` {boolean} 是否为异步截图
### $images.stopScreenCapture()
**[[Pro 8.8.12新增](https://pro.autojs.org/)]**
释放截图权限。如果并未申请截图权限,则此函数没有任何作用。
### 截取屏幕 | automator.takeScreenshot()
| 函数名 | 返回值 | 备注 |
| --- | --- | --- |
| automator.takeScreenshot() | Image对象 | 使用无障碍权限截图 |
automator.takeScreenshot()
**[[Pro 8.8.0新增](https://pro.autojs.org/)]**
- 返回 {Image}
使用无障碍权限截图,返回一个Image对象。
相比起images模块申请截图权限截图,该函数不需要额外权限,但是有以下限制:
- 截图频率限制。系统限制截图最多一秒一次,否则抛出异常。
- 需要Android 11及以上版本
$auto.waitFor(); let capture = $automator.takeScreenshot(); $images.save(capture, "./capture.png");
### 截取屏幕 | captureScreen
| 函数名 | 返回值 | 备注 |
| --- | --- | --- |
| images.captureScreen() | Image对象 | 截取当前屏幕并返回一个Image对象 |
images.captureScreen()
截取当前屏幕并返回一个Image对象。
没有截图权限时执行该函数会抛出SecurityException。
该函数不会返回null,两次调用可能返回相同的Image对象。这是因为设备截图的更新需要一定的时间,短时间内(一般来说是16ms)连续调用则会返回同一张截图。
截图需要转换为Bitmap格式,从而该函数执行需要一定的时间(0~20ms)。
另外在requestScreenCapture()执行成功后需要一定时间后才有截图可用,因此如果立即调用captureScreen(),会等待一定时间后(一般为几百ms)才返回截图。
例子:
// 请求横屏截图 requestScreenCapture(true); // 截图 var img = captureScreen(); // 获取在点(100, 100)的颜色值 var color = images.pixel(img, 100, 100); // 显示该颜色值 toast(colors.toString(color));
该函数也可以作为全局函数使用。
### 截取屏幕保存文件 |captureScreen
| 函数名 | 返回值 | 备注 |
| --- | --- | --- |
| images.captureScreen() | | 截取当前屏幕并以PNG格式保存到path中。 |
| **参数名** | **类型** | |
| 截图保存路径 | 字符串 | |
images.captureScreen(path)
- `path` {string} 截图保存路径
截取当前屏幕并以PNG格式保存到path中。如果文件不存在会被创建;文件存在会被覆盖。
该函数不会返回任何值。该函数也可以作为全局函数使用。
### 取图片单点颜色 | pixel
| 函数名 | 返回值 | 备注 |
| --- | --- | --- |
| images.pixel() | 整数型 | 返回图片image在点(x, y)处的像素的ARGB值。 |
| **参数名** | **类型** | |
| 图片 | Image | |
| 横坐标。 | 整数型 | 要获取的像素的横坐标 |
| 纵坐标。 | 整数型 | 要获取的像素的纵坐标。 |
images.pixel(image, x, y)
- `image` {Image} 图片
- `x` {number} 要获取的像素的横坐标。
- `y` {number} 要获取的像素的纵坐标。
返回图片image在点(x, y)处的像素的ARGB值。
该值的格式为0xAARRGGBB,是一个"32位整数"(虽然JavaScript中并不区分整数类型和其他数值类型)。
坐标系以图片左上角为原点。以图片左侧边为y轴,上侧边为x轴。
// 直播取性别节点颜色值 //请求截图权限 requestScreenCapture(); //屏幕截图 var img = captureScreen(); //取坐标点颜色值 log(images.pixel(img, 450,840))
### 找色 |findColor
| 函数名 | 返回值 | 备注 |
| --- | --- | --- |
| images.findColor() | 坐标 | 在图片中寻找颜色color |
| **参数名** | **类型** | |
| 图片 | Image | |
| 颜色的RGB值 | 整数型/字符串 | 如果是一个整数,则以0xRRGGBB代表RGB值(A通道会被忽略);如果是字符串,则以"#RRGGBB"代表RGB值。 |
| 选项 | 对象 | |
images.findColor(image, color, options)
- `image` {Image} 图片
- `color` {number} | {string} 要寻找的颜色的RGB值。如果是一个整数,则以0xRRGGBB的形式代表RGB值(A通道会被忽略);如果是字符串,则以"#RRGGBB"代表其RGB值。
- `options` {Object} 选项
在图片中寻找颜色color。找到时返回找到的点Point,找不到时返回null。
选项包括:
- `region` {Array} 找色区域。是一个两个或四个元素的数组。(region[0], region[1])表示找色区域的左上角;region[2]\*region[3]表示找色区域的宽高。如果只有region只有两个元素,则找色区域为(region[0], region[1])到图片右下角。如果不指定region选项,则找色区域为整张图片。
- `threshold` {number} 找色时颜色相似度的临界值,范围为0 ~ 255(越小越相似,0为颜色相等,255为任何颜色都能匹配)。默认为4。threshold和浮点数相似度(0.0~1.0)的换算为 similarity = (255 - threshold) / 255.
该函数也可以作为全局函数使用。
一个循环找色的例子如下:
requestScreenCapture(); // 循环找色,找到红色(#ff0000)时停止并报告坐标 while(true){ var img = captureScreen(); var point = findColor(img, "#ff0000"); if(point){ toast("找到红色,坐标为(" + point.x + ", " + point.y + ")"); } }
一个区域找色的例子如下:
// 读取本地图片/sdcard/1.png var img = images.read("/sdcard/1.png"); // 判断图片是否加载成功 if(!img){ toast("没有该图片"); exit(); } // 在该图片中找色,指定找色区域为在位置(400, 500)的宽为300长为200的区域,指定找色临界值为4 var point = findColor(img, "#00ff00", { region: [400, 500, 300, 200], threshold: 4 }); if(point){ toast("找到啦:" + point); }else{ toast("没找到"); }
### 区域找色(相似) |findColorInRegion
| 函数名 | 返回值 | | 备注 |
| --- | --- | --- | --- |
| images.findColorInRegion() | Point | | 区域找色的简便方法 |
| **参数名** | **类型** | **空** | |
| 图片 | Image | | |
| 要寻找的颜色 | 整数型/字符串 | | |
| 横坐标 | 整数型 | | |
| 纵坐标 | 整数型 | | |
| 区域的宽度 | 整数型 | ✔ | |
| 区域的高度 | 整数型 | ✔ | |
| 相似度 | 整数型 | ✔ | 默认为4。取值范围为0~255。 |
images.findColorInRegion(img, color, x, y[, width, height, threshold])
区域找色的简便方法。
相当于
images.findColor(img, color, { region: [x, y, width, height], threshold: threshold });
该函数也可以作为全局函数使用。
//请求截图 if (!requestScreenCapture()) { log("请求截图失败"); exit(); } else { log("请求截图成功"); } function APP_取性别() { // 0,女;1,男;2,无 // 判斷是否在用戶資料頁 // waitForActivity("com.netease.vopen.timeline.ui.UserTimelineActivity"); //等待页面出现 if (id("timeline_profile_gender").exists()) { var rect = id("timeline_profile_gender").findOne().bounds(); //寻找性别图标 var img = captureScreen(); var point = images.findColorInRegion(img, -13260321, rect.left, rect.top, rect.width(), rect.height(), 4) if (point) { log("找到蓝色啦:" + point); //男 return 1; } else { log("没找到蓝色"); point = images.findColorInRegion(img, -622207, rect.left, rect.top, rect.width(), rect.height(), 4) if (point) { log("找到粉色啦:" + point); //女 return 0; } else { log("没找到粉色"); }; }; } else { //该用户未设置性别 }; return 2; };
> 若调用此函数,打包时必须勾选图色模块,否则会报Thread[main (Spawn-2),5]: dlopen failed: library “libopencv\_java3.so” not found
### images.findAllPointsForColor(img, color, options)
- `img` {Image} 图片
- `color` {number} | {string} 要检测的颜色
- `options` {Object} 选项包括:
- `region` {Array} 找色区域。是一个两个或四个元素的数组。(region[0], region[1])表示找色区域的左上角;region[2]\*region[3]表示找色区域的宽高。如果只有region只有两个元素,则找色区域为(region[0], region[1])到图片右下角。如果不指定region选项,则找色区域为整张图片。
- `similarity` {number} 找色时颜色相似度,范围为0~1(越大越相似,1为颜色相等,0为任何颜色都能匹配)。
- `threshold` {number} 找色时颜色相似度的临界值,范围为0 ~ 255(越小越相似,0为颜色相等,255为任何颜色都能匹配)。默认为4。threshold和浮点数相似度(0.0~1.0)的换算为 similarity = (255 - threshold) / 255 。相似度与阈值二选一,同时存在则以相似度为准。
- 返回 {Array}
在图片中寻找所有颜色为color的点。找到时返回找到的点Point的数组,找不到时返回null。
例如找出所有白色的点:
log(images.findAllPointsForColor(img, "#ffffff"));
### 区域找色(相等) |findColorEquals
| 函数名 | 返回值 | | 备注 |
| --- | --- | --- | --- |
| images.findColorEquals() | Point | | 在图片img指定区域中找到颜色和color完全相等的某个点,并返回该点的左边;如果没有找到,则返回`null`。 |
| **参数名** | **类型** | 空 | |
| 图片 | Image | | |
| 要寻找的颜色 | 整数型/字符串 | | |
| 横坐标 | 整数型 | ✔ | 如果不指定找色区域,则在整张图片中寻找。 |
| 纵坐标 | 整数型 | ✔ | 如果不指定找色区域,则在整张图片中寻找。 |
| 区域的宽度 | 整数型 | ✔ | 如果不指定找色区域,则在整张图片中寻找。 |
| 区域的高度 | 整数型 | ✔ | 如果不指定找色区域,则在整张图片中寻找。 |
images.findColorEquals(img, color[, x, y, width, height])
- `img` {Image} 图片
- `color` {number} | {string} 要寻找的颜色
- `x` {number} 找色区域的左上角横坐标
- `y` {number} 找色区域的左上角纵坐标
- `width` {number} 找色区域的宽度
- `height` {number} 找色区域的高度
- 返回 {Point}
在图片img指定区域中找到颜色和color完全相等的某个点,并返回该点的左边;如果没有找到,则返回`null`。
找色区域通过`x`, `y`, `width`, `height`指定,如果不指定找色区域,则在整张图片中寻找。
该函数也可以作为全局函数使用。
示例:
(通过找QQ红点的颜色来判断是否有未读消息)
requestScreenCapture(); launchApp("QQ"); sleep(1200); var p = findColorEquals(captureScreen(), "#f64d30"); if(p){ toast("有未读消息"); }else{ toast("没有未读消息"); }
### 多点找色 |findMultiColors
| 函数名 | 返回值 | | 备注 |
| --- | --- | --- | --- |
| images.findMultiColors() | Point | | 多点找色,类似于按键精灵的多点找色,找不到时返回`null` |
| **参数名** | **类型** | **空** | |
| 图片 | Image | | |
| 第一个点的颜色 | 整数型/字符串 | | |
| 相对点 | 整数型数组 | | 相对于第一个点的位置和颜色的数组,数组的每个元素为[x, y, color] |
| 选项 | 对象 | ✔ | `region` {Array} 找色区域、`threshold` {number} 相似度 |
images.findMultiColors(img, firstColor, colors[, options])
- `img` {Image} 要找色的图片
- `firstColor` {number} | {string} 第一个点的颜色
- `colors` {Array} 表示剩下的点相对于第一个点的位置和颜色的数组,数组的每个元素为[x, y, color]
- `options` {Object} 选项,包括:
- `region` {Array} 找色区域。是一个两个或四个元素的数组。(region[0], region[1])表示找色区域的左上角;region[2]\*region[3]表示找色区域的宽高。如果只有region只有两个元素,则找色区域为(region[0], region[1])到图片右下角。如果不指定region选项,则找色区域为整张图片。
- `threshold` {number} 找色时颜色相似度的临界值,范围为0 ~ 255(越小越相似,0为颜色相等,255为任何颜色都能匹配)。默认为4。threshold和浮点数相似度(0.0~1.0)的换算为 similarity = (255 - threshold) / 255.
多点找色,类似于按键精灵的多点找色,其过程如下:
1. 在图片img中找到颜色firstColor的位置(x0, y0)
2. 对于数组colors的每个元素[x, y, color],检查图片img在位置(x + x0, y + y0)上的像素是否是颜色color,是的话返回(x0, y0),否则继续寻找firstColor的位置,重新执行第1步
3. 整张图片都找不到时返回`null`
例如,对于代码`images.findMultiColors(img, "#123456", [[10, 20, "#ffffff"], [30, 40, "#000000"]])`,假设图片在(100, 200)的位置的颜色为#123456, 这时如果(110, 220)的位置的颜色为#fffff且(130, 240)的位置的颜色为#000000,则函数返回点(100, 200)。
如果要指定找色区域,则在options中指定,例如:
var p = images.findMultiColors(img, "#123456", [[10, 20, "#ffffff"], [30, 40, "#000000"]], { region: [0, 960, 1080, 960] });
### 单点找色 |detectsColor
| 函数名 | 返回值 | | 备注 |
| --- | --- | --- | --- |
| images.detectsColor() | Point | | 返回图片image在位置(x, y)处是否匹配到颜色color。用于检测图片中某个位置是否是特定颜色。 |
| **参数名** | **类型** | **空** | |
| 图片 | Image | | |
| 要检测的颜色 | 整数型/字符串 | | |
| 横坐标 | 整数型 | | |
| 纵坐标 | 整数型 | | |
| 相似度 | 整数型 | ✔ | 默认为16。取值范围为0~255。 |
| 匹配算法 | 字符串 | ✔ | |
images.detectsColor(image, color, x, y[, threshold = 16, algorithm = “diff”])
- `image` {Image} 图片
- `color` {number} | {string} 要检测的颜色
- `x` {number} 要检测的位置横坐标
- `y` {number} 要检测的位置纵坐标
- `threshold` {number} 颜色相似度临界值,默认为16。取值范围为0 ~ 255。
- `algorithm` {string} 颜色匹配算法,包括:
- “equal”: 相等匹配,只有与给定颜色color完全相等时才匹配。
- “diff”: 差值匹配。与给定颜色的R、G、B差的绝对值之和小于threshold时匹配。
- “rgb”: rgb欧拉距离相似度。与给定颜色color的rgb欧拉距离小于等于threshold时匹配。
- “rgb+”: 加权rgb欧拉距离匹配([LAB Delta E](https://en.wikipedia.org/wiki/Color_difference))。
- “hs”: hs欧拉距离匹配。hs为HSV空间的色调值。
返回图片image在位置(x, y)处是否匹配到颜色color。用于检测图片中某个位置是否是特定颜色。
一个判断微博客户端的某个微博是否被点赞过的例子: requestScreenCapture(); // 找到点赞控件 var like = id("ly_feed_like_icon").findOne(); // 获取该控件中点坐标 var x = like.bounds().centerX(); var y = like.bounds().centerY(); // 截图 var img = captureScreen(); // 判断在该坐标的颜色是否为橙红色 if(images.detectsColor(img, "#fed9a8", x, y)){ // 是的话则已经是点赞过的了,不做任何动作 }else{ // 否则点击点赞按钮 like.click(); }
### images.detectsMultiColors(img, x, y, firstColor, colors, options)
- `img` {Image} 目标图片
- `x` {number} 第一个点的x坐标
- `y` {number} 第一个点的y坐标
- `firstColor` {number} | {string} 第一个点的颜色
- `colors` {Array} 表示剩下的点相对于第一个点的位置和颜色的数组,数组的每个元素为[x, y, color]
- `options` {Object} 选项,包括:
- `region` {Array} 找色区域。是一个两个或四个元素的数组。(region[0], region[1])表示找色区域的左上角;region[2]\*region[3]表示找色区域的宽高。如果只有region只有两个元素,则找色区域为(region[0], region[1])到图片右下角。如果不指定region选项,则区域为整张图片。
- `threshold` {number} 比色时颜色相似度的临界值,范围为0 ~ 255(越小越相似,0为颜色相等,255为任何颜色都能匹配)。默认为4。threshold和浮点数相似度(0.0~1.0)的换算为 similarity = (255 - threshold) / 255.
- 返回 `boolean`
多点比色,返回img在起始位置(x, y)处的多个点的颜色是否匹配。
参见`images.findMultiColors()`多点找色的文档。
log(images.detectsMultiColors(img, 100, 200, "#000000", [[3, 4, "#123456"], [8, 10, "#ff0000"]]));
### 找图 |findImage
| 函数名 | 返回值 | | 备注 |
| --- | --- | --- | --- |
| images.findImage() | Point | | 找图。模块匹配,找到时返回位置坐标(Point),找不到时返回null。 |
| **参数名** | **类型** | **空** | |
| 大图片 | Image | | |
| 小图片 | Image | | |
| 找图选项 | Object | ✔ | |
images.findImage(img, template[, options])
**[v8.5.5新增]**
- `img` {Image} 大图片
- `template` {Image} 小图片(模板)
- `options` {Object} 找图选项
找图。在大图片img中查找小图片template的位置(模块匹配),找到时返回位置坐标(Point),找不到时返回null。
选项包括:
- `threshold` {number} 图片相似度。取值范围为0~1的浮点数。默认值为0.9。
- `region` {Array} 找图区域。参见findColor函数关于region的说明。
- `level` {number} **一般而言不必修改此参数**。不加此参数时该参数会根据图片大小自动调整。找图算法是采用图像金字塔进行的, level参数表示金字塔的层次, level越大可能带来越高的找图效率,但也可能造成找图失败(图片因过度缩小而无法分辨)或返回错误位置。因此,除非您清楚该参数的意义并需要进行性能调优,否则不需要用到该参数。
该函数也可以作为全局函数使用。
一个最简单的找图例子如下:
var img = images.read("/sdcard/大图.png"); var templ = images.read("/sdcard/小图.png"); var p = findImage(img, templ); if(p){ toast("找到啦:" + p); }else{ toast("没找到"); }
稍微复杂点的区域找图例子如下:
auto(); requestScreenCapture(); var wx = images.read("/sdcard/微信图标.png"); // 返回桌面 home(); // 截图并找图 var p = findImage(captureScreen(), wx, { region: [0, 50], threshold: 0.8 }); if(p){ toast("在桌面找到了微信图标啦: " + p); }else{ toast("在桌面没有找到微信图标"); }
### 区域找图(简便) |findImageInRegion
| 函数名 | 返回值 | | 备注 |
| --- | --- | --- | --- |
| images.findImageInRegion() | Point | | 区域找图的简便方法。 |
| **参数名** | **类型** | **空** | |
| 大图片 | Image | | |
| 小图片 | Image | | |
| 横坐标 | 整数型 | | |
| 纵坐标 | 整数型 | | |
| 区域的宽度 | 整数型 | ✔ | |
| 区域的高度 | 整数型 | ✔ | |
| 图片相似度 | 浮点数 | ✔ | 取值范围为0~1的浮点数 |
images.findImageInRegion(img, template, x, y[, width, height, threshold])
区域找图的简便方法。相当于:
images.findImage(img, template, { region: [x, y, width, height], threshold: threshold })
该函数也可以作为全局函数使用。
### 区域找图(多位置) |matchTemplate
| 函数名 | 返回值 | 备注 |
| --- | --- | --- |
| images.matchTemplate() | MatchingResult对象 | 找多图,并返回搜索结果MatchingResult。 |
| **参数名** | **类型** | |
| 大图片 | Image | |
| 小图片 | Image | |
| 找图选项 | Object | |
images.matchTemplate(img, template, options)
**[v4.1.0新增]**
- `img` {Image} 大图片
- `template` {Image} 小图片(模板)
- `options` {Object} 找图选项:
- `threshold` {number} 图片相似度。取值范围为0~1的浮点数。默认值为0.9。
- `region` {Array} 找图区域。参见findColor函数关于region的说明。
- `max` {number} 找图结果最大数量,默认为5
- `transparentMask` {boolean} 是否使用透明模板找图。此选项开启后,传入的template参数可以是一个透明背景的图片对象用于匹配。此选项为 **[[Pro 8.0新增](https://pro.autojs.org/)]** 。
- `level` {number} **一般而言不必修改此参数**。不加此参数时该参数会根据图片大小自动调整。找图算法是采用图像金字塔进行的, level参数表示金字塔的层次, level越大可能带来越高的找图效率,但也可能造成找图失败(图片因过度缩小而无法分辨)或返回错误位置。因此,除非您清楚该参数的意义并需要进行性能调优,否则不需要用到该参数。
- 返回 {MatchingResult}
在大图片中搜索小图片,并返回搜索结果MatchingResult。该函数可以用于找图时找出多个位置,可以通过max参数控制最大的结果数量。也可以对匹配结果进行排序、求最值等操作。
### images.findCircles(gray, options)
- `gray` {Image} 灰度图片
- `options` {Object} 选项包括:
- `region` {Array} 找圆区域。是一个两个或四个元素的数组。(region[0], region[1])表示找圆区域的左上角;region[2]\*region[3]表示找圆区域的宽高。如果只有region只有两个元素,则找圆区域为(region[0], region[1])到图片右下角。如果不指定region选项,则找圆区域为整张图片。
- `dp` {number} dp是累加面与原始图像相比的分辨率的反比参数,dp=2时累计面分辨率是元素图像的一半,宽高都缩减为原来的一半,dp=1时,两者相同。默认为1。
- `minDst` {number} minDist定义了两个圆心之间的最小距离。默认为图片高度的八分之一。
- `param1` {number} param1是Canny边缘检测的高阈值,低阈值被自动置为高阈值的一半。默认为100,范围为0-255。
- `param2` {number} param2是累加平面对是否是圆的判定阈值,默认为100。
- `minRadius` {number} 定义了检测到的圆的半径的最小值,默认为0。
- `maxRadius` {number} 定义了检测到的圆的半径的最大值,0为不限制最大值,默认为0。
- 返回 {Array}
在图片中寻找圆(做霍夫圆变换)。找到时返回找到的所有圆{x,y,radius}的数组,找不到时返回null。
一个寻找圆的例子:
// 请求截图 requestScreenCapture(); // 截图 let img = captureScreen(); // 灰度化图片 let gray = images.grayscale(img); // 找圆 let arr = findCircles(gray, { dp: 1, minDst: 80, param1: 100, param2: 100, minRadius: 50, maxRadius: 80, }); // 回收图片 gray.recycle();