__init__.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. __all__ = []
  2. import numpy as np
  3. import cv2 as cv
  4. from typing import TYPE_CHECKING, Any
  5. # Same as cv2.typing.NumPyArrayNumeric, but avoids circular dependencies
  6. if TYPE_CHECKING:
  7. _NumPyArrayNumeric = np.ndarray[Any, np.dtype[np.integer[Any] | np.floating[Any]]]
  8. else:
  9. _NumPyArrayNumeric = np.ndarray
  10. # NumPy documentation: https://numpy.org/doc/stable/user/basics.subclassing.html
  11. class Mat(_NumPyArrayNumeric):
  12. '''
  13. cv.Mat wrapper for numpy array.
  14. Stores extra metadata information how to interpret and process of numpy array for underlying C++ code.
  15. '''
  16. def __new__(cls, arr, **kwargs):
  17. obj = arr.view(Mat)
  18. return obj
  19. def __init__(self, arr, **kwargs):
  20. self.wrap_channels = kwargs.pop('wrap_channels', getattr(arr, 'wrap_channels', False))
  21. if len(kwargs) > 0:
  22. raise TypeError('Unknown parameters: {}'.format(repr(kwargs)))
  23. def __array_finalize__(self, obj):
  24. if obj is None:
  25. return
  26. self.wrap_channels = getattr(obj, 'wrap_channels', None)
  27. Mat.__module__ = cv.__name__
  28. cv.Mat = Mat
  29. cv._registerMatType(Mat)