Chapter 15: Saving and Reading Images – Practical Guide to Machine Vision Software: An Introduction with LabVIEW

Saving and Reading Images

15.1 Saving Image

You can save images using IMAQ Write File as seen in Figure 15.1, which is found from the function palette in Vision and Motion»Vision Utilities»Files.

Figure 15.1 IMAQ Write File.

As seen in Figure 15.2, various kinds of image formats such as BMP, JPEG, PNG, and TIFF can be selected to save image file using the IMAQ Write File function.

Figure 15.2 Image selection.

The available types of images are summarized in Table 15.1.

Table 15.1 Image type.

BMP (BitMaP image) JPEG (Joint Photographic Coding Experts Group) PNG (Portable Network Graphics) TIFF (Tagged Image File Format)
Image from camera is bitmap image, which has the image values on each pixel location. The bitmap image is not compressed JPEG is one of the image compressing methods. The color image can be effectively compressed by using JPEG. Here, the quality and the size of the image can be adjusted PNG has been widely used in the Web to realize bitmap image The data information of image was saved in the form of Tag in front of the image. From this, image type can be recognized

When using IMAQ Write File, you can specify the Compression Ratio or Image Quality, as seen in Figure 15.3 (IMAQ Write file for JPEG) and Figure 15.4 (IMAQ Write file for JPEG2000).

Figure 15.3 IMAQ Write File (JPG save).

Figure 15.4 IMAQ Write File (JPG2000 save).

For example, the default value for Image Quality is 750 in case of saving a JPG file. The value for Image Quality ranges from 1 to 1000 depending on the image quality required. Note that even if the quality value of 1000 is used for saving jpg file, there might be some amount of image data loss due to compression. In case of JPEG2000, Compression Ratio is used to define image quality. If the compression ratio is 50, the size of the file can be reduced 50 times smaller.

Figure 15.5 shows a block diagram of a VI, which can save acquired images twice a second to a designated folder.

Figure 15.5 IMAQ image save (BMP) of acquired image.

Figure 15.5 shows the SubVI to create a folder and specify a path to save the acquired image. At Figure 15.5 , a programmatically created file name for an image is added to the specified folder path. The file names for the images will be imagxx.bmp, where xx indicates numeric characters in sequential order from the While Loop increment. Note that we have added the file extension bmp since image file type to be saved is in Microsoft bitmap format. As specified in the block diagram, the wait function at is used to save the acquired image twice a second (every 500 ms).

SubVI for Creating a Folder

In this section, the SubVI (Figure 15.5 ) for creating folder will be discussed. You may skip this section if you are familiar with creating folders. Figure 15.6 shows the block diagram of the SubVI used in Figure 15.5 . As seen in Figure 15.6, the output of the SubVI is the path information of created path.

Figure 15.6 Block diagram of SubVI to create a folder.

To save the acquired images, the folder name of Save is created as a subfolder of the current VI's path. For this purpose, Current VI's Path (Figure 15.7) is used, which can be found in Programming»File I/O»File constants function palette.

Figure 15.7 Current VI's path (Figure 15.6 ).

The Strip Path function in Figure 15.8 separates the directory path from file name. In this way, only path information for the folder, where current VI exists, is obtained.

Figure 15.8 Strip path (Figure 15.6 ).

The subfolder name Save is added to the path using the Build Path function seen in Figure 15.9. The final result is a file path to a folder where image files will be saved.

Figure 15.9 Subfolder generation from current folder (Figure 15.6 ).

The path information for the folder is connected to Check if File or Folder function in Figure 15.10 to see if the folder already exists.

Figure 15.10 Check if a file or Folder (Programming»File I.O»Advanced File Functions).

The Boolean output of File or Folder Exists? is used to check the existence of the folder. In case the folder already exists, the folder including all the files contained in the folder is deleted by using Delete function (Programming»File I.O»Advanced File Functions). Then a folder with the same name is created using the Open/Create/Replace (Programming»FileI.O» Advanced File Functions), as seen in Figure 15.11.

Figure 15.11 Delete the existing folder and create a new folder.

If there is no folder by that name, a new folder is created, as seen in Figure 15.12.

Figure 15.12 Folder creation.

The created path for the folder is used as the output of the SubVI in Figure 15.5 .

15.2 Image Read from File

15.2.1 IMAQ Readfile

In Chapter 2, the Vision Acquisition Express was used to read images from files. In this section, methods to read image files using IMAQ Readfile are discussed. The method using IMAQ Readfile has advantages because it is more effective method in building your own VI to meet your requirements.

The use of IMAQ Readfile to read image file is simple. As a first step, IMAQ Create function should be used to allocate memory space for the image to be read in. Then, the IMAQ Readfile function is used to read an image file. Figure 15.13 shows the typical block diagram of VI to read an image file.

Figure 15.13 Block diagram for image read.

IMAQ Readfile function (Figure 15.14) can be found in Vision and Motion Vision Utilities Files.

Figure 15.14 Image Read function.

15.2.2 Example of Reading Image from Image Files

Figure 15.15 shows an example of a block diagram to read image files in sequential order and show the images in the Image display on the front panel. By using the VI shown in Figure 15.15, the sequential images will be displayed with the same time interval as the images were acquired and saved. As a result, the images appear on the display window as if the images were being acquired in real time.

Figure 15.15 Block diagram for VI to read image files.

To read image files from the image folder, the folder is scanned of its contents to obtain the number of saved image files in the folder. Then, the number of files is used for the number of iterations in the FOR loop. The Recursive File List function in Figure 15.16 is used to analyze the contents of the folder.

Figure 15.16 Recursive File

Recursive File can be found in Programming»File I/O»Advanced File Functions. The file names of imagexx.bmp (xx = 1,2,…), which were file names previously saved using block diagram in Figure 15.5, are created in sequential order by using the index of FOR loop (Figure 15.5). In this VI, the saved image files in Figure 15.5 can be read in using the Image Read function in Figure 15.15 .

Exercise 15.1: Image save

From the continuous acquired images, you can drag out a ROI and save the image portion as defined via the ROI. As a result, the size of image to be saved will be reduced accordingly (Figure 15.17).

Figure 15.17 Saving a part of an image defined by a ROI.


You can use IMAQ Extract Tetragon function (Vision»Vision Utilities» Image Manipulation) to reduce the size of the image by using ROI information (Figure 15.18).

Figure 15.18 IMAQ Extract Tetragon.