简体   繁体   中英

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.

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?

You can specify it as numpy.typing.NDArray with an entry type. For example,

import numpy as np

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

def my_fun(img: Mat):
    pass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM