Skip to content

Commit

Permalink
cv-convert feature gate
Browse files Browse the repository at this point in the history
  • Loading branch information
ac-freeman committed Sep 9, 2024
1 parent 4e585d6 commit f660475
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// /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),
Expand Down
28 changes: 25 additions & 3 deletions src/util/event_adder.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -304,9 +308,16 @@ impl EventAdder {
let mut latent_image = DMatrix::<f64>::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(),
);
}
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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<f64, Dyn, Dyn>,
Expand Down
19 changes: 18 additions & 1 deletion src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
mod threaded_decoder;

fn omatrix_to_mat(omatrix: &OMatrix<f64, Dyn, Dyn>) -> 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::<f64>(i, j).unwrap() = omatrix[(i as usize, j as usize)];
}
}

mat
}
14 changes: 11 additions & 3 deletions src/util/reconstructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,6 +67,8 @@ pub enum ReconstructorError {
ArgumentError(String),
}



impl Reconstructor {
pub async fn new(
directory: String,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f660475

Please sign in to comment.