feat: during Qualibration and Sequence refacto

This commit is contained in:
Lapin Raving 2023-09-20 01:18:33 +02:00
parent 392455e903
commit a27e340de7
7 changed files with 190 additions and 197 deletions

View File

@ -13,23 +13,18 @@ use std::time::Instant;
use crate::draw::{draw_line, draw_line_dotted}; use crate::draw::{draw_line, draw_line_dotted};
use crate::point::{Color, Point}; use crate::point::{Color, Point};
use enum_iterator::{next, Sequence as Seq};
use opencv::core::Mat; use opencv::core::Mat;
use opencv::Result; use opencv::Result;
use std::f64::consts::PI; //use std::f64::consts::PI;
use opencv::core::{bitwise_and, find_file, in_range, Point as OcvPoint, Scalar, Size, Size_};
use opencv::core::{VecN, Vector};
use opencv::imgcodecs::imwrite;
use opencv::imgcodecs::{imread, IMREAD_COLOR};
use opencv::imgproc::{canny, cvt_color, line, COLOR_BGR2GRAY};
use opencv::prelude::*; use opencv::prelude::*;
use opencv::{ use opencv::{
highgui, highgui,
videoio::{self, VideoCapture}, videoio::{self, VideoCapture},
}; };
use std::fs::create_dir;
use std::fs::read_dir; mod param;
use param::Param;
mod init_border; mod init_border;
mod load_image; mod load_image;
@ -40,71 +35,6 @@ use load_image::LoadImage;
use save_image::SaveImage; use save_image::SaveImage;
use wait_space::WaitSpace; use wait_space::WaitSpace;
//impl Clone for dyn Sequence {
// fn clone() {
// }
//}
#[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<Self> {
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)
}
}
//impl
//#[derive(Copy)]
pub trait Sequence { pub trait Sequence {
fn draw(&self, mem: &Param) -> Option<Vec<Point>>; fn draw(&self, mem: &Param) -> Option<Vec<Point>>;
fn compute_sequence(&mut self, mem: &mut Param) -> Result<(), Box<dyn std::error::Error>>; fn compute_sequence(&mut self, mem: &mut Param) -> Result<(), Box<dyn std::error::Error>>;
@ -117,96 +47,11 @@ impl std::fmt::Debug for dyn Sequence {
} }
} }
#[derive(Clone, Debug)]
pub struct Param {
seq_id: usize,
imgs: Vec<Vec<Mat>>,
dst_size: i32,
r: i32,
g: i32,
b: i32,
nb_all: i32,
nb_visible: i32,
nb_liss: i32,
tresh: Treshold,
canny_v1: i32,
canny_v2: i32,
hough_param: HoughLine,
border_pt: Vec<(f64, f64)>,
homography: Mat,
h_size: Size_<i32>,
line_pos: Vec<i32>,
multiple: u16, // le nombre de fois qu'une photo est prise pour certaine sequence
pub key: i32,
dir_name: String,
begin: Instant,
pub capture_mode: bool,
}
impl Param {
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(())
}
fn load_image(&mut self) -> Result<(), Box<dyn std::error::Error>> {
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)] #[derive(Debug)]
pub struct Qualibration { pub struct Qualibration {
seq: Vec<Box<dyn Sequence>>, seq: Vec<Box<dyn Sequence>>,
cam: VideoCapture, cam: VideoCapture,
cnt: usize,
pub param: Param, pub param: Param,
} }
@ -241,37 +86,7 @@ impl Qualibration {
Ok(Qualibration { Ok(Qualibration {
seq, seq,
cam, cam,
cnt: 0, param: Param::new(dir_name.to_owned())?,
param: Param {
begin: std::time::Instant::now(),
dir_name: dir_name.clone(),
capture_mode: dir_name.len() == 0,
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,
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,
},
}) })
} }
@ -307,7 +122,6 @@ impl Qualibration {
self.seq[self.param.seq_id].compute_sequence(&mut self.param)?; self.seq[self.param.seq_id].compute_sequence(&mut self.param)?;
} }
self.cnt += 1;
Ok(()) Ok(())
} }
} }

View File

@ -25,7 +25,7 @@ opencv::not_opencv_branch_4! {
use opencv::core::LINE_AA; use opencv::core::LINE_AA;
} }
use super::Treshold; use super::param::Treshold;
const MAX_TRACKBAR: i32 = 255; const MAX_TRACKBAR: i32 = 255;
pub fn draw_histograme_dbg( pub fn draw_histograme_dbg(

View File

@ -1,4 +1,4 @@
use super::HoughLine; use super::param::HoughLine;
use crate::utils::{CartesianEquation, EqAffine, Pt}; use crate::utils::{CartesianEquation, EqAffine, Pt};
use opencv::core::{add, subtract, Mat, VecN, Vector, CV_8UC3}; use opencv::core::{add, subtract, Mat, VecN, Vector, CV_8UC3};
//use opencv::prelude::MatTraitConst; //use opencv::prelude::MatTraitConst;

View File

@ -1,4 +1,4 @@
use super::Treshold; use super::param::Treshold;
use opencv::core::{self, bitwise_and, in_range, Mat, Scalar, Size_}; use opencv::core::{self, bitwise_and, in_range, Mat, Scalar, Size_};
use opencv::imgproc; use opencv::imgproc;
use opencv::Result; use opencv::Result;

View File

@ -1,6 +1,6 @@
use crate::draw::draw_line; use crate::draw::draw_line;
use crate::point::{Color, Point}; use crate::point::{Color, Point};
use crate::qualib_refacto::{HoughLine, Param, Sequence}; use crate::qualib_refacto::{param::{Param, HoughLine}, Sequence};
use crate::qualib_refacto::annalyse::image_diff; use crate::qualib_refacto::annalyse::image_diff;
use crate::qualib_refacto::borders::{ use crate::qualib_refacto::borders::{

181
src/qualib_refacto/param.rs Normal file
View File

@ -0,0 +1,181 @@
use opencv::{Result, core::{Mat, Size_, Size, Vector, find_file}};
use opencv::imgcodecs::{imread, IMREAD_COLOR, imwrite};
use std::time::Instant;
use std::fs::{create_dir, read_dir};
use std::f64::consts::PI;
#[derive(Clone, Debug)]
pub struct Param {
pub seq_id: usize,
pub imgs: Vec<Vec<Mat>>,
pub dst_size: i32,
pub r: i32,
pub g: i32,
pub b: i32,
pub nb_all: i32,
pub nb_visible: 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_<i32>,
pub line_pos: Vec<i32>,
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<Self> {
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,
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<dyn std::error::Error>> {
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<Self> {
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)
}
}

View File

@ -12,8 +12,6 @@ impl WaitSpace {
} }
impl Sequence for WaitSpace { impl Sequence for WaitSpace {
//type Obj = Self;
fn draw(&self, mem: &Param) -> Option<Vec<Point>> { fn draw(&self, mem: &Param) -> Option<Vec<Point>> {
if mem.key == 32 { if mem.key == 32 {
return None; return None;