lj_qualibration/src/qualibration.rs

136 lines
4.0 KiB
Rust

//use opencv::Result;
//use opencv::core::{self, Mat};
#[allow(dead_code)]
static DEBUG: bool = true;
pub mod annalyse;
pub mod borders;
pub mod compute_image;
use std::env::args;
use crate::point::Point;
use opencv::core::Mat;
use opencv::Result;
//use std::f64::consts::PI;
use opencv::prelude::*;
use opencv::{
highgui,
videoio::{self, VideoCapture},
};
mod param;
use param::Param;
mod sequence;
pub use sequence::{InitBorder, InitIdcode, LineDotted, LoadImage, SaveImage, Sequence, WaitSpace};
#[derive(Debug)]
pub struct Qualibration {
seq: Vec<Box<dyn Sequence>>,
cam: VideoCapture,
pub param: Param,
}
impl Qualibration {
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
//let v: Vec<Box<dyn Sequence>> = vec![];
let mut dir_name = "".to_string(); //"building.jpg".to_string(); // by default
if let Some(dir_name_arg) = args().nth(1) {
dir_name = dir_name_arg;
}
let mut cam = videoio::VideoCapture::new(0, videoio::CAP_ANY)?; // 0 is the default camera ;
let opened_cam = videoio::VideoCapture::is_opened(&cam)?;
if !opened_cam {
panic!("Unable to open default camera!");
}
let mut frame = Mat::default();
cam.read(&mut frame)?;
// 9830400 -> r:150, v:0, b:0
let beg = Point::from((0., 0., 9830400));
let beg2 = Point::from((0., 4095., 9830400));
let end = Point::from((4095., 4095., 9830400));
let seq: Vec<Box<dyn Sequence>> = vec![
Box::new(WaitSpace::new(beg, end, 400)),
Box::new(InitBorder::new(beg, end, 400)),
Box::new(LineDotted::new(beg, end, 2, true, false, 400)),
//Box::new(InitIdcode::new(beg2, end, 400)),
];
let seq_names = get_sequence_name(&seq);
let mut param = Param::new(dir_name.to_owned(), seq_names)?;
if !param.capture_mode {
param.load_image()?;
}
Ok(Qualibration { seq, cam, param })
}
pub fn draw_sequence(&mut self) -> Option<Vec<Point>> {
if self.param.seq_id >= self.seq.len() {
return None;
}
let pl = self.seq[self.param.seq_id].draw(&self.param);
if pl.is_none() {
self.param.seq_id += 1;
if self.param.capture_mode {
self.param.imgs.push(vec![]);
}
}
pl
}
pub fn run_step(self: &mut Self) -> Result<(), Box<dyn std::error::Error>> {
let seq_id = self.param.seq_id;
let seq_name = self.seq[seq_id].sequence_name();
println!("\n\n\n\n\n\n\n\n");
println!("\t\t==================================================================");
println!("\t\t==================================================================");
println!("\t\t==================================================================");
println!("\n\n\n\n\n\n\n\n");
println!("seq[{seq_id}]: {seq_name}");
if self.param.capture_mode {
let millis_nb = self.seq[self.param.seq_id].wait_milis();
let millis = std::time::Duration::from_millis(millis_nb); // TODO: find solution to know when change has been done
std::thread::sleep(millis);
}
let mut frame = Mat::default();
//println!("sequence: {}:{:?}", self.param.seq_id, &self.seq[self.param.seq_id]);
if self.param.capture_mode {
self.cam.read(&mut frame)?;
highgui::imshow("camera", &frame)?;
if frame.size()?.width > 0 && self.seq[self.param.seq_id].is_capture() {
self.param.imgs[self.param.seq_id].push(frame.clone());
}
}
if frame.size()?.width > 0 || !self.param.capture_mode {
self.seq[self.param.seq_id].compute_sequence(&mut self.param)?;
}
Ok(())
}
}
pub fn get_sequence_name(seq: &Vec<Box<dyn Sequence>>) -> Vec<String> {
let mut v = vec![];
for i in 0..seq.len() {
v.push(seq[i].sequence_name());
}
v
}