1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use crate::imageproc;
use crate::opencv::core as core_cv;
use crate::FromCv;

impl<T> FromCv<&imageproc::point::Point<T>> for core_cv::Point_<T>
where
    T: num_traits::Num + Copy,
{
    fn from_cv(from: &imageproc::point::Point<T>) -> Self {
        core_cv::Point_::new(from.x, from.y)
    }
}

impl<T> FromCv<imageproc::point::Point<T>> for core_cv::Point_<T>
where
    T: num_traits::Num + Copy,
{
    fn from_cv(from: imageproc::point::Point<T>) -> Self {
        FromCv::from_cv(&from)
    }
}

impl<T> FromCv<&core_cv::Point_<T>> for imageproc::point::Point<T>
where
    T: num_traits::Num + Copy,
{
    fn from_cv(from: &core_cv::Point_<T>) -> Self {
        Self::new(from.x, from.y)
    }
}

impl<T> FromCv<core_cv::Point_<T>> for imageproc::point::Point<T>
where
    T: num_traits::Num + Copy,
{
    fn from_cv(from: core_cv::Point_<T>) -> Self {
        FromCv::from_cv(&from)
    }
}

#[cfg(test)]
mod tests {
    use anyhow::Result;
    use approx::abs_diff_eq;
    use crate::imageproc;
    use crate::opencv::core as core_cv;
    use crate::{common::ensure, FromCv, IntoCv};
    use rand::prelude::*;
    use std::f64;

    #[test]
    fn convert_opencv_imageproc() -> Result<()> {
        let mut rng = rand::thread_rng();

        for _ in 0..5000 {
            // FromCv
            // opencv to imageproc
            {
                let cv_point = core_cv::Point2d::new(rng.gen(), rng.gen());
                let imageproc_point = imageproc::point::Point::<f64>::from_cv(&cv_point);
                ensure!(
                    abs_diff_eq!(cv_point.x, imageproc_point.x) && abs_diff_eq!(cv_point.y, imageproc_point.y),
                    "point conversion failed"
                );
            }

            // imageproc to opencv
            {
                let imageproc_point = imageproc::point::Point::<f64>::new(rng.gen(), rng.gen());
                let cv_point = core_cv::Point2d::from_cv(&imageproc_point);
                ensure!(
                    abs_diff_eq!(imageproc_point.x, cv_point.x) && abs_diff_eq!(imageproc_point.y, cv_point.y),
                    "point conversion failed"
                );
            }

            // IntoCv
            // opencv to imageproc
            {
                let cv_point = core_cv::Point2d::new(rng.gen(), rng.gen());
                let imageproc_point: imageproc::point::Point<f64> = cv_point.into_cv();
                ensure!(
                    abs_diff_eq!(cv_point.x, imageproc_point.x) && abs_diff_eq!(cv_point.y, imageproc_point.y),
                    "point conversion failed"
                );
            }

            // imageproc to opencv
           {
                let imageproc_point = imageproc::point::Point::<f64>::new(rng.gen(), rng.gen());
                let cv_point: core_cv::Point2d = imageproc_point.into_cv();
                ensure!(
                    abs_diff_eq!(imageproc_point.x, cv_point.x) && abs_diff_eq!(imageproc_point.y, cv_point.y),
                    "point conversion failed"
                );
            }
        }
        Ok(())
    }
}