Can I use this crate to find out if an image contains a specific color? #46
Replies: 1 comment 1 reply
-
If you want to test an image for a specific RGB value, then you'd want to look at the individual pixels. Using the use image::{Rgb, RgbImage};
fn main() {
let img = RgbImage::from_vec(2, 1, vec![0, 0, 0, 255, 0, 0]).unwrap(); // random image data
let contains_color = img.pixels().any(|&p| p == Rgb([255, 0, 0]));
if contains_color {
println!("Pixel found!");
}
} If it's more of a fuzzy search, you might want to use some type of color difference formula to calculate a difference. If the difference is below an acceptable tolerance, then you can consider that color to be in the image. Exact pixel colors that you're testing for might not be in the image but "close enough" pixels may be present. Hope that helps. |
Beta Was this translation helpful? Give feedback.
-
I'm a new Rust user and I want to test if an image contains a specific color. Can I use this crate? I skimmed through the document but still not sure. Thanks.
Beta Was this translation helpful? Give feedback.
All reactions