LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Decoding IMAQ flatten image to string

Solved!
Go to solution

I'm using Labview ZMQ to send image data over to a Python server, which will ultimately reconstruct the image from Labview as a Numpy array. Right now I am flattening the IMAQ images into strings with the included VI and sending the string out over ZMQ. Python receives the string, but I don't know how to decode it.

 

I've set "Type of Flatten" to "Image", but the string ends up being quite a bit longer than the number of bytes in the image, so I know there's some extra metadata included.

 

It doesn't seem like the under-the-hood behavior of IMAQ's Flatten is documented anywhere, and diving into the VI leads to an undocumented C library call. Does anyone know how to decode the output string into an image?

0 Kudos
Message 1 of 6
(3,478 Views)
Solution
Accepted by topic author chr2

Hi chr2,

 

what about converting those IMAQ images to known image formats like PNG, JPEG or BMP - before sending them to Python?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 6
(3,473 Views)

LabVIEW (and IMAQ) use the term "Image" in multiple ways.  I think that in "IMAQ Flatten Image to String", Image refers to all of the internal specifications of the Image, as stored wherever IMAQ stores images, including a lot more "LabVIEW-internal" data than just the Array of Pixels that constitute the "picture" part of the Image.  When you nflatten it, you are transforming it back to LabVIEW's internal representation, which Python might well be unable to process.

 

An alternative would be to use IMAQ functions to extract the Pixel information and to send that across using ordinary TCP/IP, without worrying about flattening/unflattening.  Alternatively, if the image(s) is/are in a known format (.BMP, .AVI, etc.), you could transmit the contents of the File and let Python "take it apart".

 

Bob Schor

0 Kudos
Message 3 of 6
(3,449 Views)

Converting to a known type was the key insight for me. It looks like Flatten is really supposed to be used for LV-to-LV communication.

 

IMAQ has a "Write String" VI that can be set to TIFF, PNG, JPEG, etc. That turned out to be what I want. I have Labview sending the image to a TIFF string, then Python picks up the string, reads it into a BytesIO buffer that tifffile can read and return a numpy array!

Message 4 of 6
(3,441 Views)

Thank you so much chr2!  Using the IMAQ Write String is what solved my problem.  I was originally trying to send a BMP over TCP and was using the flatten to string VI.  I was receiving the correct number of bytes but the data was not correct.  

0 Kudos
Message 5 of 6
(1,431 Views)

@tcotter wrote:

Thank you so much chr2!  Using the IMAQ Write String is what solved my problem.  I was originally trying to send a BMP over TCP and was using the flatten to string VI.  I was receiving the correct number of bytes but the data was not correct.  


Flatten To String flattens all the data in an IMAQ image into a byte-stream. This includes the entire header data as IMAQ Vision itselfs needs to reconstruct the image but this is not an official BMP, JPEG or similar header but IMAQ Vision private. It also includes data such as image overlays that may be present in the image and which are even more IMAQ Vision specific.

 

Only IMAQ Vision itself knows how to unflatten such a byte-stream and that format has, unlike with most other LabVIEW data types, never been documented outside of NI. And it would likely be undoable as the format went through many iterations as IMAQ Vision added new features over the years. With the advantage of an undocumented binary format they did not have to jump through hoops and loops to maintain more than minimal backwards compatibility.

 

In respect to the original posters problem, considering that he wanted to convert the data into a numpy array anyhow on the Python side I do really wonder why he tried to go with some flattened image data format anyhow. It would have been more logical to retrieve the actual pixel array from the IMAQ image through IMAQ (Color) Image To Array, construct a small header that contains the type of pixel format (U8, U16, I16, U32, ..) and the image dimension width and height. Flatten the array and append it to the heder and send it over. On the receiver side interpret the header and turn the array data directly into a numpy array.

Rolf Kalbermatter
My Blog
0 Kudos
Message 6 of 6
(1,414 Views)