python dll 指针,将图像指针传递给python中的DLL函数

这篇博客讨论了如何通过Python调用DLL库来获取相机的原始图像数据,并将其转化为2D numpy数组。作者首先介绍了DLL库中的函数参数,然后展示了如何创建numpy数组作为数据缓冲区,并利用numpy的内存视图功能将缓冲区传递给DLL函数。最后,解释了如何正确设置numpy数组的数据类型以匹配DLL库的预期格式。
摘要由CSDN通过智能技术生成

I have a camera that has its own DLL library for passing parameters to the camera, and taking images. I am trying to make calls with this DLL library through python. I need to make a pointer for the raw image data, and i'm not exactly sure how to do this. The DLL has a function name and takes those parameters as inputs:

StTrg_TakeRawSnapShot(hCamera, pbyteraw, dwBufferSize, dwNumberOfByteTrans, dwFrameNo, dwMilliseconds)

hCamera:

This parameter sets the camera control handle that obtains by StTrg_Open.

pbyteRaw:

This parameter sets pointer of the raw image.

dwBufferSize:

This parameter sets the buffer size.

pdwNumberOfByteTrans:

This parameter sets the pointer that obtains the total number of the bytes of the image.

pdwFrameNo:

This parameter sets the pointer that obtains the frame number of the image that counts in the camera.

This number can be use for the frame drop detection.

dwMilliseconds:

This parameter sets the timeout time (Unit is msecond).

exactly as stated from the documentation of the camera, pbyteraw is: "This parameter sets pointer of the raw image" and that's all the detail they provide.

how do i create this raw image pointer, and then read it to a 2D array that i can work with in python? The camera is black and white, so i am hoping to get a 2D array of values between 0 and 255.

from ctypes import *

import numpy as np

mydll = windll.LoadLibrary('StTrgApi.dll')

hCamera = mydll.StTrg_Open()

im_height = 1600

im_width = 1200

dwBufferSize = im_height * im_width

pbyteraw = (c_ubyte * dwBufferSize)()

dwNumberOfByteTrans = 0

dwFrameNo = 0

dwMilliseconds = 3000

mydll.StTrg_TakeRawSnapShot(hCamera, pbyteraw, dwBufferSize,

dwNumberOfByteTrans, dwFrameNo, dwMilliseconds)

b_pbyte = bytearray(pbyteraw)

解决方案

Since you're working with numpy you can take advantage of the fact that numpy arrays provide a ctypes interface that let you get a reference to the underlying data buffer, which would be suitable to pass to your DLL's function. As long as you're happy keeping your data in a numpy array, try something like the following.

Instead of a ctypes array

pbyteraw = (c_ubyte * dwBufferSize)()

use a numpy array:

pbyteraw = np.zeros((im_height, im_width), dtype=np.uint16)

Then pass a reference to pbyteraw when calling StTrg_TakeRawSnapShot, as follows:

mydll.StTrg_TakeRawSnapShot(hCamera,

pbyteraw.ctypes.data_as(POINTER(c_int16)),

dwBufferSize*2, dwNumberOfByteTrans,

dwFrameNo, dwMilliseconds)

It's not clear what the size and format of the underlying pixels in the buffer are supposed to be though. For example, does the camera return 16-bit grayscale pixels? Big or little endian? You'll need to make sure that the numpy dtype agrees with the data_as ctype.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值