Python/Numpy Contiguousness Error (Bug #2100)
So I've recently installed OpenCV (via the most recent windows pre-built installation), and have been playing around with the python bindings.
I noticed when trying to implement and test the ANN_MLP python class that there's a subtle error that only appears when the second argument of ANN_MLP.predict() is Fortran contiguous, rather than C contiguous. I've attached copies of two scripts: one working and one dysfunctional. The only difference is how the variablie "z" is initialized.
This bug is easy to miss, as one-dimensional numpy arrays are both C and Fortran contiguous by default, whereas multidimensional arrays may vary based on the method of instantiation.
The quick-and-dirty fix would be just to force the second argument of MLP_ANN.predict(inputs,outputs) to be C contiguous, like so:
outputs = array(outputs,order='c')
The problem is that this bug probably exists for most or all python binding functions that take a Numpy array for output. A better fix would involve changing how C manipulates Numpy array data. If you access each member of a Numpy array's read-write buffer through strides while in C, then the continuity of the array is irrelevant.
If the latter is the preferred fix by the OpenCV team, and no one on the team is familiar with how to access Numpy stride values, and use them to properly index multidimensional Numpy arrays in C, then I could give a more detailed explanation in a followup post, as this post is already running long.
I set the priority of this bug to high under the assumption that it's endemic to many functions in the Python bindings. If I'm in error, then my sincerest apologies. Feel free to downgrade its priority if appropriate. Cheers.
work on #2100: pyopencv_to functions now can receive argument information through ArgInfo structure. Non-contiguous input numpy arrays are copied. In case of non-contiguous output array the TypeError is thrown.
Fixed in r0adf68ae. Following behaviour is implemented:
Most OpenCV functions treat arrays as "row-contiguous" (step[ndim-1] == elemsize). If non-row-contiguous NumPy array is passed to OpenCV function, it is either implicitly copied (for input arrays), or TypeError is thrown (for out and in/out arrays).
- Status changed from Open to Done
Reopened. Partially broken in rev. ff90c3eb.
No check for row-contiguous for multichannel arrays:
import numpy as np import cv2 a = np.zeros((100, 100, 3)) b = a[:, ::2] # not row-contiguous !!! cv2.circle(b, (20, 20), 20, (0, 255, 0)) # should fail here cv2.imshow('a', a) cv2.waitKey()
- Status changed from Done to Open