realsense-rust 1.3.0

High-level RealSense library in Rust
Documentation
//! This example demonstrates the hole filling filter processing block with Rerun visualization
//! It opens a depth stream, applies hole filling to fill invalid depth pixels, and visualizes the results

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<()> {
    // Initialize Rerun
    let rec = rr::RecordingStreamBuilder::new("realsense_hole_filling").spawn()?;

    // Create RealSense context and check for available devices
    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()
    );

    // Create pipeline and configuration
    let pipeline = InactivePipeline::try_from(&context)?;
    let mut config = Config::new();

    // Configure depth stream
    config.enable_stream(Rs2StreamKind::Depth, Some(0), 640, 480, Rs2Format::Z16, 30)?;

    // Start pipeline
    let mut active_pipeline = pipeline.start(Some(config))?;

    // Create processing blocks
    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)))?;

        // Get depth frame
        let depth_frames: Vec<DepthFrame> = frames.frames_of_type();
        if let Some(depth_frame) = depth_frames.into_iter().next() {
            frame_count += 1;

            // Apply hole filling filter
            hole_filling.queue(depth_frame)?;

            match hole_filling.wait(Duration::from_millis(100)) {
                Ok(filled_frame) => {
                    // Colorize the hole-filled frame
                    colorizer_filled.queue(filled_frame)?;

                    if let Ok(filled_colorized) = colorizer_filled.wait(Duration::from_millis(100))
                    {
                        // Convert filled colorized frame to RGB data for Rerun
                        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]);
                                }
                                _ => {
                                    // Fallback for other formats
                                    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);
                }
            }
        }
    }
}