-
-
Notifications
You must be signed in to change notification settings - Fork 170
Fastest way of converting between OpenCV Mat and Rust ndarray (or other Rust arrays)? Or even better, zero-copy? #267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Zero-copy would not be safe because the allocation and reallocation of the memory blocks would have to be done by different libraries. But do check out this crate: https://crates.io/crates/cv-convert The author is handling this conversion between popular cv libraries. |
Sounds great. Thanks! |
@twistedfall Hi I think maybe it can be safe sometimes? Pseudocode:
doc: https://docs.rs/opencv/0.54.0/opencv/imgproc/fn.resize.html Could you please provide some suggestions? Thanks |
In version 0.55 you can now call |
I've run some tests and it indeed works without copying the data, you just need to use the let mat = unsafe {
Mat::new_rows_cols_with_data(
1,
bytes.len() as i32,
u8::typ(),
bytes.as_mut_ptr() as *mut c_void,
core::Mat_AUTO_STEP,
)?
}; Also if you modify the resulting |
Thanks! Shall we have a wrapper for it? For example, |
For now there is no way to specify the |
Ok... Thank you all the same! |
I agree. Mat is reference counting, so it can make tons of references everywhere. Therefore, even if the Mat we construct from the &[u8] correctly drops within lifetime, there can be lots of references in other places. Then, when the &[u8]'s owner really drops the Vec, all those references become dangling. |
True that |
|
Hi thanks for the lib! I wonder what is the fastest way of converting between OpenCV Mat and Rust ndarray (or other Rust arrays)? Obviously I can iterate each element and assign the value, but it will be terribly slow. I guess there should be something like memcpy? Or even better, can we have zero-copy when converting between Mat and ndarray?
The text was updated successfully, but these errors were encountered: