rust image找图
[dependencies]
image = "0.24.6"
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 }
找色
[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); }