简体   繁体   English

如何从C ++访问嵌入式python中的numpy数组?

[英]How do I access a numpy array in embedded python from c++?

What would be a good way to access a 2dim numpy array in c++? 在c ++中访问2dim numpy数组的好方法是什么? I've already checked out the numpy/c api and some other posts but that doesn't brought me further. 我已经检查了numpy / c api和其他一些文章,但是并没有进一步介绍我。 Here's the situation: 情况如下:

I defined in a python file called Testfile.py the following numpy array: 我在一个名为Testfile.py的python文件中定义了以下numpy数组:

import numpy as np
A = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])

Now, I would like to access this array in c++ to use it for further calculations. 现在,我想使用c ++访问此数组以将其用于进一步的计算。 Here is what i did so far. 这是我到目前为止所做的。 Note: For simplicity, i left out error handling and reference counting code snippets. 注意:为简单起见,我省略了错误处理和引用计数代码段。

#define NPY_NO_DEPRECATED_APINPY_1_7_API_VERSION
#define PY_ARRAY_UNIQUE_SYMBOL cool_ARRAY_API
#include <Python.h>
#include <arrayobject.h> // numpy!
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <string>

int main(){

// Name of input-file
char pyfilename[] = "Testfile";

// initilaize python interpreter
Py_Initialize();
import_array(); 

// load input-file
PyObject *pyName = PyUnicode_FromString(pyfilename);
PyObject *pyModule = PyImport_Import(pyName);

// import my numpy array object
char pyarrayname[] = "A";
PyObject *obj = PyObject_GetAttrString(pyModule, pyarrayname);

//------------------------------------------------------------
// The Problem starts here..

// Array Dimensions
npy_intp Dims[] = { PyArray_NDIM(obj) }; // array dimension
Dims[0] = PyArray_DIM(obj, 0); // number of rows
Dims[1] = PyArray_DIM(obj, 1); // number of columns

// PyArray_SimpleNew allocates the memory needed for the array.
PyObject *ArgsArray = PyArray_SimpleNew(2, Dims, NPY_DOUBLE);

// The pointer to the array data is accessed using PyArray_DATA()
double *p = (double *)PyArray_DATA(ArgsArray);

for (int i = 0; i<Dims[0]; i++)
{
    for (int j = 0; j<Dims[1]; j++)
        {
             p[i * Dims[1] + j] = *((int *)PyArray_GETPTR2(obj, i, j));
        }
}
// -----------------------------------------------------------


Py_Finalize();

return 0;
}

I use python 3.6 and MSVC 2015. 我使用python 3.6和MSVC 2015。

EDIT: I added the headers i use and changed the problem formulation a bit. 编辑:我添加了我使用的标头,并稍微改变了问题的表述。

EDIT: I added the proposed solution strategies provided by Swift and Alan Stokes 编辑:我添加了由SwiftAlan Stokes提供的建议的解决方案策略

After you access array by 在通过访问数组之后

double *p = (double*)PyArray_DATA(ArgsArray);
int* pA = (int*)PyArray_DATA(obj);

you can work with it like with array. 您可以像处理数组一样使用它。 What are dimensions? 什么是尺寸?

int height = PyArray_DIM(obj, 0);
int width = PyArray_DIM(obj, 1);

Now you can use that pointer to access data in array 现在您可以使用该指针访问数组中的数据

for ( int i = 0; y<height; y++) 
{ 
   for (int j = 0; x<width; x++) 
   { 
      p[i * width + j] = pA[i * width + j];
   }
}

Actually, if you just need to copy array, use memcpy or std::copy at this point. 实际上,如果只需要复制数组,请在此时使用memcpy或std :: copy。

Tbh, I'd considered to use boost.python instead, but it's my own preference. Tbh,我曾考虑改为使用boost.python,但这是我自己的偏好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过ZeroMQ将图像(opencv矩阵/ numpy数组)从C ++发布者传输到python发送者? - How to I transfer an image(opencv Matrix/numpy array) from c++ publisher to python sender via ZeroMQ? 如何使用指针将numpy数组传递给C ++ / SWIG而不依赖于numpy.i? - How do I pass a numpy array to C++/SWIG using pointers without relying on numpy.i? 在c ++中嵌入python,我需要安装python吗? - embedded python in c++, do i need python installed 如何将Numpy ND数组转换为CFFI C ++数组并再次返回? - How do I convert a numpy ND-array to a CFFI C++ array and back again? 如何将从 C++ 获得的数组转换为 Python 中的 2D numpy 数组,无需 for 循环 - How to convert my array obtained from C++ to a 2D numpy array in Python without for loop 如何在C ++中创建类似于Python的numpy数组的数组? - How to create array in C++ similar to Python's numpy array? 如何链接嵌入在 ac/c++ 应用程序中的 python - How can I link python embedded in a c/c++ application 使用boost python传递一个numpy数组FROM C ++ - Passing a numpy array FROM C++ using boost python 在 C++ 应用程序中从嵌入式 Python 调用时,Numpy 导入在多数组扩展库上失败 - Numpy import fails on multiarray extension library when called from embedded Python within a C++ application Pybind11 如何从 python -&gt; c++ 传递 n 维 numpy 数组 - Pybind11 How to pass an n-dimentional numpy array from python -> c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM