Basic examples of ITK reading and writing

Posted by eelmitchell on Thu, 03 Feb 2022 01:56:37 +0100

The class of reliable reading and writing images is located in Start and end of data processing channel . These classes are considered data sources (readers) And data slot (writers) . In general, they are involved as filters, although readers No input channel, writers There is no output channel.
The itk::ImageFileReader class manages the reading of images, while the itk::ImageFileWriter manages the writing of images. Two classes It is independent of any file format of In fact, the low-level task of reading and writing special file formats consists of a group itk::ImageIO Class executes in the background.
The first step in reading and writing is to include the following header files:
        #include "itkImageFileReader.h"
        #include "itkImageFileWriter.h"
At this time, as before, the pixel type of the representation image processed by the data channel must be determined. Note: when reading and writing an image, the pixel type of the image does not have to be the same as the pixel type stored in the file. Your choice of pixel type mainly has the following two considerations:
• it should be possible to convert the pixel type in the file to the pixel type you choose. This needs to be implemented with standard C language rules, so it must be ensured that the transformation does not lead to information loss.
• the type of pixels in memory should be appropriate for the type of processing you intend to apply to the image.
         Medical image processing is a typical choice Described in the following code:
        typedef unsigned short PixelType;//Unsigned short
        const unsigned int Dimension = 2;//Two dimensional picture
        typedef itk::Image< PixelType, Dimension > ImageType;//Define image type
Note: the dimension of the image in memory should match the dimension of the image in the file. There is a special pair of States, which may not be very strict, but generally can ensure that the two dimensions match.
We now use reader And writer. These two classes are parameterized by image types:
        typedef itk::ImageFileReader< ImageType > ReaderType;
        typedef itk::ImageFileWriter< ImageType > WriterType;
At this time, we use the New() function and assign the result to an itk::SmartPointer to create objects of each type:
        ReaderType::Pointer reader = ReaderType::New( );
        WriterType::Pointer writer = WriterType::New( );
        //The smart pointer (see for details) can now be regarded as an operation on an instance of an object for the time being
SetFileName() is used for the read or write file name Function transfer:
        reader->SetFileName( inputFilename );//Read file name (path)
        writer->SetFileName( outputFilename );//Write file name (path)
We put these readers And writers Connect to the filter to create a data channel. For example, we pass the output of the reader directly to the writer To create a short data channel:
        writer->SetInput( reader->GetOutput( ) );
This may be a very useless program, but it is actually a powerful file format conversion tool. The execution of the data channel is triggered by the Update() of the last object. In this case, the final data channel object is writer . When the channel is executed, the wisest way to protect yourself is to insert it into a try/catch module Update() to throw an exception.
        try
        {
        writer->Update( );
        }
        catch( itk::ExceptionObject & err )
        {
        std::cerr << "ExceptionObject caught !" << std::endl;
        std::cerr << err << std::endl;
        return EXIT_FAILURE;
        }
Note: exceptions can only be caught by code that knows what to do with them. Catch The typical application of the module should exist in the GUI code. The activity of Catch module is to notify the user of the failure of IO operation.
IO of toolbox The architecture makes it possible to avoid the description of file formats used externally to read and write images. Object factory mechanism ImageFileReader and ImageFileWriter Can decide ( At run time ) Which file format will it work in. Typically, the file format is selected based on the file extension, but the architecture supports any complex program that determines whether a file can be read or written. As an option, the user can instantiate and set the appropriate itk::ImageIO externally Subclass to specify the data file format.
//Complete code of ITK official website (English notes deleted version, see examples / Io / ImageReadWrite. Cxx for source code)

#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImage.h"

int main(int argc, char** argv)
{

    typedef short         PixelType;
    const   unsigned int  Dimension = 2;
    typedef itk::Image< PixelType, Dimension >    ImageType;

    typedef itk::ImageFileReader< ImageType >  ReaderType;
    typedef itk::ImageFileWriter< ImageType >  WriterType;

    ReaderType::Pointer reader = ReaderType::New();
    WriterType::Pointer writer = WriterType::New();

    const char* inputFilename = argv[1];
    const char* outputFilename = argv[2];

    reader->SetFileName(inputFilename);
    writer->SetFileName(outputFilename);

    writer->SetInput(reader->GetOutput());

    try
    {
        writer->Update();
    }
    catch (itk::ExceptionObject& err)
    {
        std::cerr << "ExceptionObject caught !" << std::endl;
        std::cerr << err << std::endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

Topics: image processing