Two, ITK example of the image reader -jpg

A, ITK literacy works

  In ITK inside, we need to set the pixel type, image type reading of the image.

  Then set the read pointer, the read parameter.

  At the same time set the write pointer, will be written to the file parameter.

  In order to achieve read and write actions, we need to construct a factory IO (IOFactory), used to implement the action to read and write.

  Also need an action, used to trigger content set above, makes everything work.

 

  It's like a machine, we do not know how the inside of the machine is running (factory working mechanism), but they know we can set the parameters, then click on the start switch,

This time the machine will be able to function properly, to the effect we want. (The meaning of this is actually where API).

 

Second, the following is the explanation for the mechanism to read and write

  

 

 

   First we set up the right side ImageFileWriter and ImageFileReader, tells the function we need to operate the destination file name.

  Then factory created ImageIO.

  By determining ImageIOFactory inside which a worker can read and write to this file.

  For example, this time we find the PNGImageIOFactory (determined workers) can read and write to the target file, a PNGImageIOFactory created in memory, as ImageIOFactory operator, put the data entrusted to it to operate, completion of the corresponding read and write operations.

  Work is complete.

  This mechanism is equivalent to the same supply and demand platform:

  A person (wirter or reader pointer) to their needs, such as repair electrical appliances released, then people have the ability to repair electrical orders (to complete write or read), then register (register), name of the company (ImageIOFactory) completed appropriate action to end the work.

  Some ImageIO factory as follows:

  

 

 

 Third, examples

  Specifically engineered method, see a blog

  code show as below:

#include "itkRGBPixel.h"
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkJPEGImageIOFactory.h"
int main(void)
{
    // here when the reading is in the form RGBPixel read, if modified as follows, based on the grayscale read
  
// using PixelType = itk :: RGBPixel < unsigned char>;
    using PixelType=unsigned char;
    using ImageType=itk::Image<PixelType, 2>;

    using ReaderType =itk::ImageFileReader<ImageType>;
    using WriterType=itk::ImageFileWriter<ImageType>;
    ReaderType::Pointer reader = ReaderType::New();
    :: Pointer Writer WriterType = WriterType :: New ();
     // if this is written, then there is a need to define 
    itk :: JPEGImageIOFactory :: RegisterOneFactory ();

    reader->SetFileName("1.jpg");
    writer->SetFileName("2.jpg");
    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;
}

Fourth, pay attention

  4.1 appropriate use of the function to add the header file    

  When the update operation triggered 4.2, preferably using the following code:

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

  3, read the file located in the project directory, after running the program, we got two .jpg files

Fifth, reference documents

  InsightSoftwareGuide-Book2-5.0.1.pdf

Guess you like

Origin www.cnblogs.com/fantianliang/p/11964899.html