简体   繁体   English

如何在 Python 中输入提示 numpy arrays(OpenCV 图像)?

[英]how do I type-hint numpy arrays (OpenCV images) in Python?

This question is not about what data the OpenCV image contains but about what type information to put into a python function.这个问题不是关于 OpenCV 图像包含哪些数据,而是关于要放入 python function 的类型信息。

What I do now is:我现在要做的是:

import numpy as np
import cv2

Mat = np.ndarray

def my_fun(image: Mat):
    cv2.imshow('display', image)
    cv2.waitKey()

Is there any better way to add typing information for OpenCV images in python?有没有更好的方法在 python 中为 OpenCV 图像添加打字信息?

You can specify it as numpy.typing.NDArray with an entry type.您可以使用条目类型将其指定为numpy.typing.NDArray For example,例如,

import numpy as np

Mat = np.typing.NDArray[np.uint8]

def my_fun(img: Mat):
    pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Python 中键入提示嵌套对象? - How can I type-hint a nested object in Python? 我如何键入提示Python函数返回从超类派生的任何类的实例? - How do I type-hint that a Python function returns instance of any class derived from a superclass? Python:如何键入提示 class 变量? - Python: How to type-hint a class variable? 是否可以在 python 中键入提示已编译的正则表达式? - Is it possible to type-hint a compiled regex in python? 如何在函数参数中键入提示“SupportsOrdering”? - How to type-hint “SupportsOrdering” in a function parameter? 如何正确键入提示 Python 字典,其键为 Type[T] 且值为 generics 该类型? - How to properly type-hint a Python dict whose keys are Type[T] and values are generics of that type? 如何对返回类型取决于参数的输入类型的函数进行类型提示? - How can I type-hint a function where the return type depends on the input type of an argument? Python类型提示友好类型,约束可能的值 - Python type-hint friendly type that constrains possible values 您如何键入提示 class 原本不存在的原型? - How do you type-hint class prototypes that don't otherwise exist? 如何在python3中键入提示matplotlib.axes._subplots.AxesSubplots对象 - How to type-hint a matplotlib.axes._subplots.AxesSubplots object in python3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM