average bright spot

This commit is contained in:
2024-11-20 22:26:10 +00:00
parent ff378888a3
commit 419aa50e82

View File

@@ -4,10 +4,12 @@ use nokhwa::{
Camera,
};
use image::DynamicImage;
use image::{DynamicImage, GenericImage, GenericImageView, Rgb, Rgba};
use eframe::egui;
const RED: [u8; 4] = [255, 0, 0, 0];
fn main() {
// first camera in system
let index = CameraIndex::Index(0);
@@ -24,10 +26,29 @@ fn main() {
println!("Captured Single Frame of {}", frame.buffer().len());
// decode into an ImageBuffer
let decoded = frame.decode_image::<RgbFormat>().unwrap();
let mut img = DynamicImage::from(decoded);
for pxl in decoded.iter() {
let mut bright_spots: Vec<(u32, u32)> = vec![];
for (i, j, pxl) in img.pixels() {
let sum = pxl.0[0] as u32 + pxl.0[1] as u32 + pxl.0[2] as u32;
if sum > 250 * 3 {
bright_spots.push((i, j));
}
}
DynamicImage::from(decoded).save("test.png");
let mut sum = [0, 0];
let n = bright_spots.len();
for (i, j) in bright_spots {
sum[0] += i;
sum[1] += j;
}
sum[0] /= n as u32;
sum[1] /= n as u32;
img.put_pixel(sum[0], sum[1], Rgba(RED));
img.save("test.png");
}