python如何调用c++numpy.ndarray代码_pybind11—python numpy与C++数据传递

本文介绍了如何在Python中使用Pybind11调用C++代码,处理numpy.ndarray对象。通过示例展示了1D、2D、3D numpy数组的相加操作,并涉及了RGB图像转GRAY图像的处理和快速傅里叶变换(FFT)的C++实现。详细阐述了C++中解析和操作numpy数组的方法,以及与Python之间的数据传递。
摘要由CSDN通过智能技术生成

前言

一些处理矩阵运算,图像处理算法,直接采用python实现可能速度稍微慢,效率不高,或者为了直接在python中调用其他C++第三方库。 图像,矩阵在python中通常表示为numpy.ndarray,因此如何在C++中解析numpy对象,numpy的数据如何传递到C++非常关键,解决了这些问题,就可以丝滑的在python numpy和C++中切换,互相调用。

开发、测试环境

windows 10, 64bit

Anaconda3, with python 3.7

Visual Studio 2017

pycharm

pybind11

C++ Numpy

image.png

image.png

image.png

image.png

demo1

1d numpy.ndarray

2d numpy.ndarray

3d numpy.ndarray

C++ numpy传递参数

C++代码

#include

#include

#include

namespace py = pybind11;

/*

1d矩阵相加

*/

py::array_t add_arrays_1d(py::array_t& input1, py::array_t& input2) {

// 获取input1, input2的信息

py::buffer_info buf1 = input1.request();

py::buffer_info buf2 = input2.request();

if (buf1.ndim !=1 || buf2.ndim !=1)

{

throw std::runtime_error("Number of dimensions must be one");

}

if (buf1.size !=buf2.size)

{

throw std::runtime_error("Input shape must match");

}

//申请空间

auto result = py::array_t(buf1.size);

py::buffer_info buf3 = result.request();

//获取numpy.ndarray 数据指针

double* ptr1 = (double*)buf1.ptr;

double* ptr2 = (double*)buf2.ptr;

double* ptr3 = (double*)buf3.ptr;

//指针访问numpy.ndarray

for (int i = 0; i < buf1.shape[0]; i++)

{

ptr3[i] = ptr1[i] + ptr2[i];

}

return result;

}

/*

2d矩阵相加

*/

py::array_t add_arrays_2d(py::array_t& input1, py::array_t& input2) {

py::buffer_info buf1 = input1.request();

py::buffer_info buf2 = input2.request();

if (buf1.ndim != 2 || buf2.ndim != 2)

{

throw std::runtime_error("numpy.ndarray dims must be 2!");

}

if ((buf1.shape[0] != buf2.shape[0])|| (buf1.shape[1] != buf2.shape[1]))

{

throw std::runtime_error("two array shape must be match!");

}

//申请内存

auto result = py::array_t(buf1.size);

//转换为2d矩阵

result.resize({buf1.shape[0],buf1.shape[1]});

py::buffer_info buf_result = result.request();

//指针访问读写 numpy.ndarray

double* ptr1 = (double*)buf1.ptr;

double* ptr2 = (double*)buf2.ptr;

double* ptr_result = (double*)buf_result.ptr;

for (int i = 0; i < buf1.shape[0]; i++)

{

for (int j = 0; j < buf1.shape[1]; j++)

{

auto value1 = ptr1[i*buf1.shape[1] + j];

auto value2 = ptr2[i*buf2.shape[1] + j];

ptr_result[i*buf_result.shape[1] + j] = value1 + value2;

}

}

return result;

}

//py::array_t add_arrays_3d(py::array_t& input1, py::array_t& input2) {

//

// py::buffer_info buf1 = input1.request();

// py::buffer_info buf2 = input2.request();

//

// if (buf1.ndim != 3 || buf2.ndim != 3)

// throw std::runtime_error("numpy array dim must is 3!");

//

// for (int i = 0; i < buf1.ndim; i++)

// {

// if (buf1.shape[i]!=buf2.shape[i])

// {

// throw std::runtime_error("inputs shape must match!");

// }

// }

//

// // 输出

// auto result = py::array_t(buf1.size);

// result.resize({ buf1.shape[0], buf1.shape[1], buf1.shape[2] });

// py::buffer_info buf_result = result.request();

//

// // 指针读写numpy数据

// double* ptr1 = (double*)buf1.ptr;

// double* ptr2 = (double*)buf2.ptr;

// double* ptr_result = (double*)buf_result.ptr;

//

// for (int i = 0; i < buf1.size; i++)

// {

// std::cout << ptr1[i] << std::endl;

// }

//

// /*for (int i = 0;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值