morefigs / pymba

Python wrapper for Allied Vision's Vimba C API
MIT License
105 stars 84 forks source link

How to read 12 bit mono images to numpy array #41

Closed dboonz closed 5 years ago

dboonz commented 8 years ago

Hi there, I love the work that has been done to make the AVT cameras so much more accessible, thanks!

I would really like to use the api with a 12 bit images as well. The camera that we're using supports Mono12 and Mono12packed bit images as well as Mono8. How do I cast these to a 16 bit numpy array?

I tried (just copied from the example):

...
# set the value of a feature
camera0.AcquisitionMode = 'SingleFrame'
camera0.PixelFormat="Mono12"  
# do acquisition...

moreUsefulImgData = np.ndarray(buffer = frame0.getBufferByteData(),
                               dtype = np.uint16, # changed here
                               shape = (frame0.height,
                                        frame0.width,
                                        1))

But this fails as the data is 12 bit and not 16 bit, so the length is mismatched.

If someone can tell me how, I'd be happy to make a PR with a function that will cast the frame output to a numpy array for any mono datatype.

regards, Dirk

derricw commented 8 years ago

I have had no trouble using "Mono12" because Vimba uses two bytes per pixel which packs into a uint16 array nicely. I do exactly what you're doing and haven't had a problem. Could you let me know the exact error message?

'Mono12Packed' is going to be very difficult in python because you will have to pack the data into a uint8 array, then build a uint16 array by bit-shifting each column, alternating left and right. It might be more trouble than its worth.