-
Notifications
You must be signed in to change notification settings - Fork 623
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
OpenCV Mat to DynamicImage conversion #1723
Comments
I think this probably fits best as a plugin crate. But far as creating
|
Re: The other way around also looks intriguing, finding a good way to safely expose struct ImageMat<'a>(Mat, PhantomData<&'a DynamicImage>);
trait AsMat: Sealed {
fn as_mat(&mut self) -> ImageMat<'_>;
}
impl AsMat for image::RgbImage {
fn as_mat(&mut self) -> ImageMat<'_> {
let (rows, cols) = self.dimensions();
let data = self.as_mut_ptr();
let mat = Mat::new_rows_cols_with_data(rows, cols, CV_8UC3, data, Mat_AUTO_STEP);
ImageMat(mat, PhantomData)
}
}
//etc, additional impls for other image types I'm still not sure if a conversion of |
@HeroicKatora it is actually much easier I'm afraid (glad). There is data_bytes and data_bytes_mut, and they are safe. Already experimented with it and there is no apparent performance hit. Now my problem seems rather trivial but I'm finding many walls: Opencv's Mat internal representation works with BGR. Since image does not provide BGR Pixel implementation it makes it difficult. Now, I already went ahead and implemented Pixel for a custom Bgr type and it theoretically worked. I say this because I was not able to save the buffer because my Bgr type does not implement PixelWithColorType and it can't because The solutions I see:
Now, what solutions am I not seeing? |
Just to clarify, the situation above is only a problem when trying to create an ImageBuffer using zero-copy. This is not a problem when allocating DynamicImage and manually manipulating the pixels. |
We can't provide a zero-copy path through the encoder in any case. Most decoders don't support the on-the-fly transpose and there is no interface to configure it. That's the reason Handling image matrices in general is quite intricate, as it turns out. That's also why |
I would like to be able to transform Mat to DynamicImage.
My specific use case for this functionality is using VideoCapture to get video or network stream and use it with
image
s API.This is more generally applicable to any use case that wants to interoperate between the 2 crates.
Draft
Is this something desirable?
Maybe improve mat2image and make it a PR to implement it into mainstream? or is it OK to live as a separate "plugin crate"?
I'd love some feedback on the implementation.
The text was updated successfully, but these errors were encountered: