use anyhow::{ensure, Result};
use realsense_rust::{
config::Config,
context::Context,
frame::{DepthFrame, PixelKind},
kind::{Rs2Format, Rs2StreamKind},
pipeline::InactivePipeline,
processing_blocks::{colorizer::Colorizer, hole_filling::HoleFillingFilter},
};
use rerun as rr;
use std::{collections::HashSet, convert::TryFrom, time::Duration};
fn main() -> Result<()> {
let rec = rr::RecordingStreamBuilder::new("realsense_hole_filling").spawn()?;
let context = Context::new()?;
let devices = context.query_devices(HashSet::new());
ensure!(!devices.is_empty(), "No devices found");
println!(
"Using device \"{}\" with serial number \"{}\"",
devices[0]
.info(realsense_rust::kind::Rs2CameraInfo::Name)
.unwrap()
.to_str()
.unwrap(),
devices[0]
.info(realsense_rust::kind::Rs2CameraInfo::SerialNumber)
.unwrap()
.to_str()
.unwrap()
);
let pipeline = InactivePipeline::try_from(&context)?;
let mut config = Config::new();
config.enable_stream(Rs2StreamKind::Depth, Some(0), 640, 480, Rs2Format::Z16, 30)?;
let mut active_pipeline = pipeline.start(Some(config))?;
let mut hole_filling = HoleFillingFilter::new(5)?;
let mut colorizer_filled = Colorizer::new(5)?;
println!("Starting hole filling demonstration with Rerun visualization.");
println!("Hole filling interpolates missing depth values from neighboring pixels.");
println!("Press Ctrl+C to quit.");
let mut frame_count = 0;
loop {
let frames = active_pipeline.wait(Some(Duration::from_millis(1000)))?;
let depth_frames: Vec<DepthFrame> = frames.frames_of_type();
if let Some(depth_frame) = depth_frames.into_iter().next() {
frame_count += 1;
hole_filling.queue(depth_frame)?;
match hole_filling.wait(Duration::from_millis(100)) {
Ok(filled_frame) => {
colorizer_filled.queue(filled_frame)?;
if let Ok(filled_colorized) = colorizer_filled.wait(Duration::from_millis(100))
{
let mut filled_rgb_data = Vec::new();
for pixel in filled_colorized.iter() {
match pixel {
PixelKind::Bgr8 { b, g, r } => {
filled_rgb_data.extend_from_slice(&[*r, *g, *b]);
}
_ => {
println!(
"Unexpected pixel format in colorized frame: {:?}",
pixel
);
filled_rgb_data.extend_from_slice(&[0, 0, 0]);
}
}
}
let fld_width = filled_colorized.width() as u32;
let fld_height = filled_colorized.height() as u32;
rec.log(
"hole_filled_depth",
&rr::Image::from_rgb24(filled_rgb_data, [fld_width, fld_height]),
)?;
if frame_count % 30 == 0 {
println!("Processed {} frames with hole filling", frame_count);
}
}
}
Err(e) => {
eprintln!("Error applying hole filling filter: {}", e);
}
}
}
}
}