将图像指针传递给python中的DLL函数 [英] Passing an image pointer to a DLL function in python

查看:357
本文介绍了将图像指针传递给python中的DLL函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台相机,它有自己的DLL库,用于将参数传递给相机并拍摄图像.我正在尝试通过python使用此DLL库进行调用.我需要为原始图像数据创建一个指针,但我不确定如何执行此操作. DLL具有一个函数名称,并将这些参数作为输入:

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)

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是:此参数设置原始图像的指针",这就是它们提供的所有详细信息.

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.

如何创建此原始图像指针,然后将其读取到可以在python中使用的2D数组?相机是黑白的,所以我希望得到一个介于0到255之间的2D值数组.

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)

推荐答案

由于您正在使用numpy,因此可以利用

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.

代替ctypes数组

pbyteraw = (c_ubyte * dwBufferSize)()

使用一个numpy数组:

use a numpy array:

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

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

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)

目前尚不清楚缓冲区中底层像素的大小和格式应该是什么.例如,相机是否返回16位灰度像素?大端或小端?您需要确保numpy dtype data_as ctype一致.

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.

这篇关于将图像指针传递给python中的DLL函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆