3
我在numpyarray和C++代码中有两维数据,我想对这些数据执行一些操作。使用swig和distutils以及numpy.i
我设法将所有东西编译成一个python扩展名“goldstein”,它提供了一个函数unwrap2d
。 我使用 当将2d numpy数组传递给C++时的TypeError
import goldstein
data = np.ascontiguousarray(data_temp, dtype='double')
mask = np.ascontiguousarray(mask_temp, dtype='uint16')
outdata = np.ones_like(data)
goldstein.unwrap2d(data,mask,outdata)
我得到一个TypeError: array cannot be safely cast to required type
测试。任何人都可以指出我如何以正确的方式传递这些数组?
参考:为了产生模块我使用的接口文件
%define DOCSTRING
"Wrapper for c++-code"
%enddef
%module(docstring=DOCSTRING) goldstein
%{
#define SWIG_FILE_WITH_INIT
#include "goldstein.h"
%}
/*include numpy typemaps*/
%include "numpy.i"
/*initialize module*/
%init %{
import_array();
%}
%rename(unwrap2d) phase_unwrapping_func;
/* typemaps for the arrays*/
%apply (int DIM1,int DIM2,float* IN_ARRAY2) {(int xsize,int ysize,float* in)};
%apply (int DIM1,int DIM2,unsigned short* IN_ARRAY2) {(int x1,int y1,unsigned short* mask)};
%apply (int DIM1,int DIM2,float* INPLACE_ARRAY2) {(int x2,int y2,float* out)};
/*wrapper function calling the original phase_unwrapping using only the needed parameters*/
%inline %{
void phase_unwrapping_func(int xsize,int ysize,float* in,int x1,int y1,unsigned short* mask,int x2,int y2,float* out) {
phase_unwrapping(xsize, ysize, in, mask, out);
}
%}
我试图打开语法突出显示,但我没有成功...... :( – Dux
你的'data'数组是dtype double的,但是你的C函数期望有一个浮点数,这可能是使NumPy抱怨的原因。 'data = np.ascontiguousarray(data_temp,dtype ='float')'在调用你的函数之前应该注意这一点。 – Jaime