Accessing Image Data Programmatically
You can access image data using routines designed for general image file access, designed specifically for an image file format, or using unformatted data access routines. Which option you choose depends on the file type and the level of control you want over reading and writing the file. See the following topics for details:
Note
These sections describe how to load data into a variable and includes examples of passing variable data to an iTool programmatically. See Importing Data from the IDL Session (iTool User's Guide) if you want information on how you can access variable data from the iTools Data Manager.
Importing Formatted Image Data Programmatically
The majority of IDL image data access routine require a file specification, indicating the file from which to access the data. The FILEPATH routine is often used within a data access routine as shown in the following example.
Note
To validate that an image file can be accessed using READ_* routines, you can query the image first. See Returning Image File Information for details.
The following example opens a JPEG file from the examples/data directory, performs feature extraction, and displays both images using IIMAGE.
; Open a file and access the data. file = FILEPATH('n_vasinfecta.jpg', $ SUBDIRECTORY = ['examples', 'data']) READ_JPEG, file, image, /GRAYSCALE ; Mask out pixel values greater than 120 ; and create a distance map. binaryImg = image LT 120 distanceImg = MORPH_DISTANCE(binaryImg, NEIGHBOR_SAMPLING = 1) ; Launch iImage, creating a 2 column, 1 row layout. ; Display the original and distanceImg in the two views. IIMAGE, image, VIEW_GRID=[2,1] IIMAGE, distanceImg, /VIEW_NEXT, /OVERPLOT
In the previous example, you could use the READ_IMAGE function instead of the READ_JPEG function by replacing the following statement:
with
In this instance, you do not have control over the color table associated with the image. It is often more useful to use a specific READ_* routine or object designed for the image file format to precisely control characteristics of the imported image.
For a list of available image access, import and export routines and objects, see Image Data Formats under the functional category "Input/Output" (IDL Quick Reference).
Note
IDL can also import images stored in scientific data formats, such as HDF and netCDF. For more information on these formats, see the Scientific Data Formats manual.
Importing Unformatted Image Files
Images in unformatted binary files can be imported with the READ_BINARY function using the DATA_DIMS and DATA_TYPE keywords as follows:
- You must specify the size of the image within the file using the DATA_DIMS keyword. This is required because the READ_BINARY function assumes the data values are arranged in a single vector (a one-dimensional array). The DATA_DIMS keyword is used to specify the size of the two- or three-dimensional image array.
- You can set the DATA_TYPE keyword to the image's data type using the associated IDL type code (see IDL Type Codes and Names under the SIZE function for a complete list of type code). Most images in binary files are of the byte data type, which is the default setting for the DATA_TYPE keyword.
No standard exists by which image parameters are provided in an unformatted binary file. Often, these parameters are not provided at all. In this case, you should already be familiar with the size and type parameters of any images you need to access within binary files.
For example, the worldelv.dat file is a binary file that contains an image. You can only import this image by supplying the information that the data values of the image are byte and that the image has dimensions of 360 pixels by 360 pixels. Before using the READ_BINARY function to access this image, you must first determine the path to the file:
Define the size parameters of the image with a vector:
An image type parameter is not required because we know that the data values of image are byte, which is the default type for the READ_BINARY function.
The READ_BINARY function can now be used to import the image contained in the worldelv.dat file:
Exporting Formatted Image Files Programmatically
Images can be exported to common image file formats using the WRITE_IMAGE procedure. The WRITE_IMAGE procedure requires three inputs: the exported file's name, the image file type, and the image itself. You can also provide the red, green, and blue color components to an associated color table if these components exist.
For example, you can import the image from the worldelv.dat binary file:
file = FILEPATH('worldelv.dat', $
SUBDIRECTORY = ['examples', 'data'])
imageSize = [360, 360]
image = READ_BINARY(file, DATA_DIMS = imageSize)
You can export this image to an image file (a JPEG file) with the WRITE_IMAGE procedure:
IDL also provides format-specific WRITE_* routines that are similar to the WRITE_IMAGE procedure, but provide more flexibility when exporting a specific image file type. See Image Data Formats under the functional category "Input/Output" (IDL Quick Reference) for a list of available image access, import and export routines and objects.
Note
IDL can also export images stored in scientific data formats, such as HDF and netCDF. For more information on these formats, see the Scientific Data Formats manual.
Exporting Unformatted Image Files
Images can be exported to an unformatted binary file with the WRITEU procedure. Before using the WRITEU procedure, you must open a file to which the data will be written using the OPENW procedure. Any file you open must be specifically closed using either the FREE_LUN or CLOSE procedure when you are done exporting the image.
For example, you can import the image from the rose.jpg image file:
You can export this image to a binary file by first opening a new file:
Then, use the WRITEU procedure to write the image to the open file:
You must remember to close the file once the data has been written to it:
Note
For complete details about reading, writing and formatting unformatted data, see Files and Input/Output (Application Programming).