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设计】。

相关文章

5款Python程序员高频使用开发工具推荐

5款Python程序员高频使用开发工具推荐

很多Python学习者想必都会有如下感悟:最开始学习Python的时候,因为没有去探索好用的工具,吃了很多苦头。后来工作中深刻体会到,合理使用开发的工具的便利和高效。今天,我就把Pyth...

python tkinter界面居中显示的方法

由于tkinter没有直接提供居中显示的api,因此,要想将tk的对话框居中显示,需要用到tk自带的设定位置的方法geometry() nScreenWid, nScreenHei...

python reduce 函数使用详解

python reduce 函数使用详解

reduce() 函数在 python 2 是内置函数, 从python 3 开始移到了 functools 模块。 官方文档是这样介绍的 reduce(...) reduce(fu...

python3.5 tkinter实现页面跳转

python3.5 tkinter实现页面跳转

本文实例为大家分享了tkinter实现页面跳转的具体代码,供大家参考,具体内容如下 主函数main.py from tkinter import * from LoginPag...

Python简单读写Xls格式文档的方法示例

Python简单读写Xls格式文档的方法示例

本文实例讲述了Python简单读写Xls格式文档的方法。分享给大家供大家参考,具体如下: 1. 模块安装 使用pip install命令安装, 即: pip install xlrd...