twistedfall / opencv-rust

Rust bindings for OpenCV 3 & 4
MIT License
1.96k stars 157 forks source link

[Question] Access Mat from BoxedRef<Mat> without cloning #576

Closed mexicantexan closed 4 months ago

mexicantexan commented 4 months ago

I was going through and updating the opencv dependency in my Rust project. Due to some breaking changes, functions that previously returned a Mat now return a BoxedRef. How would I return the underlying Mat without cloning? Here is an example couple lines of code:

pub fn crop(input: &Mat, x: i32, y: i32, width: i32, height: i32) -> opencv::Result<Mat, RipsError> {
    let cropped = Mat::roi(input, Rect::new(x, y, width, height))?;
    Ok(cropped)
}

Now returns the error:

Type mismatch [E0308] expected `Mat`, but found `BoxedRef<Mat>`

Is there any way to "unwrap" the Mat out of the BoxedRef? I'm trying to avoid cloning if possible, but I'm a bit confused on how to do this with how the crate currently functions.

Opencv Version: 0.91.3 OS: Ubuntu 22.04 Edition: 2021 Cargo Version: 1.77

twistedfall commented 4 months ago

Not being able to extract Mat to not lose lifetime information was one of the goals of introducing BoxedRef so it’s not possible. To fix the issue you’re encountering you’ll need to update the signature of crop function to take &impl MatTraitConst instead of &Mat to match what roi function expects.