cv_convert_fork/
lib.rs

1//! Data conversion among computer vision libraries.
2//!
3//! # Version Selection
4//!
5//! In the default setting, up-to-date dependencies are used. The
6//! default dependency versions are listed in `[features]` in
7//! cv-convert `Cargo.toml`.
8//!
9//! You can manually select desired dependency versions. The choices
10//! of dependency versions are named accordingly as Cargo
11//! features. For example, the feature `nalgebra_0-31` enables
12//! nalgebra 0.31.x.  It allows to list crate version selections in
13//! `Cargo.toml`.
14//!
15//! ```toml
16//! [dependencies.cv-convert]
17//! version = 'x.y.z'
18//! default-features = false
19//! features = [
20//!     'image_0-24',
21//!     'opencv_0-76',
22//!     'tch_0-10',
23//!     'nalgebra_0-32',
24//!     'ndarray_0-15',
25//! ]
26//! ```
27//!
28//! It's impossible to enable two or more versions for a
29//! dependency. For example, `nalgebra_0-31` and `nalgebra_0-32` are
30//! incompatible.
31//!
32//! # Traits
33//!
34//! The traits [FromCv] and [IntoCv] provide `.from_cv()` and `.into_cv()`, and
35//! traits [TryFromCv] and [TryIntoCv] provide `.try_from_cv()` and `.try_into_cv()` methods respectively.
36//! Just like std's [From], [Into], [TryFromCv] and [TryIntoCv].
37//!
38//! ```rust
39//! # use cv_convert::{nalgebra, opencv};
40//! use cv_convert::{FromCv, IntoCv, TryFromCv, TryIntoCv};
41//! use nalgebra as na;
42//! use opencv as cv;
43//!
44//! // FromCv
45//! let cv_point = cv::core::Point2d::new(1.0, 3.0);
46//! let na_points = na::Point2::<f64>::from_cv(&cv_point);
47//!
48//! // IntoCv
49//! let cv_point = cv::core::Point2d::new(1.0, 3.0);
50//! let na_points: na::Point2<f64> = cv_point.into_cv();
51//!
52//! // TryFromCv
53//! let na_mat = na::DMatrix::from_vec(2, 3, vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
54//! let cv_mat = cv::core::Mat::try_from_cv(&na_mat).unwrap();
55//!
56//! // TryIntoCv
57//! let na_mat = na::DMatrix::from_vec(2, 3, vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
58//! let cv_mat: cv::core::Mat = na_mat.try_into_cv().unwrap();
59//! ```
60//!
61//!
62//! # Supported conversions
63//!
64//! The notations are used for simplicity.
65//!
66//! - `S -> T` suggests the conversion is defined by non-fallible [FromCv].
67//! - `S ->? T` suggests the conversion is defined by fallible [TryFromCv].
68//! - `(&)T` means the type can be either owned or borrowed.
69//! - `&'a S -> &'a T` suggests that the target type borrows the source type.
70//!
71//! ## opencv -> opencv
72//!
73//! - [(&)Mat](opencv::core::Mat) ->? [Point_<T>](opencv::core::Point_)
74//! - [(&)Mat](opencv::core::Mat) ->? [Point3_<T>](opencv::core::Point3_)
75//! - [(&)Point_<T>](opencv::core::Point_) ->? [Mat](opencv::core::Mat)
76//! - [(&)Point3_<T>](opencv::core::Point3_) ->? [Mat](opencv::core::Mat)
77//!
78//! ## std -> tch
79//!
80//! - owned/borrowed multi-dimensional [array](array) ->? [Tensor](tch::Tensor)
81//!   - [(&)\[T; N\]](array) ->? [Tensor](tch::Tensor)
82//!   - [(&)\[\[T; N2\]; N1\]](array) ->? [Tensor](tch::Tensor)
83//!   - ... and so on up to 6 dimensions
84//!
85//! ## tch -> std
86//!
87//! - &'a [Tensor](tch::Tensor) -> &'a multi-dimensional [array](array)
88//!   - &'a [Tensor](tch::Tensor) -> &'a [\[T; N\]](array)
89//!   - &'a [Tensor](tch::Tensor) -> &'a [\[\[T; N2\]; N1\]](array)
90//!   - ... and so on up to 6 dimensions
91//! - [(&)Tensor](tch::Tensor) -> owned multi-dimensional [array](array)
92//!   - [(&)Tensor](tch::Tensor) ->? [\[T; N\]](array)
93//!   - [(&)Tensor](tch::Tensor) ->? [\[\[T; N2\]; N1\]](array)
94//!   - ... and so on up to 6 dimensions
95//!
96//! ## tch -> ndarray
97//!
98//! - &[Tensor](tch::Tensor) ->? [Array](ndarray::Array)
99//!
100//! ## ndarray -> tch
101//!
102//! - &[Array](ndarray::Array) ->? [Tensor](tch::Tensor)
103//!
104//! ## ndarray -> opencv
105//!
106//! - &[Array](ndarray::Array) ->? [Mat](opencv::core::Mat)
107//!
108//! ## image -> tch
109//!
110//! - [(&)ImageBuffer](image::ImageBuffer) ->? [TchTensorAsImage]
111//! - [(&)DynamicImage](image::DynamicImage) ->? [TchTensorAsImage]
112//!
113//! ## image -> opencv
114//!
115//! - [(&)ImageBuffer](image::ImageBuffer) ->? [Mat](opencv::core::Mat)
116//! - [(&)DynamicImage](image::DynamicImage) ->? [Mat](opencv::core::Mat)
117//!
118//! ## opencv -> image 0.23
119//!
120//! - [Mat](opencv::core::Mat) ->? [(&)ImageBuffer](image::ImageBuffer)
121//!
122//! ## opencv -> image 0.24
123//!
124//! - [Mat](opencv::core::Mat) ->? [(&)ImageBuffer](image::ImageBuffer)
125//! - [Mat](opencv::core::Mat) ->? [(&)DynamicImage](image::DynamicImage)
126//!
127//! ## opencv -> imageproc
128//! - [(&)Point_<T>](opencv::core::Point_) -> [Point<T>](imageproc::point::Point)
129//!
130//! ## imageproc -> opencv
131//! - [(&)Point<T>](imageproc::point::Point) -> [Point_<T>](opencv::core::Point_)
132//!
133//! ## opencv -> nalgebra
134//!
135//! - [(&)Mat](opencv::core::Mat) ->? [OMatrix](nalgebra::OMatrix)
136//! - [(&)Point_<T>](opencv::core::Point_) -> [Point2<T>](nalgebra::Point2)
137//! - [(&)Point3_<T>](opencv::core::Point3_) -> [Point2<T>](nalgebra::Point3)
138//! - [(&)OpenCvPose<(&)Point3d>](OpenCvPose) ->? [Isometry3<f64>](nalgebra::Isometry3)
139//! - [(&)OpenCvPose<(&)Mat>](OpenCvPose) ->? [Isometry3<f64>](nalgebra::Isometry3)
140//!
141//! ## nalgebra -> opencv
142//!
143//! - [(&)OMatrix](nalgebra::OMatrix) ->? [Mat](opencv::core::Mat)
144//! - [(&)Point2<T>](nalgebra::Point2) -> [Point_<T>](opencv::core::Point_)
145//! - [(&)Point3<T>](nalgebra::Point3) -> [Point3_<T>](opencv::core::Point3_)
146//! - [(&)Translation<N, D>](nalgebra::Translation) ->? [Mat](opencv::core::Mat)
147//! - [(&)Isometry3<T>](nalgebra::Isometry3) ->? [OpenCvPose<Point3_<T>>](OpenCvPose)
148//! - [(&)Isometry3<f64>](nalgebra::Isometry3) ->? [OpenCvPose<Mat>](OpenCvPose)
149//! - [(&)Isometry3<f32>](nalgebra::Isometry3) ->? [OpenCvPose<Mat>](OpenCvPose)
150//!
151//! ## opencv -> tch
152//!
153//! - [(&)Mat](opencv::core::Mat) ->? [Tensor](tch::Tensor)
154//!
155//!   The input [Mat](opencv::core::Mat) is regarded as an n-dimensional array with a m-channel elements.
156//!   The output [Tensor](tch::Tensor) have n+1 dimensions, which last additional m-sized dimension is the channel.
157//!
158//! - [(&)Mat](opencv::core::Mat) ->? [TchTensorAsImage]
159//!
160//!   The input [Mat](opencv::core::Mat) must be a 2D image.
161//!   The output [Tensor](tch::Tensor) within [TchTensorAsImage] has 3 dimensions, which last additional dimension is the channel.
162//!
163//! - [&Mat](opencv::core::Mat) ->? [OpenCvMatAsTchTensor]
164//!
165//! ## tch -> opencv
166//!
167//! - [(&)Tensor](tch::Tensor) ->? [Mat](opencv::core::Mat)
168//!
169//!    The n-dimensinoal input [Tensor](tch::Tensor) is converted to a [Mat](opencv::core::Mat)
170//!    with n dimensions and a channel of size 1.
171//!
172//! - [(&)TchTensorAsImage](TchTensorAsImage) ->? [Mat](opencv::core::Mat)
173//!
174//!    The output [Mat](opencv::core::Mat) is a 2D image, which height, width and channel size
175//!    are judged from the input [TchTensorAsImage](TchTensorAsImage) shape.
176//!
177//!
178//! ## opencv -> ndarray
179//!
180//! - [&Mat](opencv::core::Mat) ->? [ArrayView](ndarray::ArrayView)
181//! - [(&)Mat](opencv::core::Mat) ->? [Array](ndarray::Array)
182//!
183//!
184//! # Notes for OpenCV
185//!
186//! For opencv older than 0.66, some systems requires `clang-runtime`
187//! feature to build successfully. Otherwise you will get `libclang
188//! shared library is not loaded on this thread!` panic. Add `opencv`
189//! dependency along side `cv-convert` in your project Cargo.toml to
190//! enable this feature.
191//!
192//! ```toml
193//! cv-convert = { version = "0.22.0", default-features = false, features = ["opencv_0-65"] }
194//! opencv = { version = "0.65", features = ["clang-runtime"] }
195//! ```
196//!
197//! Most opencv modules, such as `videoio` and `aruco`, are disabled
198//! by default to avoid bloating. Add opencv dependency to your
199//! project Cargo.toml to enable default modules in your project.
200
201mod common;
202
203mod traits;
204pub use traits::*;
205
206pub mod prelude {
207    pub use crate::traits::{FromCv, IntoCv, TryFromCv, TryIntoCv};
208}
209
210mod macros;
211use macros::*;
212
213// modules
214has_opencv! {
215    mod with_opencv;
216    pub use with_opencv::*;
217}
218
219has_tch! {
220    mod with_tch;
221    pub use with_tch::*;
222}
223
224has_tch! {
225    has_image! {
226        mod with_tch_image;
227        pub use with_tch_image::*;
228    }
229}
230
231has_tch! {
232    has_ndarray! {
233        mod with_tch_ndarray;
234        pub use with_tch_ndarray::*;
235    }
236}
237
238has_image! {
239    has_opencv! {
240        #[cfg(feature = "image_0-23")]
241        mod with_opencv_image_0_23;
242        #[cfg(feature = "image_0-23")]
243        pub use with_opencv_image_0_23::*;
244
245        #[cfg(feature = "image_0-24")]
246        mod with_opencv_image_0_24;
247        #[cfg(feature = "image_0-24")]
248        pub use with_opencv_image_0_24::*;
249
250    }
251}
252
253has_imageproc! {
254    has_opencv! {
255        mod with_opencv_imageproc;
256        pub use with_opencv_imageproc::*;
257    }
258}
259
260has_nalgebra! {
261    has_opencv! {
262        mod with_opencv_nalgebra;
263        pub use with_opencv_nalgebra::*;
264    }
265}
266
267has_tch! {
268    has_opencv! {
269        mod with_opencv_tch;
270        pub use with_opencv_tch::*;
271    }
272}
273
274has_opencv! {
275    has_ndarray! {
276        mod with_opencv_ndarray;
277        pub use with_opencv_ndarray::*;
278    }
279}
280
281/* Include generated code */
282include!("../generated/lib.rs.snipplet");