ITK/Examples/IO/ImageFileWriter

From KitwarePublic

Jump to: navigation, search

This example demonstrates how to write an itkImage to a file. The file type is determined by the extension of the specified filename.

ImageFileWriter.cxx

#include "itkImage.h"
#include "itkImageFileWriter.h"
 
#include <iostream>
#include <string>
 
int main(int argc, char *argv[])
{
  std::string outputFilename;
  if(argc > 1)
    {
    outputFilename = argv[1];
    }
  else
    {
    outputFilename = "test.png";
    }
 
  typedef unsigned char     PixelType;
  const     unsigned int    Dimension = 2;
  typedef itk::Image< PixelType, Dimension >  ImageType;
 
  ImageType::RegionType region;
  ImageType::IndexType start;
  start[0] = 0;
  start[1] = 0;
 
  ImageType::SizeType size;
  size[0] = 200;
  size[1] = 300;
 
  region.SetSize(size);
  region.SetIndex(start);
 
  ImageType::Pointer image = ImageType::New();
  image->SetRegions(region);
  image->Allocate();
 
  ImageType::IndexType ind;
  ind[0] = 10;
  ind[1] = 10;
 
  typedef  itk::ImageFileWriter< ImageType  > WriterType;
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName(outputFilename);
  writer->SetInput(image);
  writer->Update();
 
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
 
project(ImageFileWriter)
 
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
 
add_executable(ImageFileWriter ImageFileWriter.cxx)
 
if( "${ITK_VERSION_MAJOR}" LESS 4 )
  target_link_libraries(ImageFileWriter ITKReview ${ITK_LIBRARIES})
else( "${ITK_VERSION_MAJOR}" LESS 4 )
  target_link_libraries(ImageFileWriter ${ITK_LIBRARIES})
endif( "${ITK_VERSION_MAJOR}" LESS 4 )
Personal tools