fix(python): accept NumPy inputs in UMat constructor stubs

This commit is contained in:
Vladimir Iglovikov
2026-10-17 07:32:41 +05:45
committed by Alexander Smorkalov
parent 4d190b202f
commit 1efa7e9c12
2 changed files with 31 additions and 0 deletions

View File

@@ -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"),

View File

@@ -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))