From 1efa7e9c1278da813f295fa5d6ae710d348e563c Mon Sep 17 00:00:00 2001 From: Vladimir Iglovikov Date: Sat, 17 Oct 2026 07:32:41 +0545 Subject: [PATCH] fix(python): accept NumPy inputs in UMat constructor stubs --- .../typing_stubs_generation/api_refinement.py | 13 +++++++++++++ modules/python/test/test_input_array_typing.py | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/modules/python/src2/typing_stubs_generation/api_refinement.py b/modules/python/src2/typing_stubs_generation/api_refinement.py index 1d9f889245..12868ad609 100644 --- a/modules/python/src2/typing_stubs_generation/api_refinement.py +++ b/modules/python/src2/typing_stubs_generation/api_refinement.py @@ -455,10 +455,23 @@ def refine_min_max(root_node: NamespaceNode, "src1_type", (first_type, AliasRefTypeNode("Scalar")) ) +def refine_umat_constructor(root_node: NamespaceNode, + function_symbol_name: SymbolName) -> None: + # shadow_umat.hpp maps Mat inputs even though C++ constructors declare UMat. + constructor = find_function_node(root_node, function_symbol_name) + for overload in constructor.overloads: + index = _find_argument_index(overload.arguments, "m") + if index is not None: + argument = overload.arguments[index] + argument.type_node = UnionTypeNode( + "umat_input", (cast(TypeNode, argument.type_node), AliasRefTypeNode("MatLike")) + ) + NODES_TO_REFINE = { SymbolName(("cv", ), (), "min"): refine_min_max, SymbolName(("cv", ), (), "max"): refine_min_max, + SymbolName(("cv", ), ("UMat", ), "UMat"): refine_umat_constructor, SymbolName(("cv", ), (), "add"): make_matlike_or_scalar_arg("src2"), SymbolName(("cv", ), (), "multiply"): make_matlike_or_scalar_arg("src2"), SymbolName(("cv", ), (), "remap"): make_optional_arg("map2"), diff --git a/modules/python/test/test_input_array_typing.py b/modules/python/test/test_input_array_typing.py index 7f7e7fce39..676ffb3687 100644 --- a/modules/python/test/test_input_array_typing.py +++ b/modules/python/test/test_input_array_typing.py @@ -43,6 +43,24 @@ class input_array_typing_test(NewOpenCVTests): self.assertIsInstance(result, cv.UMat) np.testing.assert_array_equal(result.get(), expected) + def test_umat_numpy_constructors(self): + for flat in (np.arange(45, dtype=np.uint8), np.arange(45, dtype=np.float32)): + for channels in (1, 3): + shape = (3, 5) if channels == 1 else (3, 5, channels) + image = flat[:15 * channels].reshape(shape) + original = image.copy() + for actual, expected in ( + (cv.UMat(image), original), + (cv.UMat(m=image), original), + (cv.UMat(image, (0, 2), (0, 3)), original[:2, :3]), + (cv.UMat(image, (1, 1, 3, 2)), original[1:3, 1:4]), + (cv.UMat(image, [(0, 2), (0, 3)]), original[:2, :3]), + ): + result = actual.get() + np.testing.assert_array_equal(result, expected) + self.assertEqual(result.dtype, original.dtype) + np.testing.assert_array_equal(image, original) + def test_empty_second_remap_map(self): image = np.arange(15, dtype=np.uint8).reshape(3, 5) x, y = np.meshgrid(np.arange(5, dtype=np.float32), np.arange(3, dtype=np.float32))