improve UMat support
MODIFY
gen_rust.py
gen_rust.py
@@ -86,6 +86,7 @@ decls_manual_pre = {
"core": [
("class cv.Range", "", ["/Ghost"], []),
("class cv.Mat", "", ["/Ghost"], []),
+ ("class cv.UMat", "", ["/Ghost"], []),
("class cv.Algorithm", "", ["/Ghost"], []),
("class cv.DMatch", "", ["/Ghost", "/Simple"], []),
("class cv.KeyPoint", "", ["/Ghost", "/Simple"], []),
@@ -139,38 +140,38 @@ func_rename = {
### core ###
"cv_addImpl_int_const_char_X": "-",
"cv_MatExpr_type_const": "typ",
- "cv_Mat_Mat_int_int_int": "new_rows_cols",
- "cv_Mat_Mat_Size_int": "new_size",
- "cv_Mat_Mat_int_int_int_Scalar": "new_rows_cols_with_default",
- "cv_Mat_Mat_Size_int_Scalar": "new_size_with_default",
+ "cv_Mat_Mat_int_int_int": "+_rows_cols",
+ "cv_Mat_Mat_Size_int": "+_size",
+ "cv_Mat_Mat_int_int_int_Scalar": "+_rows_cols_with_default",
+ "cv_Mat_Mat_Size_int_Scalar": "+_size_with_default",
"cv_Mat_Mat_int_const_int_X_int": "-", # duplicate of cv_Mat_Mat_VectorOfint_int, but with pointers
- "cv_Mat_Mat_VectorOfint_int": "new_nd",
- "cv_Mat_Mat_int_const_int_X_int_Scalar": "-", # duplicate of cv_Mat_Mat_VectorOfint_int_Scalar_s, but with pointers
- "cv_Mat_Mat_VectorOfint_int_Scalar": "new_nd_with_default",
+ "cv_Mat_Mat_VectorOfint_int": "+_nd",
+ "cv_Mat_Mat_int_const_int_X_int_Scalar": "-", # duplicate of cv_Mat_Mat_VectorOfint_int_Scalar, but with pointers
+ "cv_Mat_Mat_VectorOfint_int_Scalar": "+_nd_with_default",
"cv_Mat_Mat_Mat": "copy",
"cv_Mat_Mat_int_int_int_void_X_size_t": "new_rows_cols_with_data",
"cv_Mat_Mat_Size_int_void_X_size_t": "new_size_with_data",
"cv_Mat_Mat_int_const_int_X_int_void_X_const_size_t_X": "-", # duplicate of cv_Mat_Mat_VectorOfint_int_void_X_const_size_t_X, but with pointers
- "cv_Mat_Mat_VectorOfint_int_void_X_const_size_t_X": "new_nd_with_data",
+ "cv_Mat_Mat_VectorOfint_int_void_X_const_size_t_X": "+_nd_with_data",
"cv_Mat_Mat_Mat_Range_Range": "rowscols",
"cv_Mat_Mat_Mat_Rect": "roi",
- "cv_Mat_Mat_Mat_const_Range": "-", # duplicate of cv_Mat_Mat_Mat_VectorOfRange_ranges, but with pointers
+ "cv_Mat_Mat_Mat_const_Range": "-", # duplicate of cv_Mat_Mat_Mat_VectorOfRange, but with pointers
"cv_Mat_Mat_Mat_VectorOfRange": "ranges",
- "cv_Mat_colRange_const_Range": "colrange",
- "cv_Mat_colRange_const_int_int": "colbounds",
- "cv_Mat_copyTo_const_Mat_Mat": "copy_to_masked",
+ "cv_Mat_colRange_const_int_int": "col_bounds",
+ "cv_Mat_rowRange_const_int_int": "row_bounds",
+ "cv_Mat_copyTo_const_Mat_Mat": "+_masked",
"cv_Mat_create_int_int_int": "+_rows_cols",
"cv_Mat_create_Size_int": "+_size",
"cv_Mat_create_VectorOfint_int": "+_nd",
"cv_Mat_create_int_const_int_X_int": "-", # duplicate of cv_Mat_create_VectorOfint_int, but with pointers
- "cv_Mat_diag_Mat": "diag_new_mat",
- "cv_Mat_ptr_int": "ptr_mut",
- "cv_Mat_ptr_int_int": "ptr_2d_mut",
- "cv_Mat_ptr_const_int_int": "ptr_2d",
- "cv_Mat_ptr_int_int_int": "ptr_3d_mut",
- "cv_Mat_ptr_const_int_int_int": "ptr_3d",
- "cv_Mat_ptr_const_int_X": "ptr_nd_mut",
- "cv_Mat_ptr_const_const_int_X": "ptr_nd",
+ "cv_Mat_diag_Mat": "+_new_mat",
+ "cv_Mat_ptr_int": "+_mut",
+ "cv_Mat_ptr_int_int": "+_2d_mut",
+ "cv_Mat_ptr_const_int_int": "+_2d",
+ "cv_Mat_ptr_int_int_int": "+_3d_mut",
+ "cv_Mat_ptr_const_int_int_int": "+_3d",
+ "cv_Mat_ptr_const_int_X": "+_nd_mut",
+ "cv_Mat_ptr_const_const_int_X": "+_nd",
"cv_Mat_at_int": "at_mut",
"cv_Mat_at_int_int": "at_2d_mut",
"cv_Mat_at_const_int_int": "at_2d",
@@ -179,7 +180,6 @@ func_rename = {
"cv_Mat_at_const_int_X": "at_nd_mut",
"cv_Mat_at_const_const_int_X": "at_nd",
"cv_Mat_resize_size_t_Scalar": "resize_with_default",
- "cv_Mat_rowRange_const_int_int": "rowbounds",
"cv_Mat_type_const": "typ",
"cv_Mat_reshape_const_int_int_const_int_X": "-", # duplicate of cv_Mat_reshape_const_int_VectorOfint, but with pointers
"cv_Mat_reshape_const_int_VectorOfint": "reshape_nd",
@@ -189,6 +189,37 @@ func_rename = {
"cv_Mat_set_size_MatSize": "-", # doesn't allow writing
"cv_Mat_set_step_MatStep": "-", # same as above
"cv_Mat_data": "+_mut",
+ "cv_Mat_copySize_Mat": "-", # internal function
+ "cv_Mat_getUMat_const_AccessFlag_UMatUsageFlags": "get_umat",
+ "cv_Mat_getUMat_const_int_UMatUsageFlags": "get_umat", # 3.2 only
+ "cv_UMat_UMat_int_int_int_UMatUsageFlags": "+_rows_cols",
+ "cv_UMat_UMat_Size_int_UMatUsageFlags": "+_size",
+ "cv_UMat_UMat_int_int_int_Scalar_UMatUsageFlags": "+_rows_cols_with_default",
+ "cv_UMat_UMat_Size_int_Scalar_UMatUsageFlags": "+_size_with_default",
+ "cv_min_UMat_UMat_UMat": "+_umat",
+ "cv_max_UMat_UMat_UMat": "+_umat",
+ "cv_swap_UMat_UMat": "+_umat",
+ "cv_UMat_UMat_int_const_int_X_int_UMatUsageFlags": "+_nd",
+ "cv_UMat_UMat_int_const_int_X_int_Scalar_UMatUsageFlags": "+_nd_with_default", # fixme
+ "cv_UMat_UMat_UMat": "copy",
+ "cv_UMat_UMat_UMat_Range_Range": "rowscols",
+ "cv_UMat_UMat_UMat_Rect": "roi",
+ "cv_UMat_UMat_UMat_const_Range": "-", # duplicate of cv_UMat_UMat_UMat_VectorOfRange, but with pointers
+ "cv_UMat_UMat_UMat_VectorOfRange": "ranges",
+ "cv_UMat_colRange_const_int_int": "col_bounds",
+ "cv_UMat_rowRange_const_int_int": "row_bounds",
+ "cv_UMat_size_const": "mat_size",
+ "cv_UMat_step_const": "mat_step",
+ "cv_UMat_set_size_MatSize": "-", # doesn't allow writing
+ "cv_UMat_set_step_MatStep": "-", # same as above
+ "cv_UMat_create_int_int_int_UMatUsageFlags": "+_rows_cols",
+ "cv_UMat_create_Size_int_UMatUsageFlags": "+_size",
+ "cv_UMat_create_VectorOfint_int_UMatUsageFlags": "+_nd",
+ "cv_UMat_create_int_const_int_X_int_UMatUsageFlags": "-", # duplicate of cv_UMat_create_VectorOfint_int_UMatUsageFlags, but with pointers
+ "cv_UMat_type_const": "typ",
+ "cv_UMat_copyTo_const_Mat": "copy_to",
+ "cv_UMat_copyTo_const_Mat_Mat": "copy_to_masked",
+ "cv_UMat_copySize_UMat": "-", # internal function
"cv_Mat_push_back__const_void_X": "-", # internal method
"cv_merge_const_Mat_size_t_Mat": "-", # duplicate of cv_merge_VectorOfMat_Mat, but with pointers
"cv_Matx_TOp_Matx_TOp_Matx_TOp": "copy",
@@ -211,9 +242,6 @@ func_rename = {
"cv_RotatedRect_RotatedRect": "default",
"cv_RotatedRect_RotatedRect_Point2f_Point2f_Point2f": "for_points",
"cv_TermCriteria_TermCriteria": "default",
- "cv_UMat_type_const": "typ",
- "cv_UMat_copyTo_const_Mat": "copy_to",
- "cv_UMat_copyTo_const_Mat_Mat": "copy_to_masked",
"cv_calcCovarMatrix_const_Mat_int_Mat_Mat_int_int": "-", # duplicate of cv_calcCovarMatrix_Mat_Mat_Mat_int_int, but with pointers
"cv_clipLine_Size_Point_Point": "clip_line_size",
"cv_clipLine_Size2l_Point2l_Point2l": "clip_line_size_i64",
@@ -369,6 +397,7 @@ func_rename = {
"cv_dnn_slice_Mat_Range_Range": "slice_2d",
"cv_dnn_slice_Mat_Range_Range_Range": "slice_3d",
"cv_dnn_slice_Mat_Range_Range_Range_Range": "slice_4d",
+ "cv_dnn_shape_UMat": "+_umat",
"cv_dnn_shape_const_int_X_int": "shape_nd",
"cv_dnn_shape_int_int_int_int": "shape_3d",
"cv_dnn_blobFromImage_Mat_Mat_double_Size_Scalar_bool_bool_int": "blob_from_image_to",
@@ -462,6 +491,12 @@ func_unsafe_list = {
"cv_Mat_create_int_int_int",
"cv_Mat_create_Size_int",
"cv_Mat_create_VectorOfint_int",
+ "cv_UMat_UMat_int_int_int_UMatUsageFlags",
+ "cv_UMat_UMat_Size_int_UMatUsageFlags",
+ "cv_UMat_UMat_int_const_int_X_int_UMatUsageFlags",
+ "cv_UMat_create_int_int_int_UMatUsageFlags",
+ "cv_UMat_create_Size_int_UMatUsageFlags",
+ "cv_UMat_create_VectorOfint_int_UMatUsageFlags",
# allows passing arbitrary data
"cv_Mat_set_data_uchar_X",
# no bounds checking
@@ -678,6 +713,17 @@ force_slice = {
"arg_count": 4,
"arg_name": "steps",
},
+ # accepts pointer to array of dimensions, make it slice
+ "cv.UMat.UMat": (
+ {
+ "arg_count": 4,
+ "arg_name": "sizes",
+ },
+ {
+ "arg_count": 5,
+ "arg_name": "sizes",
+ }
+ )
},
}
@@ -762,7 +808,7 @@ def decl_patch(module, decl):
decl[3][i][0] = decl[3][i][0].replace("*", "[]")
if module == "core":
# size() and step() of Mat and UMat should be const
- if decl[0] == "cv.Mat.size" or decl[0] == "cv.Mat.step":
+ if decl[0] == "cv.Mat.size" or decl[0] == "cv.Mat.step" or decl[0] == "cv.UMat.size" or decl[0] == "cv.UMat.step":
decl[2].append("/C")
elif module == "dnn":
# set method takes generic, force it to take DictValue wrapper
@@ -2954,8 +3000,7 @@ class RustWrapperGenerator(object):
None,
prop.comment
])
- if not read_func.is_ignored and not read_func.rv_type().is_ignored:
- if not is_const:
+ if not read_func.is_ignored and not read_func.rv_type().is_ignored and not is_const:
attrs = ["/ATTRSETTER"]
self.add_decl(module, [
"{}.set_{}".format(item.fullname.replace("::", "."), prop.name),

MODIFY
hdr_parser.py
hdr_parser.py
@@ -800,7 +800,7 @@ class CppHeaderParser(object):
if (context == "struct" or context == "class") and end_token == ";" and stmt:
# looks like it's member declaration; append the members to the class declaration
class_decl = stack_top[self.CLASS_DECL]
- if ("CV_PROP" in stmt) or class_decl[0] in ("class cv.Mat", "class cv.RotatedRect", "class cv.TermCriteria", "class cv.Range", "class cv.dnn.LayerParams"): # or (class_decl and ("/Map" in class_decl[2])):
+ if ("CV_PROP" in stmt) or class_decl[0] in ("class cv.Mat", "class cv.UMat", "class cv.RotatedRect", "class cv.TermCriteria", "class cv.Range", "class cv.dnn.LayerParams"): # or (class_decl and ("/Map" in class_decl[2])):
var_modlist = []
if "CV_PROP_RW" in stmt:
var_modlist.append("/RW")

MODIFY
src/manual/core/mat.rs
src/manual/core/mat.rs
@@ -3,7 +3,7 @@ use std::{fmt, ops::Deref, slice};
use libc::size_t;
use crate::{
- core::{self, Mat, MatSize, MatStep, Scalar},
+ core::{self, Mat, MatSize, MatStep, Scalar, UMat},
Error,
Result,
sys,
@@ -441,6 +441,19 @@ impl Default for Mat {
}
}
+impl UMat {
+ #[inline]
+ pub fn size(&self) -> Result<core::Size> {
+ let me = self.as_raw_UMat();
+ cpp!(unsafe [me as "const cv::UMat*"] -> sys::cv_return_value_SizeWrapper as "cv_return_value_SizeWrapper" {
+ try {
+ cv::Size ret = me->size();
+ return { Error::Code::StsOk, NULL, *reinterpret_cast<SizeWrapper*>(&ret) };
+ } CVRS_CATCH(cv_return_value_SizeWrapper)
+ }).into_result()
+ }
+}
+
#[cfg(feature = "opencv-32")]
impl MatSize {
#[inline]

ADD
tests/umat.rs
+use opencv::{
+ core::{Rect, Size, UMat, UMatUsageFlags, Vec3d},
+ prelude::*,
+ Result,
+ types::VectorOfint,
+};
+
+#[cfg(not(feature = "opencv-41"))]
+use opencv::core::ACCESS_READ;
+#[cfg(feature = "opencv-41")]
+use opencv::core::AccessFlag::ACCESS_READ;
+
+#[test]
+fn umat_default() -> Result<()> {
+ let mat = UMat::new(UMatUsageFlags::USAGE_DEFAULT)?;
+ assert_eq!(u8::typ(), mat.typ()?);
+ assert_eq!(u8::depth(), mat.depth()?);
+ assert_eq!(u8::channels(), mat.channels()?);
+ assert_eq!(Size::new(0, 0), mat.size()?);
+ assert_eq!(0, mat.dims()?);
+ Ok(())
+}
+
+#[test]
+fn umat_create() -> Result<()> {
+ let mut mat = UMat::new(UMatUsageFlags::USAGE_DEFAULT)?;
+ unsafe { mat.create_rows_cols(10, 10, u16::typ(), UMatUsageFlags::USAGE_DEFAULT)? };
+ assert_eq!(Size::new(10, 10), mat.size()?);
+ assert_eq!(2, mat.dims()?);
+ Ok(())
+}
+
+#[test]
+fn umat_to_mat() -> Result<()> {
+ {
+ let mut vec = VectorOfint::new();
+ vec.push(1);
+ vec.push(2);
+ vec.push(3);
+ let umat = Mat::from_exact_iter(vec.into_iter())?.get_umat(ACCESS_READ, UMatUsageFlags::USAGE_DEFAULT)?;
+ assert_eq!(3, umat.rows()?);
+ assert_eq!(1, umat.cols()?);
+ assert_eq!(i32::typ(), umat.typ()?);
+ let mat = umat.get_mat(ACCESS_READ)?;
+ assert_eq!(1, *mat.at_2d::<i32>(0, 0)?);
+ assert_eq!(2, *mat.at_2d::<i32>(1, 0)?);
+ assert_eq!(3, *mat.at_2d::<i32>(2, 0)?);
+ }
+
+ {
+ let vec: Vec<i32> = vec![1, 2, 3];
+ let umat = Mat::from_exact_iter(vec.into_iter())?.get_umat(ACCESS_READ, UMatUsageFlags::USAGE_DEFAULT)?;
+ assert_eq!(3, umat.rows()?);
+ assert_eq!(1, umat.cols()?);
+ assert_eq!(i32::typ(), umat.typ()?);
+ let mat = umat.get_mat(ACCESS_READ)?;
+ assert_eq!(1, *mat.at_2d::<i32>(0, 0)?);
+ assert_eq!(2, *mat.at_2d::<i32>(1, 0)?);
+ assert_eq!(3, *mat.at_2d::<i32>(2, 0)?);
+ }
+ Ok(())
+}
+
+#[test]
+fn umat_for_rows_and_cols() -> Result<()> {
+ let mat = unsafe { UMat::new_rows_cols(400, 300, Vec3d::typ(), UMatUsageFlags::USAGE_DEFAULT) }?;
+ assert_eq!(Vec3d::typ(), mat.typ()?);
+ assert_eq!(Vec3d::depth(), mat.depth()?);
+ assert_eq!(Vec3d::channels(), mat.channels()?);
+ assert!(mat.is_continuous()?);
+ assert!(!mat.is_submatrix()?);
+ assert_eq!(Size::new(300, 400), mat.size()?);
+ assert_eq!(400, mat.rows()?);
+ assert_eq!(300, mat.cols()?);
+ assert_eq!(2, mat.mat_size()?.len());
+ assert_eq!(400, mat.mat_size()?[0]);
+ assert_eq!(300, mat.mat_size()?[1]);
+ assert_eq!(2, mat.dims()?);
+ assert_eq!(2, mat.mat_step()?.len());
+ assert_eq!(7200, mat.mat_step()?[0]);
+ assert_eq!(24, mat.mat_step()?[1]);
+ assert_eq!(24, mat.elem_size()?);
+ assert_eq!(8, mat.elem_size1()?);
+ assert_eq!(900, mat.step1(0)?);
+ assert_eq!(3, mat.step1(1)?);
+ assert_eq!(120000, mat.total()?);
+ Ok(())
+}
+
+#[test]
+fn umat_continous() -> Result<()> {
+ let s: Vec<Vec<f32>> = vec![
+ vec![1., 2., 3.],
+ vec![4., 5., 6.],
+ vec![7., 8., 9.],
+ ];
+
+ let umat = Mat::from_slice_2d(&s)?.get_umat(ACCESS_READ, UMatUsageFlags::USAGE_DEFAULT)?;
+
+ {
+ let sub_umat_non_cont = UMat::roi(&umat, Rect::new(1, 1, 2, 2))?;
+ assert_eq!(umat.typ()?, sub_umat_non_cont.typ()?);
+ assert_eq!(2, sub_umat_non_cont.rows()?);
+ assert_eq!(2, sub_umat_non_cont.cols()?);
+ assert!(sub_umat_non_cont.is_submatrix()?);
+ assert!(!sub_umat_non_cont.is_continuous()?);
+
+ let umat_clone = sub_umat_non_cont.clone()?;
+ assert_eq!(umat.typ()?, umat_clone.typ()?);
+ assert_eq!(2, umat_clone.rows()?);
+ assert_eq!(2, umat_clone.cols()?);
+ assert!(!umat_clone.is_submatrix()?);
+ assert!(umat_clone.is_continuous()?);
+ }
+
+ {
+ let sub_umat_cont = UMat::roi(&umat, Rect::new(0, 1, 3, 2))?;
+ assert_eq!(umat.typ()?, sub_umat_cont.typ()?);
+ assert_eq!(2, sub_umat_cont.rows()?);
+ assert_eq!(3, sub_umat_cont.cols()?);
+ assert!(sub_umat_cont.is_submatrix()?);
+ assert!(sub_umat_cont.is_continuous()?);
+ }
+
+ Ok(())
+}

improve UMat support