Add From implementations from tuples and similar C++ conversions for Point, Size, Rect and Point3
MODIFY
examples/discrete_fourier_transform.rs
examples/discrete_fourier_transform.rs
@@ -19,7 +19,7 @@ fn main() -> opencv::Result<()> {
let mut padded = Mat::default();
let m = core::get_optimal_dft_size(I.rows())?;
let n = core::get_optimal_dft_size(I.cols())?;
- core::copy_make_border(&I, &mut padded, 0, m - I.rows(), 0, n - I.cols(), core::BORDER_CONSTANT, Scalar::all(0.))?;
+ core::copy_make_border(&I, &mut padded, 0, m - I.rows(), 0, n - I.cols(), core::BORDER_CONSTANT, 0.into())?;
let plane_size = padded.size()?;
let mut planes = core::Vector::<Mat>::new();
let mut padded_f32 = Mat::default();

MODIFY
src/manual/core/point.rs
src/manual/core/point.rs
@@ -78,7 +78,26 @@ impl<T> Point_<T> {
}
}
-opencv_type_simple_generic! { Point_<Copy> }
+impl<T> From<(T, T)> for Point_<T> {
+ #[inline]
+ fn from(s: (T, T)) -> Self {
+ Self::new(s.0, s.1)
+ }
+}
+
+impl<T> From<VecN<T, 2>> for Point_<T> {
+ #[inline]
+ fn from(s: VecN<T, 2>) -> Self {
+ Self::from_vec2(s)
+ }
+}
+
+impl<T> From<Size_<T>> for Point_<T> {
+ #[inline]
+ fn from(s: Size_<T>) -> Self {
+ Self::from_size(s)
+ }
+}
impl<T> Add for Point_<T> where Self: AddAssign, {
type Output = Self;
@@ -143,3 +162,5 @@ impl<T: DivAssign + Copy> DivAssign<T> for Point_<T> {
self.y /= rhs;
}
}
+
+opencv_type_simple_generic! { Point_<Copy> }

MODIFY
src/manual/core/point3.rs
src/manual/core/point3.rs
@@ -76,7 +76,27 @@ impl<T> Point3_<T> {
VecN::<_, 3>::from([self.x, self.y, self.z])
}
}
-opencv_type_simple_generic! { Point3_<Copy> }
+
+impl<T> From<(T, T, T)> for Point3_<T> {
+ #[inline]
+ fn from(s: (T, T, T)) -> Self {
+ Self::new(s.0, s.1, s.2)
+ }
+}
+
+impl<T> From<VecN<T, 3>> for Point3_<T> {
+ #[inline]
+ fn from(s: VecN<T, 3>) -> Self {
+ Self::from_vec3(s)
+ }
+}
+
+impl<T: Zero> From<Point_<T>> for Point3_<T> {
+ #[inline]
+ fn from(s: Point_<T>) -> Self {
+ Self::from_point(s)
+ }
+}
impl<T> Add for Point3_<T> where Self: AddAssign {
type Output = Self;
@@ -145,3 +165,5 @@ impl<T: DivAssign + Copy> DivAssign<T> for Point3_<T> {
self.z /= rhs;
}
}
+
+opencv_type_simple_generic! { Point3_<Copy> }

MODIFY
src/manual/core/rect.rs
src/manual/core/rect.rs
@@ -84,7 +84,26 @@ impl<T> Rect_<T> {
}
}
-opencv_type_simple_generic! { Rect_<Copy> }
+impl<T> From<(T, T, T, T)> for Rect_<T> {
+ #[inline]
+ fn from(s: (T, T, T, T)) -> Self {
+ Self::new(s.0, s.1, s.2, s.3)
+ }
+}
+
+impl<T> From<(Point_<T>, Size_<T>)> for Rect_<T> {
+ #[inline]
+ fn from(s: (Point_<T>, Size_<T>)) -> Self {
+ Self::from_point_size(s.0, s.1)
+ }
+}
+
+impl<T: PartialOrd + Sub<Output=T> + Copy> From<(Point_<T>, Point_<T>)> for Rect_<T> {
+ #[inline]
+ fn from(s: (Point_<T>, Point_<T>)) -> Self {
+ Self::from_points(s.0, s.1)
+ }
+}
impl<P, R> Add<Point_<P>> for Rect_<R> where Self: AddAssign<Point_<P>> {
type Output = Self;
@@ -224,3 +243,5 @@ fn test_partial() {
assert_eq!(2, partial_max(2, 1));
assert_eq!(2, partial_max(2, 2));
}
+
+opencv_type_simple_generic! { Rect_<Copy> }

MODIFY
src/manual/core/scalar.rs
src/manual/core/scalar.rs
@@ -11,6 +11,13 @@ impl<T> Scalar_<T> {
}
}
+impl From<i32> for Scalar_<f64> {
+ #[inline]
+ fn from(v0: i32) -> Self {
+ Self::from(f64::from(v0))
+ }
+}
+
impl<T: Zero> From<T> for Scalar_<T> {
#[inline]
fn from(v0: T) -> Self {

MODIFY
src/manual/core/size.rs
src/manual/core/size.rs
@@ -42,7 +42,19 @@ impl<T> Size_<T> {
}
}
-opencv_type_simple_generic! { Size_<Copy> }
+impl<T> From<(T, T)> for Size_<T> {
+ #[inline]
+ fn from(s: (T, T)) -> Self {
+ Self::new(s.0, s.1)
+ }
+}
+
+impl<T> From<Point_<T>> for Size_<T> {
+ #[inline]
+ fn from(s: Point_<T>) -> Self {
+ Self::from_point(s)
+ }
+}
impl<T> Add for Size_<T> where Self: AddAssign {
type Output = Self;
@@ -107,3 +119,5 @@ impl<T: DivAssign + Copy> DivAssign<T> for Size_<T> {
self.height /= rhs;
}
}
+
+opencv_type_simple_generic! { Size_<Copy> }

MODIFY
tests/point.rs
tests/point.rs
-use opencv::core::{Point, Point2d, Point2f, Point2i, Point2l, Size2l, Vec2d};
+use opencv::core::{Point, Point2d, Point2f, Point2i, Point2l, Size2d, Size2l, Vec2d, Vec2f};
#[test]
fn point_add() {
@@ -103,3 +103,10 @@ fn point_conv() {
let vec = Vec2d::from([10., 20.]);
assert_eq!(vec, Point2d::from_vec2(vec).to_vec2());
}
+
+#[test]
+fn point_from() {
+ assert_eq!(Point2l::new(1, 2), (1, 2).into());
+ assert_eq!(Point2f::new(1., 2.), Vec2f::from([1., 2.]).into());
+ assert_eq!(Point2d::new(1., 2.), Size2d::new(1., 2.).into());
+}

MODIFY
tests/point3.rs
tests/point3.rs
-use opencv::core::{Point2d, Point3d, Point3f, Point3i, Vec3d};
+use opencv::core::{Point2d, Point2f, Point3d, Point3f, Point3i, Vec3d, Vec3i};
#[test]
fn point3_add() {
@@ -104,3 +104,10 @@ fn point3_conv() {
let vec = Vec3d::from([10., 20., 30.]);
assert_eq!(vec, Point3d::from_vec3(vec).to_vec3());
}
+
+#[test]
+fn point3_from() {
+ assert_eq!(Point3d::new(1., 2., 3.), (1., 2., 3.).into());
+ assert_eq!(Point3i::new(1, 2, 3), Vec3i::from([1, 2, 3]).into());
+ assert_eq!(Point3f::new(1., 2., 0.), Point2f::new(1., 2.).into());
+}

MODIFY
tests/rect.rs
tests/rect.rs
-use opencv::core::{self, Point2d, Point2i, Rect, Rect2d, Rect2f, Rect2i, Size2d, Size2i};
+use opencv::core::{self, Point2d, Point2f, Point2i, Rect, Rect2d, Rect2f, Rect2i, Size2d, Size2f, Size2i};
#[test]
fn rect_add() {
@@ -144,3 +144,10 @@ fn rect_conv() {
let recti = Rect2i::new(1, 2, 3, 4);
assert_eq!(Rect2f::new(1., 2., 3., 4.), recti.to::<f32>().unwrap());
}
+
+#[test]
+fn rect_from() {
+ assert_eq!(Rect2i::new(1, 2, 3, 4), (1, 2, 3, 4).into());
+ assert_eq!(Rect2f::new(1., 2., 3., 4.), (Point2f::new(1., 2.), Size2f::new(3., 4.)).into());
+ assert_eq!(Rect2d::new(1., 2., 3., 4.), (Point2d::new(1., 2.), Point2d::new(4., 6.)).into());
+}

MODIFY
tests/size.rs
tests/size.rs
-use opencv::core::{Point2d, Size, Size2d, Size2f, Size2i};
+use opencv::core::{Point, Point2d, Size, Size2d, Size2f, Size2i};
#[test]
fn size_add() {
@@ -91,3 +91,9 @@ fn size_conv() {
assert_eq!(Size2d::new(10., 20.), Size2d::from_point(Point2d::new(10., 20.)));
}
+
+#[test]
+fn size_from() {
+ assert_eq!(Size2f::new(1., 2.), (1., 2.).into());
+ assert_eq!(Size::new(1, 2), Point::new(1, 2).into());
+}

Add From implementations from tuples and similar C++ conversions for Point, Size, Rect and Point3