pybind11向C++ dll 传递数组 图像
python 通过pybind11向C++ dll 传递数组 图像
传递python中的List
pybind11
很贴心地帮你把 vector<T>
跟 python 的 list
做好了转换,你只需要 #include <pybind11/stl.h>
即可 [1]
C++端代码
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <vector>
using std::vector;
namespace py = pybind11;
vector<float> ListMul(vector<float>& in_list, float coef) {
vector<float> ret_vec;
ret_vec.clear();
ret_vec.reserve(in_list.size()); // Requests that the vector capacity be at least enough to contain n elements.
for (size_t i = 0; i < in_list.size(); ++i) {
int v = in_list[i];
ret_vec.push_back(coef * v);
}
return ret_vec;
}
PYBIND11_MODULE(example, m) {
m.doc() = "pass and return a list";
m.def("ListMul", &ListMul, "example function");
}
python端调用
import example
print(example.ListMul([1.0, 2, 3, 4, 5, 6], 5))
python端的数据类型有时候不是那么严格,会进行自动转换,但是python float到c的int会出错,
类似地还有map<Tk, Tv>
在 python 里对应的就是 Dict[Tk, Tv] [1]。
传递Numpy 数组
两个数组相加的案例 [2]
C++端代码
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
namespace py = pybind11;
py::array_t<double> add_arrays(py::array_t<double> input1, py::array_t<double> input2) {
// read input arrays buffer_info
py::buffer_info buf1 = input1.request(), buf2 = input2.request();
if (buf1.size != buf2.size)
throw std::runtime_error("Input shapes must match");
// allocate the output buffer
py::array_t<double> result = py::array_t<double>(buf1.size);
py::buffer_info buf3 = result.request(); // acquire buffer info
double *ptr1 = (double *)buf1.ptr, *ptr2 = (double *)buf2.ptr, *ptr3 = (double *)buf3.ptr;
size_t high = buf1.shape[0];
size_t width = buf1.shape[1];
// Add both arrays
for (size_t idy = 0; idy < high; idy++)
{
for (size_t idx = 0; idx < width; idx++)
{
int curIdx = idy*width + idx;
ptr3[curIdx] = ptr1[curIdx] + ptr2[curIdx];
}
}
/* Reshape result to have same shape as input */
result.resize({ high, width });
return result;
}
PYBIND11_MODULE(example, m) {
m.doc() = "Add two vectors using pybind11"; // optional module docstring
m.def("add_arrays", &add_arrays, "Add two NumPy arrays");
}
python端调用
import numpy as np
import example
a = np.ones((10,3))
b = np.ones((10,3)) * 3
c = example.add_arrays(a, b)
print(c)
These numpy
array arguments can either be generic py:array
or typed py:array_t<double>
. The properties of the numpy
array can be obtained by calling its request
method. This returns a struct
of the following form [3]:
struct buffer_info {
void *ptr;
size_t itemsize;
std::string format;
int ndim;
std::vector<size_t> shape;
std::vector<size_t> strides;
};
python传递图像到c++ dll
根据上面的案例,很容易写出C++端代码
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <iostream>
using namespace std;
namespace py = pybind11;
#include <io.h>
#include <fcntl.h>
// display console
void OpenConsole()
{
// create a console
AllocConsole();
//
FILE* stream;
freopen_s(&stream, "CON", "r", stdin); //
freopen_s(&stream, "CON", "w", stdout); //
SetConsoleTitleA("Information output"); //
HANDLE _handleOutput;
_handleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
// FreeConsole();
}
void GetImage(py::array_t<char> input1)
{
py::buffer_info buf1 = input1.request();
OpenConsole();
printf("dim:%d \n", buf1.ndim);
for (int cnt = 0; cnt < buf1.ndim; cnt++)
{
printf("dim size %d : %d\n", cnt, buf1.shape[cnt]);
}
unsigned char *ptr1 = (unsigned char *)buf1.ptr;
printf("image data: %d %d %d %d %d %d\n", ptr1[0], ptr1[1], ptr1[2], ptr1[3], ptr1[4], ptr1[5]);
}
PYBIND11_MODULE(example, m) {
m.doc() = "Get NumPy arrays"; // optional module docstring
m.def("GetImage", &GetImage, "Get NumPy arrays");
}
python端代码
import numpy as np
import example
from PIL import Image #PIL pakage name is Pillow
im = Image.open('leopard-2.png')
in_data = np.asarray(im, dtype=np.uint8)
in_data = np.rollaxis(in_data,2,0)
print(type(in_data))
example.GetImage(in_data)
pybind11向C++ dll 传递数组 图像相关推荐
- LabVIEW DLL传递一个二维数组报错
LabVIEW DLL传递一个二维数组报错 当调用一个LabVIEW DLL时,首先需要声明处理程序变量并将其初始化为NULL,比如,在C中,代码如下所示: main() { /* Labvie ...
- python 调用 c 生成数组_python调用c++传递数组的实例
如下所示: input = c_int * 4 # 实例化一个长度为2的整型数组 input = input() # 为数组赋值(input这个数组是不支持迭代的) input[0] = 11 inp ...
- python中的记录指针_使用Python向C语言的链接库传递数组、结构体、指针类型的数据...
使用python向C语言的链接库传递数组.结构体.指针类型的数据 由于最近的项目频繁使用python调用同事的C语言代码,在调用过程中踩了很多坑,一点一点写出来供大家参考,我们仍然是使用ctypes来 ...
- python传递参数 调用c++ 传递vector_python调用c++传递数组的实例
如下所示: INPUT = c_int * 4 # 实例化一个长度为2的整型数组 input = INPUT() # 为数组赋值(input这个数组是不支持迭代的) input[0] = 11 inp ...
- python结构体数组传出接收c动态库_使用Python向C语言的链接库传递数组、结构体、指针类型的数据...
使用python向C语言的链接库传递数组.结构体.指针类型的数据 由于最近的项目频繁使用python调用同事的C语言代码,在调用过程中踩了很多坑,一点一点写出来供大家参考,我们仍然是使用ctypes来 ...
- Go 学习笔记(10)— 数组定义、数组声明、数组初始化、访问数组、数组相等、向函数传递数组
1. 数组定义 数组是具有相同唯一类型的一组已编号且长度固定的数据项序列,这种类型可以是任意的原始类型例如整形.字符串或者自定义类型. 2. 声明数组 Go 语言数组声明需要指定元素类型及元素个数,语 ...
- 传递数组_Fortran:派生数组与数组传递进子程序耗费时间比较
在优化程序的过程中发现其中存在大量的派生类型变量(type),同时发现Fortran子程序可以接受type类型数组中元素,即将type类型中元素当作独立的数组传递.传递过程如下所示: ... type ...
- ajax传递数组 php,jQuery.ajax向后台传递数组问题如何解决
本文主要为大家详细介绍了jQuery.ajax向后台传递数组问题的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能帮助到大家. 今天重温了一个问题,jQuery.ajax向后台传递一 ...
- springMVC通过ajax传递参数list对象或传递数组对象到后台
springMVC通过ajax传递参数list对象或传递数组对象到后台 环境: 前台传递参数到后台 前台使用ajax 后台使用springMVC 传递的参数是N多个对象 JSON对象和JSON字符串 ...
最新文章
- Mathematica该注意的两种特殊的输入方式(blanksequence and ruledelayed)
- VS2017源代码版本管理
- efi分区咋移动到c盘里_怎么手动安装CLOVER到U盘EFI分区
- 每日一笑 | 一些关于学编程的领悟
- python高阶函数filter_Python进阶系列连载(13)——Python内置高阶函数filter(上)...
- matplotlib的优点_超详细matplotlib基础介绍!!!
- jQuery之防止【冒泡事件】,阻止默认行为 【return false】 event.stopPropagation event.preventDefault...
- 正则表达式的运算符优先级
- python vars() 函数用法及实例
- 【kafka】kafka jmx docker 容器下 跨容器连接 没有到主机的路由 host unreachable
- 使用webpack5模块联邦
- 如何自制会跳舞的AI小姐姐?这有一份易上手的开源攻略
- 2010.11.18 关于向窗口发送消息
- MicroMsg.SDK.WXMediaMessage: checkArgs fail, thumbData is invalid
- MSN天气不显示数据、打不开、微软商店打不开报错0x80131500
- ProxySQL 配置详解及读写分离(+GTID)等功能说明2 (完整篇)
- hypermill后处理构造器安装_ug10后处理安装步骤ug后处理在什么位置ug法兰克后处理下载ug后处理器如何设置ug后处理构造器...
- IC/FPGA面试之任意时钟分频电路的产生
- 嵌入式物联网技术栈【协议篇】OPC UA协议
- 基于Spring boot的个人理财系统
热门文章
- Android--AudioManager控制音量
- 不区分大小写比较Java_java-如何使字符串比较不区分大小写?
- linux git hudson,如何使用SSH密钥配置Hudson和git插件
- php redis 密码,redis如何设置密码
- java 调用 swf 文件上传_java文件上传方法
- rest_快速检查REST API是否有效的方法-从清单文件中获取详细信息
- maven_Maven排除所有传递依赖项
- python 设计模式 原型模式_python设计模式–原型模式
- Eclipse单元测试Android编程,在Eclipse中进行Android单元测试-Fun言
- 增强使用功能的Steam开源工具箱一枚