Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use numeric dtype for MatLike instead of generic #25406

Merged

Conversation

VadimLevin
Copy link
Contributor

closes #24803

cv2/typing/__init__.py:

import typing as _typing

if _typing.TYPE_CHECKING:
    NumPyArrayNumeric = numpy.ndarray[_typing.Any, numpy.dtype[numpy.integer[_typing.Any] | numpy.floating[_typing.Any]]]
else:
    NumPyArrayNumeric = numpy.ndarray

MatLike = _typing.Union[cv2.mat_wrapper.Mat, NumPyArrayNumeric]

Test file (test_numeric_ndarray.py) content:

import numpy as np
import cv2

mask = np.zeros((4, 4), dtype=np.uint8)
contours, hierarchy = cv2.findContours(mask, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_TC89_KCOS)
contour = contours[0] * 2

pyproject.toml content

[tool.mypy]
strict = true
ignore_missing_imports = true
plugins = [
    "numpy.typing.mypy_plugin"
]
$ mypy test_numeric_ndarray.py
Success: no issues found in 1 source file

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@asmorkalov asmorkalov assigned asmorkalov and unassigned VadimLevin Apr 12, 2024
@asmorkalov asmorkalov merged commit 0bd066e into opencv:4.x Apr 13, 2024
26 of 28 checks passed
@asmorkalov asmorkalov mentioned this pull request Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type annotation issues with opencv-python 4.9.0.80
3 participants