cv-bridge-rs

Crates.io Docs.rs

Rust implemenation of cv_bridge that converts between ROS image messages and OpenCV images

Warning: This package is still under active development. Use at your own risk.

Getting Started

Adding cv_bridge to your project

Add the following to your Cargo.toml file under dependencies: toml [dependencies] cv-bridge = "0.2.0" or you can use cargo to add the dependency: bash cargo add cv_bridge

Converting between ROS image messages and OpenCV images

```rust use cvbridge::CvImage; use rosrust::sensormsgs::Image; use opencv::prelude::*;

fn main() { # Convert from ROS image message to OpenCV image let rosimage = Image::default(); let cvimage = CvImage::fromimgmsg(rosimage); let mat = cvimage.ascvmat("rgb8".to_string());

# Convert from OpenCV image to ROS image message
let cv_image = CvImage::from_cvmat(mat);
let ros_image = cv_image.into_imgmsg();

} ```

Using the underlying DynamicImage type

```rust use cvbridge::CvImage; use rosrust::sensormsgs::Image; use image::DynamicImage;

fn main() { # Convert from ROS image message to let rosimage = Image::default(); let cvimage = CvImage::fromimgmsg(rosimage);

# Using the underlying DynamicImage type
let image = cv_image.as_image(); # Returns a immutable reference to the underlying DynamicImage
let image = cv_image.as_mut_image(); # Returns a mutable reference to the underlying DynamicImage

} ```

Creating CvImage from a vector of bytes

```rust use cv_bridge::CvImage; use std::vec;

fn main() { let bytes:Vec = vec![0, 0, 0, 255, 255, 255, 0, 0, 0, 255, 255, 255, 0, 0, 0, 255, 255, 255, 0, 0, 0, 255, 255, 255, 0, 0, 0]; # 3x3 image with 3 channels let cvimage = CvImage::fromvec(3, 3, "rgb8".to_string(), bytes); # width, height, encoding, bytes } ```

Features