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

问题描述 投票:0回答:2

我有一个相机,它有自己的DLL库,用于将参数传递给相机,并拍摄图像。我试图通过python使用此DLL库进行调用。我需要为原始图像数据制作一个指针,我不确定如何做到这一点。 DLL具有函数名称并将这些参数作为输入:

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). 

正如相机文档所述,pbyteraw是:“此参数设置原始图像的指针”,这就是它们提供的所有细节。

我如何创建这个原始图像指针,然后将其读取到我可以在python中使用的2D数组?相机是黑白的,所以我希望得到一个介于0到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)
python pointers dll camera ctypes
2个回答
1
投票

通过一些小的改动,你应该有所作为。设置函数的.argtypes将有助于捕获错误,如果您创建输出参数的实例,则可以通过引用传递它们:

dwNumberOfByteTrans = c_uint32()
dwFrameNo = c_uint32()
mydll.StTrg_TakeRawSnapShot.argtypes = c_void_p,POINTER(c_ubyte),c_uint32,POINTER(c_uint32),POINTER(c_uint32),c_uint32
mydll.StTrg_TakeRawSnapShot(hCamera, pbyteraw, dwBufferSize, byref(dwNumberOfByteTrans), byref(dwFrameNo), dwMilliseconds)

您的原始缓冲区是一维字节数组。您可以通过数学运算来访问正确的行/列,或者使用numpy提供的ctypes接口作为@Ross提供,以提供更直观的界面。


1
投票

由于你正在使用numpy,你可以利用numpy arrays provide a ctypes interface这个事实,它可以让你获得对底层数据缓冲区的引用,这将适合传递给你的DLL函数。只要您愿意将数据保存在一个numpy数组中,请尝试以下操作。

而不是ctypes数组

pbyteraw = (c_ubyte * dwBufferSize)()

使用numpy数组:

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

然后在调用pbyteraw时传递对StTrg_TakeRawSnapShot的引用,如下所示:

mydll.StTrg_TakeRawSnapShot(hCamera,
                            pbyteraw.ctypes.data_as(POINTER(c_int16)),
                            dwBufferSize*2, dwNumberOfByteTrans,
                            dwFrameNo, dwMilliseconds)

目前尚不清楚缓冲区中底层像素的大小和格式应该是多少。例如,相机是否返回16位灰度像素?大端还是小端?你需要确保numpy dtypedata_as ctype一致。

© www.soinside.com 2019 - 2024. All rights reserved.