lj_qualibration/src/qualib_refacto.rs

128 lines
3.2 KiB
Rust

//use opencv::Result;
//use opencv::core::{self, Mat};
static DEBUG: bool = true;
pub mod annalyse;
pub mod borders;
pub mod compute_image;
use std::env::args;
use std::time::Instant;
use crate::draw::{draw_line, draw_line_dotted};
use crate::point::{Color, 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 init_border;
mod load_image;
mod save_image;
mod wait_space;
use init_border::InitBorder;
use load_image::LoadImage;
use save_image::SaveImage;
use wait_space::WaitSpace;
pub trait Sequence {
fn draw(&self, mem: &Param) -> Option<Vec<Point>>;
fn compute_sequence(&mut self, mem: &mut Param) -> Result<(), Box<dyn std::error::Error>>;
fn is_capture(&self) -> bool;
}
impl std::fmt::Debug for dyn Sequence {
fn fmt(self: &Self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{self:?}")
}
}
#[derive(Debug)]
pub struct Qualibration {
seq: Vec<Box<dyn Sequence>>,
cam: VideoCapture,
pub param: Param,
}
impl Qualibration {
pub fn new() -> Result<Self> {
//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)?;
let beg = Point::from((0., 0., 38400)); // r:150, v:0, b:0
let end = Point::from((4095., 4095., 38400)); // r:150, v:0, b:0
let seq: Vec<Box<dyn Sequence>> = vec![
Box::new(LoadImage::new()),
Box::new(WaitSpace::new()),
Box::new(InitBorder::new(beg, end)),
Box::new(SaveImage::new()),
];
//let now = std::time::Instant::now();
Ok(Qualibration {
seq,
cam,
param: Param::new(dir_name.to_owned())?,
})
}
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 mut frame = Mat::default();
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(())
}
}