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

相关文章

python3.0 字典key排序

IDLE 3.0 >>> dic = {"aa":1,"bb":2,"ab":3} >>> dic {'aa': 1, 'ab': 3, 'bb':...

python根据出生日期获得年龄的方法

本文实例讲述了python根据出生日期获得年龄的方法。分享给大家供大家参考。具体如下: 这段代码可以根据用户的出生日期获得其年龄,born参数为date类型 def calculat...

Python 中字符串拼接的多种方法

python拼接字符串一般有以下几种方法: ①直接通过(+)操作符拼接 s = 'Hello'+' '+'World'+'!' print(s) 输出结果: Hello Worl...

python中使用ctypes调用so传参设置遇到的问题及解决方法

问题 近日在做一组声纹聚类时,使用了另一团队同学开发的声纹距离算法。该算法对外提供的是一组so包,需要使用方自己去使用。在python中调用纯so包一般使用ctypes类库,用起来看起来...

Python实现正则表达式匹配任意的邮箱方法

Python实现正则表达式匹配任意的邮箱方法

首先来个简单的例子,利用Python实现匹配163邮箱的代码: #-*- coding:utf-8 -*- __author__ = '杨鑫' import re text = in...