Giter Club home page Giter Club logo

Comments (4)

slanzmich avatar slanzmich commented on June 12, 2024
  • The borderValue argument to cv2.erode is annotated as cv2.typing.Scalar, which is defined as Sequence[float]. However, the code also works with passing a single int. Would it make sense to define Scalar = float | Sequence[float]? The same applies to color argument to the cv2.circle, and probably others.
  • cv2.filter2D should also have at least slightly more specific type annotations.

The list of functions is quite arbitrary, I have only listed what is reported in our current code base.

Does the OpenCV Python API actually support arrays that contain np.generic but not np.number? These could be Booleans or any opaque data that does not allow arithmetic.

For functions that only support integer and floating point numbers (but not complex floats), np.ndarray[Any, np.dtype[np.integer | np.floating]] could be a better choice than np.ndarray[Any, np.dtype[np.number]]. The latter includes complex numbers, which do not support %.

from opencv.

johnthagen avatar johnthagen commented on June 12, 2024

We get tons of new Mypy errors on OpenCV 4.9 on the following functions:

  • transform
  • bitwise_and
  • approxPolyDP
  • connectedComponents
  • putText
  • rectangle
  • etc.

Of the form:

error: Incompatible types in assignment (expression has type "ndarray[Any, dtype[generic]]", variable has type "ndarray[Any, dtype[signedinteger[_64Bit]]]")  [assignment]

Variables passed into these functions are type annotated like:

input: NDArray[np.int_] = ...

Environment

  • opencv-python-headless 4.9.0.80
  • mypy 1.8.0
  • Pythom 3.10
[tool.mypy]
ignore_missing_imports = true
strict = true
disallow_subclassing_any = false
plugins = [
    "pydantic.mypy",
    "numpy.typing.mypy_plugin",
]

from opencv.

slanzmich avatar slanzmich commented on June 12, 2024

Right, for these types of issues, functions that preserve the input array shape and data type would need to use a TypeVar rather than independent cv2.typing.MatLike. An example of the to-be-generated code would look like this:

NumPyArrayNumeric = np.ndarray[Any, np.dtype[np.number]]
ArrayT = TypeVar("ArrayT", bound=NumPyArrayNumeric)

def erode(
    src: ArrayT,
    kernel: cv2.typing.MatLike,
    dst: ArrayT | None = ...,
    anchor: cv2.typing.Point = ...,
    iterations: int = ...,
    borderType: int = ...,
    borderValue: cv2.typing.Scalar = ...,
) -> ArrayT:
    ...

Whether the type of src is preserved depends on the individual function, though. For functions like cv2.filter2D, the ddepth argument can only be properly accounted for using overloads (if that level of typing is desired at all). Ideally, this kind of information would be encoded in the C++ documentation comments, but I don't know how to best do this with doxygen.

from opencv.

asmorkalov avatar asmorkalov commented on June 12, 2024

@VadimLevin Could you take a look on this?

from opencv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.