rust image找图

[dependencies]
image = "0.24.6"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use image::{GenericImageView, ImageBuffer, Rgb};
 
fn main() {
    let img_a = image::open("2.png").unwrap().to_rgb8();
    let img_b = image::open("1.png").unwrap().to_rgb8();
 
    if is_image_a_in_image_b(&img_a, &img_b) {
        println!("Image A is in Image B.");
    } else {
        println!("Image A is not in Image B.");
    }
}
 
fn is_image_a_in_image_b(img_a: &ImageBuffer<Rgb<u8>, Vec<u8>>, img_b: &ImageBuffer<Rgb<u8>, Vec<u8>>) -> bool {
    let (width_a, height_a) = img_a.dimensions();
    let (width_b, height_b) = img_b.dimensions();
 
    if width_a > width_b || height_a > height_b {
        // Image A is larger than Image B, cannot be contained in Image B.
        return false;
    }
 
    for y in 0..height_b - height_a {
        for x in 0..width_b - width_a {
            if is_image_a_at_position_in_image_b(img_a, img_b, x, y) {
                return true;
            }
        }
    }
 
    false
}
 
fn is_image_a_at_position_in_image_b(img_a: &ImageBuffer<Rgb<u8>, Vec<u8>>, img_b: &ImageBuffer<Rgb<u8>, Vec<u8>>, x: u32, y: u32) -> bool {
    let (width_a, height_a) = img_a.dimensions();
 
    for dy in 0..height_a {
        for dx in 0..width_a {
            let pixel_a = img_a.get_pixel(dx, dy);
            let pixel_b = img_b.get_pixel(x + dx, y + dy);
 
            if pixel_a != pixel_b {
                return false;
            }
        }
    }
 
    true
}

  

 

找色

1
2
3
4
5
6
7
8
9
10
11
[dependencies]
image = "0.23.14"
 
 
use image::GenericImageView;
 
fn main() {
    let img = image::open("path/to/image.png").unwrap();
    let pixel_color = img.get_pixel(10, 20);
    println!("Color at (10, 20): {:?}", pixel_color);
}

  

posted @   CrossPython  阅读(171)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2019-03-28 关于 stream, InheritedWidget, bloc 的一些看法, 比较重要
2019-03-28 Vertical viewport was given unbounded height
2019-03-28 Flutter之MaterialApp使用详解
点击右上角即可分享
微信分享提示