Convert ctypes 16 bit c_short array to a 32 bit numpy array

W

Wanderer

I'm using ctypes to have a dll fill a buffer with 16 bit data. I then
want to convert this data to a numpy array. The code snippet below
converts the data from 16 bit to 32 bit, but two 16 bit numbers are
concatenated to make a 32 bit number and half the array is zero.

Buffer = (c_short * byteSize)()
self.cam.Qframe.pBuffer = cast(pointer(Buffer), c_void_p)
perr = self.cam.GrabFrame()
image1 = np.frombuffer(Buffer, int)
xdim = self.cam.Qframe.width
ydim = self.cam.Qframe.height
image2 = image1.reshape(xdim, ydim)

image2 looks like

[[6291555 6357091 6160481 ..., 6488160 6226020 6553697]
[6488163 6422625 6684770 ..., 6422624 6553697 6553696]
[6488160 6357091 6226018 ..., 6815842 6422627 6553696]
...,
[ 0 0 0 ..., 0 0 0]
[ 0 0 0 ..., 0 0 0]
[ 0 0 0 ..., 0 0 0]]

How do convert 16 bit data to 32 bit data?
Thanks
 
W

Wanderer

I'm using ctypes to have a dll fill a buffer with 16 bit data. I then
want to convert this data to a numpy array. The code snippet below
converts the data from 16 bit to 32 bit, but two 16 bit numbers are
concatenated to make a 32 bit number and half the array is zero.

        Buffer = (c_short * byteSize)()
        self.cam.Qframe.pBuffer = cast(pointer(Buffer), c_void_p)
        perr = self.cam.GrabFrame()
        image1 = np.frombuffer(Buffer, int)
        xdim = self.cam.Qframe.width
        ydim = self.cam.Qframe.height
        image2 = image1.reshape(xdim, ydim)

image2 looks like

[[6291555 6357091 6160481 ..., 6488160 6226020 6553697]
 [6488163 6422625 6684770 ..., 6422624 6553697 6553696]
 [6488160 6357091 6226018 ..., 6815842 6422627 6553696]
 ...,
 [      0       0       0 ...,       0       0       0]
 [      0       0       0 ...,       0       0       0]
 [      0       0       0 ...,       0       0       0]]

How do convert 16 bit data to 32 bit data?
Thanks

I figured it out.

Buffer = (c_ubyte * byteSize)()
self.cam.Qframe.pBuffer = cast(pointer(Buffer), c_void_p)
perr = self.cam.GrabFrame()
image1 = np.frombuffer(Buffer, np.uint16)
xdim = self.cam.Qframe.width
ydim = self.cam.Qframe.height
image2 = image1.reshape(xdim, ydim)

Though Eclipse thinks
Buffer = (c_ubyte * byteSize)()

is an error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top