zoukankan      html  css  js  c++  java
  • numpy中,从一片c_void_p指向的区域里获取数据

    以前采用的数据拷贝的笨办法:

    1 bitmap_size = (1080, 1920, 3)
    2 buf = create_string_buffer(bitmap_size[0]*bitmap_size[1]*bitmap_size[2])
    3 pointer_data = c_void_p(addressof(xxxx))
    4 memmove(buf, pointer_data, bitmap_size[0]*bitmap_size[1]*bitmap_size[2])
    5 #
    6 np_arr = np.frombuffer(buf, dtype=np.uint8, count=bitmap_size[0]*bitmap_size[1]*bitmap_size[2])
    7 np_arr = np_arr.reshape(bitmap_size)

    现在找到了更好的办法,不用拷贝数据:

    1 bitmap_size = (1080, 1920, 3)
    2 bytes_count = bitmap_size[0]*bitmap_size[1]*bitmap_size[2]
    3 pointer_data = c_void_p(addressof(xxxx))
    4 #定义一个指向数组的指针
    5 array_pointer = cast(pointer_data, POINTER(c_char*bytes_count)))
    6 np_arr = np.frombuffer(array_pointer.contents, dtype=np.uint8, count=bytes_count)
    7 np_arr = np_arr.reshape(bitmap_size)
  • 相关阅读:
    setsid
    dup
    信号量
    linux标准输入输出
    linux守护进程范例
    c++字符串操作
    浏览器缓存
    bfc
    苹果手机自制铃声
    vue-cli 源码解读
  • 原文地址:https://www.cnblogs.com/ahfuzhang/p/10920095.html
Copyright © 2011-2022 走看看