在Python中将C回调字节数组转换为uint16_t数组
我使用的是一台输出12位图像数据的摄像机。用于接收帧的回调返回一个8位数组,其中12位数据按以下格式拆分为两个字节在Python中将C回调字节数组转换为uint16_t数组,python,numpy,opencv,Python,Numpy,Opencv,我使用的是一台输出12位图像数据的摄像机。用于接收帧的回调返回一个8位数组,其中12位数据按以下格式拆分为两个字节 | Byte 1 | Byte 2 | | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | |MSB| X | X | X | X | X | X | X | X | X | X |LSB| - | - | -
| Byte 1 | Byte 2 |
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|MSB| X | X | X | X | X | X | X | X | X | X |LSB| - | - | - | - |
这意味着对于xy大小的图像,数据长度将为xy*2,是元素数量的两倍。下面是python中的回调函数,这是摄像头的数据入口点
@ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.POINTER(ctypes.c_byte), ctypes.c_ulong)
def receive_frame(_context, data, data_length):
数据
是指向带有图像的内存的指针
data\u length
是指针的大小
理想情况下,我希望使用dtype np.uint_16将数据存储在numpy数组中,但将数组放入numpy会导致我的程序速度变慢,因为处理此信息每帧大约需要0.8s-0.9s,我希望以30fps的速度捕获数据,这意味着我理想情况下需要大约0.03s(我已准备好多处理来分发处理,请参见底部)
我相信有一种更快的方法可以做到这一点,因为在第一个实例中将数据导入numpy需要很长时间
nlist = np.asarray(data[:data_length], dtype='<B')
Takes: ~0.8515s
nlist = np.right_shift(nlist.view(np.uint16), 4).reshape(464, 664)
Takes: ~0.0070s
上面的函数将帧编号和帧添加到multiprocessing.Queue()中,以下多处理池将在其中拾取帧
@staticmethod
def frame_processing_pool_function(centers, points_masks_dict, frame_queue, output_queue):
while True:
queue_item = frame_queue.get()
list = queue_item[1]
nlist = np.asarray(list, dtype='<B')
nlist = np.right_shift(nlist.view(np.uint16), 4).reshape(464, 664)
...
@staticmethod
def帧处理池功能(中心、点屏蔽、帧队列、输出队列):
尽管如此:
queue\u item=frame\u queue.get()
列表=队列\项目[1]
nlist=np.asarray(列表,dtype=)
nlist = np.asarray(data[:data_length], dtype='<B')
Takes: ~0.8515s
nlist = np.right_shift(nlist.view(np.uint16), 4).reshape(464, 664)
Takes: ~0.0070s
@ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.POINTER(ctypes.c_byte), ctypes.c_ulong)
def receive_frame(_context, data, data_length):
global GLOBAL_FRAME_QUEUE, GLOBAL_FRAME_COUNT
GLOBAL_FRAME_QUEUE.put([GLOBAL_FRAME_COUNT, data[:data_length]])
GLOBAL_FRAME_COUNT += 1
@staticmethod
def frame_processing_pool_function(centers, points_masks_dict, frame_queue, output_queue):
while True:
queue_item = frame_queue.get()
list = queue_item[1]
nlist = np.asarray(list, dtype='<B')
nlist = np.right_shift(nlist.view(np.uint16), 4).reshape(464, 664)
...