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 101 102 103 104 105 106 107
use crate::{common::*, TryFromCv}; use opencv::{core, prelude::*}; impl<T> TryFromCv<&core::Mat> for core::Point_<T> where T: core::DataType + core::ValidPointType, { type Error = Error; fn try_from_cv(from: &core::Mat) -> Result<Self> { let slice = from.data_typed::<T>()?; ensure!(slice.len() == 2, "invalid length"); let point = Self { x: slice[0], y: slice[1], }; Ok(point) } } impl<T> TryFromCv<core::Mat> for core::Point_<T> where T: core::DataType + core::ValidPointType, { type Error = Error; fn try_from_cv(from: core::Mat) -> Result<Self> { TryFromCv::try_from_cv(&from) } } impl<T> TryFromCv<&core::Mat> for core::Point3_<T> where T: core::DataType + core::ValidPoint3Type, { type Error = Error; fn try_from_cv(from: &core::Mat) -> Result<Self> { let slice = from.data_typed::<T>()?; ensure!(slice.len() == 3, "invalid length"); let point = Self { x: slice[0], y: slice[1], z: slice[2], }; Ok(point) } } impl<T> TryFromCv<core::Mat> for core::Point3_<T> where T: core::DataType + core::ValidPoint3Type, { type Error = Error; fn try_from_cv(from: core::Mat) -> Result<Self> { TryFromCv::try_from_cv(&from) } } impl<T> TryFromCv<&core::Point_<T>> for core::Mat where T: core::DataType + core::ValidPointType, { type Error = Error; fn try_from_cv(from: &core::Point_<T>) -> Result<Self> { let core::Point_ { x, y, .. } = *from; let mat = core::Mat::from_slice(&[x, y])?; Ok(mat) } } impl<T> TryFromCv<core::Point_<T>> for core::Mat where T: core::DataType + core::ValidPointType, { type Error = Error; fn try_from_cv(from: core::Point_<T>) -> Result<Self> { TryFromCv::try_from_cv(&from) } } impl<T> TryFromCv<&core::Point3_<T>> for core::Mat where T: core::DataType + core::ValidPoint3Type, { type Error = Error; fn try_from_cv(from: &core::Point3_<T>) -> Result<Self> { let core::Point3_ { x, y, z, .. } = *from; let mat = core::Mat::from_slice(&[x, y, z])?; Ok(mat) } } impl<T> TryFromCv<core::Point3_<T>> for core::Mat where T: core::DataType + core::ValidPoint3Type, { type Error = Error; fn try_from_cv(from: core::Point3_<T>) -> Result<Self> { TryFromCv::try_from_cv(&from) } }