[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]
Image Input and Output |
In this section we'll show you how to import and export an image with VIGRA. If you want to import an image from disk and enquire about its properties, you must use an object of vigra::ImageImportInfo
class. It reads the header of the image file. The constructor expects the file name, the file type will be determined automatically.
The vigra::ImageImportInfo
class currently recognizes the following file formats:
In the following example, the image file name is given in the first command line argument, and the most important image metadata are printed:
As you can see, the ImageImportInfo
object contains a lot of information, some of it is printed in the example. Using this image
we get the following output:
Image information: file format: GIF width: 154 height: 145 pixel type: UINT8 color image: no (number of channels: 1)
To process the image, we must load the actual image data into an array such as the one described in Basic MultiArray Usage. To do so, we create a vigra::MultiArray
with the appropriate shape and then call vigra::importImage(). This function needs an ImageImportInfo
object specifying the image to be loaded and a MultiArrayView object of apropriate size to copy the image data in. The code looks like this:
If you already know the type of data in the file, you can also just pass the filename and a MultiArray, which will automatically be resized as appropariate:
Writing the image data from an array to a file is quite similar. For this purpose, you use the function vigra::exportImage(), which takes an 2D MultiArrayView and an vigra::ImageExportInfo object or a string (the filename). The ImageExportInfo object also needs a file name, but gives you more control over how the image is written. The desired file format is guessed from the file name's extension (but can be overridden with the method ImageExportInfo::setFileType
. Recognized extensions are: '.bmp', '.exr', '.gif', '.jpeg', '.jpg', '.p7', '.png', '.pbm', '.pgm', '.pnm', '.ppm', '.ras', '.tif', '.tiff', '.xv', '.hdr' (as for reading, '.exr' requires libopenexr, '.jpg' requires libjpeg, '.png' requires libpng and '.tif' requires libtiff). In the following example, we create and save a 160x160 pixels image, where the image is a checkerboard. The image is saved as "testimage.gif" in the same folder as the executed code.
The resulting images are the following:
Finally, we give a complete example of importing, editing and exporting an image. After importing, we set every other horizontal line to black. This can be done with the bind<M>(i)
method explained in bind<M>(i) and bindAt(M, i). Input and output file names are specified via command line arguments.
The input image and the resulting output image are:
The handling of color images is exactly the same, but instead of instantiating a vigra::MultiArray<2, UInt8>
you need a vigra::MultiArray<2, vigra::RGBValue<UInt8> >
array as described in Basic MultiArray Usage. Images with alpha channel are supported by importImageAlpha() and exportImageAlpha().
Note that image processing often requires more complicated calculations than in these examples. In this case, it is better to import and convert the data into a float
array (i.e. vigra::MultiArray<2, float>
) instead of the simple unsigned char
type in order to minimize rounding errors. When a file is imported into such an array, the conversion is automatically performed by the importImage() function. When an array is to be exported, the handling of float
depends on the file format: If the file format supports float (currently: TIFF and VIFF), the data are written verbatim (unless this is explicitly overridden, see below). Otherwise, the data are mapped to unsigned char
via a linear transform of the orginal range, followed by rounding (use vigra::linearRangeMapping() to override this behavior by an explicit user-defined mapping).
The ImageExportInfo
class provides a number of additional methods to customize data export, including:
See vigra::ImageExportInfo for a complete list and more details.
The recommended file format for arrays of arbitrary dimension is HDF5. It supports all possible pixel types, arbitrary dimensions, on-the-fly compression, arbitrary many arrays per file, and flexible metadata storage along with arrays. See Import/Export of Images and Arrays in HDF5 Format for more information.
The functions importVolume() and exportVolume() support three additional methods to read and write 3D volume data:
© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de) |
html generated using doxygen and Python
|