cv_convert_fork/
with_opencv_imageproc.rs

1use crate::imageproc;
2use crate::opencv::core as core_cv;
3use crate::FromCv;
4
5impl<T> FromCv<&imageproc::point::Point<T>> for core_cv::Point_<T>
6where
7    T: num_traits::Num + Copy,
8{
9    fn from_cv(from: &imageproc::point::Point<T>) -> Self {
10        core_cv::Point_::new(from.x, from.y)
11    }
12}
13
14impl<T> FromCv<imageproc::point::Point<T>> for core_cv::Point_<T>
15where
16    T: num_traits::Num + Copy,
17{
18    fn from_cv(from: imageproc::point::Point<T>) -> Self {
19        FromCv::from_cv(&from)
20    }
21}
22
23impl<T> FromCv<&core_cv::Point_<T>> for imageproc::point::Point<T>
24where
25    T: num_traits::Num + Copy,
26{
27    fn from_cv(from: &core_cv::Point_<T>) -> Self {
28        Self::new(from.x, from.y)
29    }
30}
31
32impl<T> FromCv<core_cv::Point_<T>> for imageproc::point::Point<T>
33where
34    T: num_traits::Num + Copy,
35{
36    fn from_cv(from: core_cv::Point_<T>) -> Self {
37        FromCv::from_cv(&from)
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use anyhow::Result;
44    use approx::abs_diff_eq;
45    use crate::imageproc;
46    use crate::opencv::core as core_cv;
47    use crate::{common::ensure, FromCv, IntoCv};
48    use rand::prelude::*;
49    use std::f64;
50
51    #[test]
52    fn convert_opencv_imageproc() -> Result<()> {
53        let mut rng = rand::thread_rng();
54
55        for _ in 0..5000 {
56            // FromCv
57            // opencv to imageproc
58            {
59                let cv_point = core_cv::Point2d::new(rng.gen(), rng.gen());
60                let imageproc_point = imageproc::point::Point::<f64>::from_cv(&cv_point);
61                ensure!(
62                    abs_diff_eq!(cv_point.x, imageproc_point.x) && abs_diff_eq!(cv_point.y, imageproc_point.y),
63                    "point conversion failed"
64                );
65            }
66
67            // imageproc to opencv
68            {
69                let imageproc_point = imageproc::point::Point::<f64>::new(rng.gen(), rng.gen());
70                let cv_point = core_cv::Point2d::from_cv(&imageproc_point);
71                ensure!(
72                    abs_diff_eq!(imageproc_point.x, cv_point.x) && abs_diff_eq!(imageproc_point.y, cv_point.y),
73                    "point conversion failed"
74                );
75            }
76
77            // IntoCv
78            // opencv to imageproc
79            {
80                let cv_point = core_cv::Point2d::new(rng.gen(), rng.gen());
81                let imageproc_point: imageproc::point::Point<f64> = cv_point.into_cv();
82                ensure!(
83                    abs_diff_eq!(cv_point.x, imageproc_point.x) && abs_diff_eq!(cv_point.y, imageproc_point.y),
84                    "point conversion failed"
85                );
86            }
87
88            // imageproc to opencv
89           {
90                let imageproc_point = imageproc::point::Point::<f64>::new(rng.gen(), rng.gen());
91                let cv_point: core_cv::Point2d = imageproc_point.into_cv();
92                ensure!(
93                    abs_diff_eq!(imageproc_point.x, cv_point.x) && abs_diff_eq!(imageproc_point.y, cv_point.y),
94                    "point conversion failed"
95                );
96            }
97        }
98        Ok(())
99    }
100}