imagecomputer-visionlabview

Create a LabVIEW IMAQ image from a binary buffer/file with and without NI Vision


Assume you have a binary buffer or file which represents a 2-dimensional image.

How can you convert the binary data into a IMAQ image for further processing using LabVIEW?


Solution

  • With NI Vision

    For LabVIEW users who have the NI vision library installed, there are VIs that allow for the image data of an IMAQ image to be copied from a 2D array.

    For single-channel images (U8, U16, I16, float) the VI is

    Vision and Motion >> Vision Utilites >> Pixel Manipulation >> IMAQ ArrayToImage.vi

    For multichannel images (RGB etc) the VI is

    Vision and Motion >> Vision Utilites >> Color Utilities >> IMAQ ArrayColorToImage.vi

    Example 1

    An example of using the IMAQ ArrayToImage.vi is shown in the snippet below where U16 data is read from a binary file and written to a Greyscale U16 type IMAQ image. Please note, if the file has been created by other software than LabVIEW then it is likely that it will have to be read in little-endian format which is specified for the Read From Binary File.vi

    LabVIEW Code to set the IMAQ Image data from a data buffer with NI Vision

    Example 2

    A similar process can be used when some driver DLL call is used to get the image data as a buffer. For example, if the driver has a function capture(unsigned short * buffer) then the following technique could be employed where a correctly sized array is initialized before the function call using the initialize array primitive.

    LabVIEW Code to set the IMAQ Image data from library call with NI Vision

    // example function which fills a buffer with image data
    
    #include <stdint.h>
    
    __declspec(dllexport) int capture(uint16_t * buffer) 
    {
      int width,height;
      width = 2500;
      height = 3052;
    
    
      // check pointer
      if(!buffer){
        return -1;
      }
    
      // fill buffer with some data for testing
    
      // this should be a greyscale gradient 
      // black in the top left corner
      // to white in the bottom left
    
      for(int row = 0; row<height;row++){
        for(int pixel=0; pixel<width; pixel++){
          *buffer = row * 8 + pixel * 8;
          buffer++;
        }
      }
    
      return 0;
    }
    

    Without NI Vision

    For LabVIEW users who do not have NI vision installed, we can use a VI called GetImagePixelPtr.vi which is installed alongside the NI-IMAQ toolkit/library. This VI may not be visible in the palettes but should be on disk in <LabVIEW-Install-Directory>\vi.lib\vision\Basics.llb.

    In addition, we will use the MoveBlock shared-library call from LabVIEW's memory manager library

    These VI/library calls can be used as shown in the snippet below where, as in the previous snippet, U16 data is read from a binary file and written to a Greyscale U16 type IMAQ image.

    LabVIEW Code to set the IMAQ Image data from a data buffer without NI Vision

    Once we have the image data as a 2D array we need to prepare the IMAQ image by setting its dimensions. A for-loop is then used to iterate over the rows of the image data; for each row, we obtain a pointer to the start of the corresponding IMAQ Image row and use the MoveBlock call to copy the data across. After each MoveBlock call, we unmap the IMAQ image pointer to tidy up.

    Please note, this example used U16 data; For other data types ensure that the bytes per pixels numeric constant (in the for-loop) is updated accordingly.