From f66047568e589ab4e9023626788296914a789935 Mon Sep 17 00:00:00 2001 From: Andrew Freeman Date: Mon, 9 Sep 2024 15:18:31 -0500 Subject: [PATCH] cv-convert feature gate --- Cargo.toml | 5 ++++- src/main.rs | 2 +- src/util/event_adder.rs | 28 +++++++++++++++++++++++++--- src/util/mod.rs | 19 ++++++++++++++++++- src/util/reconstructor.rs | 14 +++++++++++--- 5 files changed, 59 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f32867b..bcddd7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ categories = ["multimedia::encoding", "science"] exclude = ["dataset", "**/*.gif"] [dependencies] -opencv = { version = "0.84.5", default-features = false, features = [ +opencv = { version = "0.92.3", default-features = false, features = [ "clang-runtime", "highgui", "imgproc","videoio"] } @@ -34,9 +34,12 @@ num-derive = "0.3.3" async-scoped = { version = "0.7.0", features = ["use-tokio"] } async-trait = "0.1.57" +[features] +convert = ["cv-convert"] [dependencies.cv-convert] version = '0.25.0' +optional = true default-features = false features = [ 'opencv_0-84', diff --git a/src/main.rs b/src/main.rs index fe56e31..e8c32e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,7 +42,7 @@ async fn main() -> Result<(), Box> { // /mnt/tmp is a mounted ramdisk, eg.: // sudo mount -t tmpfs -o rw,size=20G tmpfs /mnt/tmp let mut cv_video_writer = VideoWriter::new( - "/mnt/tmp/tmp.avi", + "~/Downloads/tmp.avi", opencv::videoio::VideoWriter::fourcc('M', 'J', 'P', 'G').unwrap(), 30.0, opencv::core::Size::new(reconstructor.width as i32, reconstructor.height as i32), diff --git a/src/util/event_adder.rs b/src/util/event_adder.rs index e3b0f2b..5297cd6 100644 --- a/src/util/event_adder.rs +++ b/src/util/event_adder.rs @@ -1,6 +1,6 @@ use aedat::base::Packet; use aedat::events_generated::Event; -use cv_convert::TryFromCv; +// use cv_convert::TryFromCv; use nalgebra::{DMatrix, Dyn, OMatrix}; use opencv::core::{ create_continuous, mean, no_array, normalize, sqrt, sum_elems, ElemMul, Mat, MatExprTraitConst, @@ -158,6 +158,10 @@ impl EventAdder { // Take the exp of L^tilde(t) to get L(t), the final latent image event_counter.mul_assign(c); event_counter = event_counter.map(|x: f64| x.exp()); + #[cfg(not(feature ="cv-convert"))] + let event_counter_mat = crate::util::omatrix_to_mat(&event_counter); + + #[cfg(feature ="cv-convert")] let event_counter_mat = Mat::try_from_cv(event_counter).unwrap(); self.latent_image @@ -304,9 +308,16 @@ impl EventAdder { let mut latent_image = DMatrix::::zeros(self.height as usize, self.width as usize); let mut edge_image = latent_image.clone(); if self.event_during_queue.is_empty() { + + #[cfg(not(feature ="cv-convert"))] + return ( + omatrix_to_mat(&self.blur_info.as_ref().unwrap().blurred_image.clone_owned()), + omatrix_to_mat(&edge_image), + ); + + #[cfg(feature ="cv-convert")] return ( - Mat::try_from_cv(self.blur_info.as_ref().unwrap().blurred_image.clone_owned()) - .unwrap(), + Mat::try_from_cv(self.blur_info.as_ref().unwrap().blurred_image.clone_owned()).unwrap(), Mat::try_from_cv(edge_image).unwrap(), ); } @@ -409,6 +420,16 @@ impl EventAdder { } // show_display_force("latent", &latent_image, 1, false); + // If the "cv-convert" feature is enabled + #[cfg(not(feature ="cv-convert"))] + { + ( + omatrix_to_mat(&latent_image), + omatrix_to_mat(&edge_image), + ) + } + + #[cfg(feature ="cv-convert")] ( Mat::try_from_cv(latent_image).unwrap(), Mat::try_from_cv(edge_image).unwrap(), @@ -538,6 +559,7 @@ fn event_polarity_float(event: &Event) -> f64 { } use opencv::imgproc::{sobel, threshold, THRESH_BINARY}; +use crate::util::omatrix_to_mat; pub struct BlurInfo { pub blurred_image: OMatrix, diff --git a/src/util/mod.rs b/src/util/mod.rs index c4ef27e..3beeb6b 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,3 +1,20 @@ +use nalgebra::{Dyn, OMatrix}; +use opencv::core::{Mat, MatExprTraitConst, MatTrait, CV_64F}; + pub(crate) mod event_adder; pub mod reconstructor; -mod threaded_decoder; \ No newline at end of file +mod threaded_decoder; + +fn omatrix_to_mat(omatrix: &OMatrix) -> Mat { + let rows = omatrix.nrows() as i32; + let cols = omatrix.ncols() as i32; + let mut mat = Mat::zeros(rows, cols, CV_64F).unwrap().to_mat().unwrap(); + + for i in 0..rows { + for j in 0..cols { + *mat.at_2d_mut::(i, j).unwrap() = omatrix[(i as usize, j as usize)]; + } + } + + mat +} \ No newline at end of file diff --git a/src/util/reconstructor.rs b/src/util/reconstructor.rs index 6b52e85..eaab473 100644 --- a/src/util/reconstructor.rs +++ b/src/util/reconstructor.rs @@ -4,10 +4,10 @@ use aedat::base::{Decoder, ParseError, Stream, StreamContent}; use crate::util::reconstructor::ReconstructorError::ArgumentError; use crate::util::threaded_decoder::{setup_packet_threads, PacketReceiver, TimestampedPacket}; use aedat::events_generated::Event; -use cv_convert::TryFromCv; -use nalgebra::DMatrix; +// use cv_convert::TryFromCv; +use nalgebra::{DMatrix, Dyn, OMatrix}; use num_traits::FromPrimitive; -use opencv::core::{Mat, MatTrait, MatTraitConst, Size, CV_8S, NORM_MINMAX}; +use opencv::core::{Mat, MatTrait, MatTraitConst, Size, CV_64F, CV_8S, NORM_MINMAX}; use opencv::highgui; use opencv::imgproc::resize; use simple_error::SimpleError; @@ -67,6 +67,8 @@ pub enum ReconstructorError { ArgumentError(String), } + + impl Reconstructor { pub async fn new( directory: String, @@ -336,9 +338,15 @@ impl Reconstructor { let deblur_res = { if self.show_blurred_display { + #[cfg(not(feature ="cv-convert"))] + let tmp_blurred_mat = crate::util::omatrix_to_mat(&self.event_adder.blur_info.as_ref().unwrap().blurred_image); + + + #[cfg(feature ="cv-convert")] let tmp_blurred_mat = Mat::try_from_cv(&self.event_adder.blur_info.as_ref().unwrap().blurred_image) .unwrap(); + _show_display_force("blurred input", &tmp_blurred_mat, 1, false); } deblur_image(&mut self.event_adder)