Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The problem is the way dstack constructs the array:

In [1]: import cv2

In [2]: channel = np.arange(240 * 320, dtype=np.uint8).reshape(240, 320, 1)

In [3]: merged = cv2.merge([channel for i in range(3)])

In [4]: stacked = np.dstack([channel for i in range(3)])

In [5]: stacked.strides
Out[5]: (1, 240, 76800)

In [6]: merged.strides
Out[6]: (960, 3, 1)

In order to reshape and stack the arrays in them manner you requested, numpy only modifies the strides through the array, it does not move the data around. merge appears to actually move the data around.

I think OpenCV can represent arrays stored in this fashion, but the python wrappers for OpenCV do not perform this translation.

See here for more information about numpy's strided array storage.