python: cv.Mat wrapper over numpy.ndarray#20558
Conversation
| obj = arr.view(Mat) | ||
| return obj | ||
|
|
||
| def __init__(self, arr, **kwargs): |
There was a problem hiding this comment.
Why __init__ is required?
I think Mat is RealisticInfoArray from the user guide except that Mat should wrap only numeric types. For Python objects and strings it should raise AssertionError
And it is better to explicitly add wrap_channels argument to __new__ with the documentation string.
There was a problem hiding this comment.
It make sense to keep instance custom initialization in __init__ instead of __new__.
- https://docs.python.org/3/reference/datamodel.html
- debugging purposes
- code autocompletion works better with
__init__
I thought about pre-checks too, but they can be added later. At least to keep this patch minimal. Currently such cases are catched in cv2.cpp anyway.
| raise unittest.SkipTest('Python 2.x is not supported') | ||
|
|
||
|
|
||
| class MatTest(NewOpenCVTests): |
There was a problem hiding this comment.
2 practical use cases to test:
- Create a
Matwrapper forndarrayand try to use one of the NumPyufuncs e.g.:
data = np.arange(10)
mat_data = cv.Mat(data)
np.testing.assert_equal(2 * data, 2 * mat_data)- Should 2
Matobjects constructed with different value ofwrap_channelsproperty be treated as equals?
data = np.ones((10, 10, 3))
mat_wrapped = cv.Mat(data, wrap_channels=True)
mat_simple = cv.Mat(data)
self.assertEqual(mat_wrapped, mat_simple) # ???There was a problem hiding this comment.
@VadimLevin Thank you for the review!
I added ufunc test case.
Comparison is tricky for now (numpy == returns an array which can't be casted to boolean result without .any/.all calls), so I would like to postpone introducing of custom comparison overloads.
|
👍 |
|
I just learned of the existence of this wrapper thanks to a question on Stack Overflow, which lead me to dig through the repo. The only hint at its existence seems to be a single sentence in the note you added to https://docs.opencv.org/4.x/da/d49/tutorial_py_bindings_basics.html
.... and just like in the quote, the link in the documenation goes to https://docs.opencv.org/4.x/d3/d63/classcv_1_1Mat.html which contains no mention at all of anything relevant. This really needs better documentation. |
resolves #19091
On hold, merge after: #20611Usage:
C++-written cv.Mat is not reliable with Python limited API and numpy 1.20+ (not stable ABI guarantee).
Package loader is required (OPENCV_SKIP_PYTHON_LOADER must be OFF).
No plans to backport to "3.4" maintenance branch.
No plans to support Python 2.7.
TODO: