Skip to content

Python: unable to pass 3D matrix to C++ code #19091

@alalek

Description

@alalek

Currently OpenCV Python bindings "automatically" converts 3D NumPy arrays into 2D cv::Mat with wrapping last dimension onto number of channels (to simulate Image with colors):

a = np.zeros((2,3,4), dtype='f')
print(a.shape)
info = cv.utils.dumpInputArray(a)
print(info)

output:

(2, 3, 4)
InputArray: empty()=false kind=0x00010000 flags=0x01010000 total(-1)=6 dims(-1)=2 size(-1)=3x2 type(-1)=CV_32FC4

Related code is here (previous variant of similar code is here from 2010). By initial design there is support for 2D matrices and 1D arrays only.

So currently there is no way to pass 3D cv::Mat (1-channel) from Python to C++ code.

Just changing of mentioned code is not an option due to breaking of many user applications.
Compatibility should be preserved.

Note: There is such issue in Java because Java's Mat type is used.
Python's bindings relies on np.array type which leads to mentioned reinterpretation issues.


relates #17272
relates #19001
probably relates #18413 #17456

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions