I have two numpy 3D-array in python with different height and width. I want to pass them to my C-Extension. How can I resize and subtract them in c++? Please see the comments in the code.
static PyObject *my_func(PyObject *self, PyObject *args)
{
Py_Initialize();
import_array();
PyObject *arr1;
PyObject *arr2;
if(!PyArg_ParseTuple(args, "OO", &arr1, &arr2))
{
return NULL;
}
//How can I do this?
//resize arr1 to [100, 100, 3]
//resize arr2 to [100, 100, 3]
//res = arr1 - arr2
//return res
}
Start by making the desired shape. It's easier to do this as a tuple than a list:
Check this against NULL to ensure do error has occurred and handle if it has.
You can call the numpy
resizefunction on both arrays to resize them. Unless you are certain that the data isn't shared then you need to callnumpy.resizerather than the.resizemethod of the arrays. This involves importing the module and getting the resize attribute:I've omitted all the error checking, which you should do after each stage.
Make sure you decrease the reference counts on the various
PyObjects once you don't need them any more.Use
PyNumber_Subtractto do the subtraction (do it on the result fromresize).Addition: A shortcut for calling resize that should avoid most of the intermediates:
(The
"(iii)"creates the shape tuple rather than needing to do it separately.)If you are certain that
arr1andarr2are the only owners of the data then you can call the numpy.resizemethod either by the normal C API function calls or the specific numpy functionPyArray_Resize.