python调用c++传递数组的实例

yipeiwu_com6年前Python基础

如下所示:

INPUT = c_int * 4
# 实例化一个长度为2的整型数组
input = INPUT()
# 为数组赋值(input这个数组是不支持迭代的)
input[0] = 11
input[1] = 2
input[2] = 3
input[3] = 4
dll.teststring.restype = c_char_p
# bytes(aaaa, encoding="utf-8")
a = dll.teststring(input,4)


MYLIBDLL char* teststring(int* plus1, int len);

char* teststring(int* plus1,int len) {


	for (int i = 0; i < len; i++) {
		printf("%d \n", plus1[i]);
	} 




	Mat mat;
	//加载图片  
	mat = imread("bgs.jpg", CV_LOAD_IMAGE_COLOR);
	printf("a %d %d", mat.rows, mat.cols);
	//if (!mat.empty()) {


	int m, n;
	n = mat.cols * 3;
	m = mat.rows;
	unsigned char *data = (unsigned char*)malloc(sizeof(unsigned char) * m * n);
	int p = 0;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			data[p] = mat.at<unsigned char>(i, j);
			p++;
		}
	}
	*plus1 = p;
	return (char*)data;
}

以上这篇python调用c++传递数组的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python实现微信好友的数据分析

Python实现微信好友的数据分析

基于微信开放的个人号接口python库itchat,实现对微信好友的获取,并对省份、性别、微信签名做数据分析。 效果: 直接上代码,建三个空文本文件stopwords.txt,ne...

Pycharm学习教程(7)虚拟机VM的配置教程

Pycharm学习教程(7)虚拟机VM的配置教程

设想这样一种情况,你在一个平台上操作你的工程,但你希望在另外一个平台上完善并运行它,这就是为什么Pycharm做了很多工作来支持远程调试。   在虚拟机上运行一个工程主要包含以下步骤:...

Python实现发送QQ邮件的封装

Python实现发送QQ邮件的封装

本文实例为大家分享了Python实现发送QQ邮件的封装代码,供大家参考,具体内容如下 封装code import smtplib from email.mime.image impo...

wxPython+Matplotlib绘制折线图表

wxPython+Matplotlib绘制折线图表

使用Matplotlib在wxPython的Panel上绘制曲线图,需要导入: import numpy from matplotlib.backends.backend_wxagg...

python进程间通信Queue工作过程详解

Process之间有时需要通信,操作系统提供了很多机制来实现进程间的通信。 1. Queue的使用 可以使用multiprocessing模块的Queue实现多进程之间的数据传递,Que...