Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将2D numpy数组转换为C++;短**?_C++_Python_Numpy_Shared Libraries_Short - Fatal编程技术网

C++ 将2D numpy数组转换为C++;短**?

C++ 将2D numpy数组转换为C++;短**?,c++,python,numpy,shared-libraries,short,C++,Python,Numpy,Shared Libraries,Short,我在共享C++库中使用Python调用方法。我有一个问题转换为一个函数输入的一个NUMPY 2D阵列到一个C++的2D数组。我创建了一个玩具示例,展示了这个问题。请随意编译并试用 下面是python代码(soexample.py): 这里是C++库(SOStudio.CPP): 运行python文件时,出现以下错误: >> python soexample.py Traceback (most recent call last): File "soexample.py", lin

我在共享C++库中使用Python调用方法。我有一个问题转换为一个函数输入的一个NUMPY 2D阵列到一个C++的2D数组。我创建了一个玩具示例,展示了这个问题。请随意编译并试用

下面是python代码(soexample.py):

这里是C++库(SOStudio.CPP):

运行python文件时,出现以下错误:

>> python soexample.py
Traceback (most recent call last):
  File "soexample.py", line 13, in <module>
    cpplib.func_py(cppobj,array)
ctypes.ArgumentError: argument 2: <type 'exceptions.TypeError'>: Don't know how to     convert parameter 2
>python soexample.py
回溯(最近一次呼叫最后一次):
文件“soexample.py”,第13行,在
cpplib.func_py(cppobj,数组)
ctypes.ArgumentError:参数2::不知道如何转换参数2

如何正确纠正这一不幸的
类型错误

您可以使用
ctypes
c\u short
指针
来帮助中间转换。下面的函数将numpy数组转换为C类型的2darray,该数组可以传递到需要
short**
的C函数中

def c_short_2darr(numpy_arr):
  c_short_p = POINTER(c_short)
  arr = (c_short_p * len(numpy_arr) ) ()
  for i in range(len(numpy_arr)):
    arr[i] = (c_short * len(numpy_arr[i]))()
    for j in range(len(numpy_arr[i])):
      arr[i][j] = numpy_arr[i][j]
  return arr
注意,我修改了
func_py
CPPClass::func
以获取给定数组的宽度和长度这两个额外参数。这样,
CPPClass::func
就可以打印出数组的所有元素:

// ...
void CPPClass::func(unsigned short **array, size_t w, size_t h)
{
    for(size_t i = 0; i < w; ++i)
    {
      for(size_t j = 0; j < h; ++j)
          cout << array[i][j] << ", ";
      cout << '\n';
    }
}
// ...
void func_py(CPPClass *myClass,
             unsigned short **array, 
             size_t w, size_t h)
{
    myClass->func(array, w, h);
}

我相信C的短整数是16位。另一方面,默认的numpy int通常是32位。您可以尝试将数组创建为
array=np.array([[1,2,3],[1,2,3]],dtype=np.uint16)
并查看结果。谢谢!这非常有效。我还有一个类似的问题,你可以在这里帮助我:
>> python soexample.py
Traceback (most recent call last):
  File "soexample.py", line 13, in <module>
    cpplib.func_py(cppobj,array)
ctypes.ArgumentError: argument 2: <type 'exceptions.TypeError'>: Don't know how to     convert parameter 2
def c_short_2darr(numpy_arr):
  c_short_p = POINTER(c_short)
  arr = (c_short_p * len(numpy_arr) ) ()
  for i in range(len(numpy_arr)):
    arr[i] = (c_short * len(numpy_arr[i]))()
    for j in range(len(numpy_arr[i])):
      arr[i][j] = numpy_arr[i][j]
  return arr
// ...
void CPPClass::func(unsigned short **array, size_t w, size_t h)
{
    for(size_t i = 0; i < w; ++i)
    {
      for(size_t j = 0; j < h; ++j)
          cout << array[i][j] << ", ";
      cout << '\n';
    }
}
// ...
void func_py(CPPClass *myClass,
             unsigned short **array, 
             size_t w, size_t h)
{
    myClass->func(array, w, h);
}
>>> arr = numpy.array([ [1,2,3], [4,5,6] ])
>>> arr
array([[1, 2, 3],
       [4, 5, 6]])
>>> cpplib.func_py(cppobj, c_short_2darr(arr), 2, 3)
1, 2, 3,
4, 5, 6,
0