我有一个数字数组和C++代码中的二维数据,我想对这些数据执行一些操作。使用swig和distutils以及numpy.i,我成功地将所有内容编译成一个python扩展"goldstein",该扩展提供了一个函数unwrap2d。我用
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);
}
%}发布于 2013-08-16 13:37:39
@Jaimie当然是对的。跟‘双人’一样,‘浮’是64位的,这就是为什么我没有再检查一遍的原因,我只是想起来没什么关系。但是c++中的浮动显然需要32位,在numpy中是'float32'。谢谢!
https://stackoverflow.com/questions/18270618
复制相似问题