#计算机视觉 #图像处理 #截图 #模式匹配 #图像识别

spectrust

一个快速的基于 Rust 的计算机视觉库,用于在屏幕上定位图像

2 个不稳定版本

0.2.0 2023年7月19日
0.1.0 2023年7月17日

#161 in 多媒体

MIT/Apache

14KB
141

SpectRust

一个基于 Rust 的计算机视觉库,用于在屏幕上定位图像

它可以在比 pyautogui 快 3 倍的情况下搜索整个屏幕,并且不需要 OpenCV 来进行像置信度这样的选项。我还添加了一个名为 Tolerance 的选项,允许对接近原始图像像素颜色的像素进行宽容处理。纯 Rust 编写。

locate_img_center(img: &DynamicImage, region: Option<(u16, u16, u16, u16)>, min_confidence: Option<f32>, tolerance: Option<u8>) -> Option<(u32, u32, f32)>)
locate_img(img: &DynamicImage, region: Option<(u16, u16, u16, u16)>, min_confidence: Option<f32>, tolerance: Option<u8>) 

img: 必须是借用的 DynamicImage

region: 需要元组 BoundingBox (x, y, width, height)(默认整个屏幕)

min_confidence: 0.1 - 1.0,需要匹配的像素百分比(默认 0.95)

tolerance: 0 - 255,可接受的像素范围。例如,如果图像中有一个像素为 234, 52, 245,而容忍度为 10,则定位器将接受从 224, 42, 235 到 244, 62, 255 的值。(默认 5)

所有这些要求(除了 img)都是可选的,并且需要 Some()None

示例

use spectrust::*;
fn main () {
    let img = image::open("images.png").expect("Unable to locate file.");
    let region = None;
    let min_confidence = Some(0.8);
    let tolerance = Some(5);

    match locate_center_of_image(&img, region, min_confidence, tolerance) {
        Some((x, y, confidence)) => {
            println!("Image center found at {}, {} with confidence {}", x, y, confidence);
        },
        None => println!("Image not found"),
    }
}
fn main() {
    let img = image::open("images.png").expect("Unable to locate file.");

    match locate_image(&img, None, None, None) {
        Some((x, y, img_width, img_height, _confidence)) => {
            println!("x: {}, y: {}, width: {}, height: {}",x, y, img_width, img_height)
        },
        None => println!("Image not found")
    }
}

如果你在寻找图像时遇到困难。尝试降低置信度或增加容忍度。

基准测试

Python:150ms Rust:53ms(两者都在 1920x1080 屏幕上找到大小为 225x225px 的 Rust 图标)

适用于创建机器人

*反应测试(实际时间约为 12ms)
*在在线瞄准训练器上运行 20 分钟

依赖关系

~13–40MB
~401K SLoC