将三级指针数组转换为NumPy数组或列表Python

3
我正在通过将C代码集成到我的Python程序中来加速程序。我正在使用ctypes从Python执行C函数。
C程序:
    #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define MAX_ITERATIONS 1000

static void calculate_pixel(int x, int y, int size, float*** img){
    int i = 0;
    float a = 0.0, b = 0.0;

    while(a*a+b*b <= 4.0 && i < MAX_ITERATIONS){
        float temp_a = a;
        a = a*a - b*b + x;
        b = 2*a*b + y;

        i++;
    }

    float temp[3];
    memset(&temp, 0, sizeof(float)*3);

    if(i != MAX_ITERATIONS){
        float brightness = logf(1.75 + i - logf(log(a*a + b*b))) / log(MAX_ITERATIONS);
        temp[0] = brightness;
        temp[1] = brightness;
        temp[2] = 1;
    }

    memcpy(img[x][y], &temp, sizeof(float)*3);
} 

float*** mandel(int size){
    ssize_t len = (size)*sizeof(float*);
    float*** img = malloc(len);

    int x, y;
    for(x = 0; x < size; x++){
        img[x] = malloc(len);
        for(y = 0; y < size; y++){
            img[x][y] = malloc(sizeof(float)*3);
            calculate_pixel(x, y, size, img);
        }
    }

    return img;
}

Python程序:
 from ctypes import *
 import matplotlib.pyplot as pl

 size = 1000
 lib = './mandelbrot3.so'

 dll = cdll.LoadLibrary(lib)
 dll.mandel.argtypes = [c_int]

 #what i get in return from the c program
 dll.mandel.restype = POINTER(POINTER(POINTER(c_float*3)*size)*size)

 #calling function "mandel" in c program
 res = dll.mandel(size)

 #printing first value, does work this way
 print(res.contains[0].contains[0].contains[0])

 #creating a picture with pyplot, and inserting the array this way,
 #does not work because its pointers
 pl.imshow(res.contains)
 pl.show()

`dll.mandel.restype`是一个三级指针,大小为1000*1000*3。这创建了一个尺寸为1000*1000像素的图片,其中3个浮点数是RGB值。
我的问题是,从C程序返回的只是一个三级指针。我需要能够将其转换为普通的3D Python列表或NumPy数组。有没有比使用for循环读取指针数组中的所有元素并将它们插入新列表或NumPy数组更好的方法?

这看起来不像是一个3D数组,而是一个三级指针。它们是根本不同的数据结构。 - too honest for this site
我应该一起发布我的 C 代码吗? - Murillio4
注意:成为三星级的C程序员并不是一种称赞。 - too honest for this site
不是说我...只是试图解决这个任务。如果你有一个更好的解决方案,请分享 :) - Murillio4
1个回答

0

我不确定这个三级指针的问题,但是将C语言中的数组传递到numpy的方法如下:

假设你已经收到了一个ctypes的POINTER指向你的图像缓冲区,并且你知道heightwidth和数据类型...

你可以通过以下方式从缓冲区创建一个与你的图像大小相同的ctypes数组:

arr = (c_float * height * width * 3).from_address(addressof(data.contents))

在这种情况下,dataPOINTER
然后使用 buffer 关键字参数从中创建一个 ndarray
import numpy as np
img = np.ndarray(buffer=arr, dtype=np.float32, shape=(height, width, 3))

谢谢!最终我用这种方式完成了,但是降到了双指针。 - Murillio4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接