Rust opencv::imgcodecs::imwrite

Hello,
I am wondering if someone can help advise on the following issue I have, although the question is specific to the opencv image processing crate. I have been trying for a few hours to save an image using the imwrite function below.

In C++ I can just pass in the destination file name e.g. "image.jpg" along with the Mat object. The 3rd argument for parameters is optional in C++ and I usually don't specify anything.

When I use this function in Rust the compiler tells me that I need to specify all 3 options. I don't need to specify any parameters for my program, but I am willing to do so to satisfy the Rust compiler. The trouble is I do not know how to specify a "VectorOfInt" or what format this would take.

I am hoping someone with good Rust coding knowledge can help out even if they are not familiar with OpenCV. The crate documentation does not seem to have any examples where an image is saved that I could learn from.

Thanks,
djme.

My code snip:
imgcodecs::imwrite(&filename, &frame, VectorOfint - not sure here )?;

https://docs.rs/opencv/0.8.0/opencv/imgcodecs/fn.imwrite.html
pub fn imwrite(filename: &str, img: &Mat, params: &VectorOfint) -> Result<bool>

Documentation:
Parameters:

  • filename: Name of the file.
  • img: Image to be saved.
  • params: Format-specific parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ... .) see cv::ImwriteFlags

https://docs.rs/opencv/0.8.0/opencv/types/struct.VectorOfint.html

It looks like you should be able to just create an empty VectorOfint and pass that:

let params = opencv::types::VectorOfint::new();
imwrite(&filename, &frame, &params);

Thanks for your reply asymmetrikon.

I had previously tried your suggestion but receive the following error when I attempt to compile. I'll keep playing around with it and report back if I can find a solution.

Thanks,
djme.

error[E0599]: no function or associated item named new found for type opencv::opencv::hub::types::VectorOfint in the current scope
--> src/camera.rs:109:50
|
109 | let params = opencv::types::VectorOfint::new();
| ^^^ function or associated item not found in opencv::opencv::hub::types::VectorOfint
|
= help: items from traits can only be used if the trait is in scope
= note: the following trait is implemented but not in scope, perhaps add a use for it:
use opencv::templ::vector::Vector;

Which version of opencv are you using? The documentation you linked was 0.8.0, and that has a new function, but the most recent version (0.19.1) doesn't. If you're using that version, you have to include the Vector trait with use opencv::prelude::Vector; - then you should be able to call VectorOfint::new().

Hi asymetrikon,

Yes you are correct!

In the past few minutes I found some test code below which demonstrates usage of the VectorOfint type. I had no idea why the code you suggested (i.e. VectorOfint::new(); ) did not work even though I had included "use opencv::types::VectorOfint;"

I noticed that the link below also included "use opencv::prelude::*," so I included that too and it worked!!
I was about to come back and reply to you, not knowing why it now worked but I see you have replied in the mean time with the answer :smile:

From my Cargo.toml file it looks like I am using opencv version 0.18.. I checked the 0.18 documentation for the VectorOfint type and just like you said, there is no new() method.

Thank you replying and for pointing out the exact issue. I should have paid more attention to the documentation version, but I also learned a few things from your reply.

Thanks again!

https://github.com/twistedfall/opencv-rust/blob/master/tests/vector.rs

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.