use opencv::imgcodecs::{imread, imwrite, IMREAD_COLOR}; use opencv::{ core::{find_file, Mat, Size, Size_, Vector}, Result, }; use std::f64::consts::PI; use std::fs::{create_dir, read_dir}; use std::time::Instant; #[derive(Clone, Debug)] pub struct Param { pub seq_id: usize, pub imgs: Vec>, pub dst_size: i32, pub r: i32, pub g: i32, pub b: i32, pub nb_all: i32, pub nb_visible: i32, pub nb_wait: i32, pub nb_liss: i32, pub tresh: Treshold, pub canny_v1: i32, pub canny_v2: i32, pub hough_param: HoughLine, pub border_pt: Vec<(f64, f64)>, pub homography: Mat, pub h_size: Size_, pub line_pos: Vec, pub multiple: u16, // le nombre de fois qu'une photo est prise pour certaine sequence pub key: i32, pub dir_name: String, pub begin: Instant, pub capture_mode: bool, } impl Param { pub fn new(dir_name: String) -> Result { Ok(Self { begin: std::time::Instant::now(), capture_mode: dir_name.len() == 0, dir_name, key: -1, imgs: vec![vec![]], seq_id: 0, dst_size: 900, r: 150, g: 0, b: 0, nb_all: 120, nb_visible: 40, nb_liss: 10, nb_wait: 30, tresh: Treshold::new("histogram", 160, 255)?, canny_v1: 170, canny_v2: 255, hough_param: HoughLine { rho: 100, theta: 100, treshold: 30, min_length: 0, max_line_gap: 50000, }, border_pt: vec![], homography: Mat::default(), h_size: Size::default(), line_pos: vec![4095; 34], multiple: 20, }) } pub fn save_image(&self) -> Result<()> { let now = self.begin; let img_root = format!("image"); create_dir(&img_root).unwrap_or(()); let new_dir = format!( "{img_root}/{:0>6?}_{:0>9?}", now.elapsed().as_millis(), now.elapsed().as_nanos() ); create_dir(&new_dir).unwrap_or(()); for (i, img_seq) in self.imgs.iter().enumerate() { let seq_dir_name = format!("{new_dir}/{i}"); create_dir(&seq_dir_name).unwrap_or(()); for img in img_seq { let mut name_img = format!("{seq_dir_name}/"); name_img.push_str(&format!("img_{i}.png")); imwrite(&name_img, img, &Vector::from_slice(&[6, 6, 6, 0]))?; } } Ok(()) } pub fn load_image(&mut self) -> Result<(), Box> { let mut imgs = vec![]; let paths = read_dir(&self.dir_name)?; for entry in paths { let mut seq_img = vec![]; let dir = entry?; let path = dir.path(); // sequence directory let names: Vec<&str> = path.to_str().unwrap().split("/").collect(); let seq_id: usize = names[names.len() - 1].parse()?; for entry in read_dir(&path)? { let sub_path = entry?.path(); let names: Vec<&str> = path.to_str().unwrap().split("/").collect(); let img_name = names[names.len() - 1]; let img_id: usize = img_name[4..img_name.len() - 4].parse()?; let img: Mat = imread( &find_file(&sub_path.to_str().unwrap(), false, false)?, IMREAD_COLOR, )?; seq_img.push((img_id, img)); } imgs.push((seq_id, seq_img)); } self.imgs = vec![vec![]; imgs.len()]; for (seq_id, seq_img) in imgs { self.imgs[seq_id] = vec![Mat::default(); seq_img.len()]; for (img_id, img) in seq_img { self.imgs[seq_id][img_id] = img; } } Ok(()) } } #[derive(Debug, Clone)] pub struct HoughLine { pub rho: i32, pub theta: i32, pub treshold: i32, pub min_length: i32, pub max_line_gap: i32, } // ca c'est les donner qu'on envoie a la fonction pub struct HoughLineValue { pub rho: f64, pub theta: f64, pub treshold: i32, pub min_length: f64, pub max_line_gap: f64, } impl HoughLine { pub fn get_param(&self) -> HoughLineValue { HoughLineValue { rho: self.rho as f64 / 100., theta: self.theta as f64 / 100. * PI / 180., treshold: self.treshold, min_length: self.min_length as f64 / 100., max_line_gap: self.max_line_gap as f64 / 100., } } } #[derive(Clone, Debug)] pub struct Treshold { pub win_name: String, pub min_0: i32, pub min_1: i32, pub min_2: i32, pub max_0: i32, pub max_1: i32, pub max_2: i32, } impl Treshold { pub fn new(name: &str, min: i32, max: i32) -> Result { let tresh = Treshold { win_name: name.to_owned(), min_0: min, min_1: min, min_2: min, max_0: max, max_1: max, max_2: max, }; Ok(tresh) } }