[Insight-users] Reading/writing images and CMake

John Drozd john.drozd at gmail.com
Fri Nov 26 15:00:30 EST 2010


Hi,

Did you copy the png images to the same directory as your executable
program?
(otherwise you may need to specify the path)

I revised your code as attached and it worked for me on linux.
(I used the attached CMakeLists.txt file that specifies the
include_directories where the libraries are on my system,
it includes more directories because I use this CMakeLists.txt file's
include_directories on all my compiliations)
Unfortunately, I have not figured out how to get CMake to automatically find
the include_directories and ITK_DIR. I enter the ITK_DIR each time I first
compile the program.

John

On Fri, Nov 26, 2010 at 1:57 PM, Brecht Heyde
<Brecht.Heyde at med.kuleuven.be>wrote:

>  Hi all,
>
>
>
> I am fairly new to ITK and have a couple of questions:
>
>
>
> 1. When creating a new ITK program, do I always have to go through CMake to
> create (in my case) a .sln Visual Studio Solution? Or do I otherwise have to
> specify all the additional directories + libraries for the linker manually?
>
>
>
> 2. I tried adapting the ImageReadWrite.cxx slightly in order to read the
> BrainMidSagittalSlice.png found in the ITK/Examples/Data directory.
> My source code is:
>
> #include "itkImageFileReader.h"
>
> #include "itkImageFileWriter.h"
>
>
>
> #include "itkImage.h"
>
>
>
>
>
> int main()
>
> {
>
>   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  = "BrainMidSagittalSlice.png";
>
>   const char * outputFilename = "BrainMidSagittalSlice_output.png";
>
>
>
>   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;
>
> }
>
>
>
> With CmakeList.txt:
>
>
>
>                 CMAKE_MINIMUM_REQUIRED(VERSION 2.4)
>
> IF(COMMAND CMAKE_POLICY)
>
>   CMAKE_POLICY(SET CMP0003 NEW)
>
> ENDIF(COMMAND CMAKE_POLICY)
>
>
>
>
>
> # This project is designed to be built outside the Insight source tree.
>
> PROJECT(ImageReadWrite)
>
>
>
> # Find ITK.
>
> FIND_PACKAGE(ITK REQUIRED)
>
> INCLUDE(${ITK_USE_FILE})
>
>
>
> ADD_EXECUTABLE(ImageReadWrite ImageReadWrite.cxx)
>
>
>
> TARGET_LINK_LIBRARIES(ImageReadWrite ITKIO)
>
>
>
> When running the resulting executable from a cmd prompt as
> ‘ImageReadWrite’, I get the following error:
>
>
>
> C:\ITK-dev\ImageReadWrite\debug>ImageReadWrite
>
> ExceptionObject caught !
>
>
>
> itk::ImageFileReaderException (0133F0CC)
>
> Location: "void __thiscall itk::ImageFileReader<class
> itk::Image<short,2>,class
>
> itk::DefaultConvertPixelTraits<short> >::GenerateOutputInformation(void)"
>
> File:
> c:\itk-src\insighttoolkit-3.20.0\insighttoolkit-3.20.0\code\io\itkImageFileReader.txx
>
> Line: 146
>
> Description:  Could not create IO object for file BrainMidSagittalSlice.png
>
>   Tried to create one of the following:
>
>     BioRadImageIO
>
>     GDCMImageIO
>
>     MetaImageIO
>
>     PNGImageIO
>
>     VTKImageIO
>
>     GiplImageIO
>
>     LSMImageIO
>
>     AnalyzeImageIO
>
>     NiftiImageIO
>
>     StimulateImageIO
>
>     JPEGImageIO
>
>     TIFFImageIO
>
>     NrrdImageIO
>
>     BMPImageIO
>
>     DICOMImageIO2
>
>   You probably failed to set a file suffix, or  set the suffix to an
> unsupported type.
>
>
>
> As I understood from previous mails on this list, the IO object decides
> automatically to what format it should write. What am I doing wrong?
>
>
>
> 3. I’d like to read a 3D+time dataset in ITK. The original dataset is in
> dicom format but I have previously extracted the relevant data and converted
> to a .mat format for further processing in Matlab (in practice this is just
> one big 4D matrix). Furthermore, we have developed a visualisation tool to
> manually segment such a dataset and export a mesh (cloud of points with
> connectivity matrix) containing this manual contouring.
>
>
>
> I was wondering what format I could use to import such a series of 3D
> matrices and import this cloud of points. I doubt that a dicomreader would
> work as the image data is stored in a private datastructure in the
> dicomformat.
>
>
>
> 4. I’d like to register this set of 3D images and know how my cloud of
> points are deformed by the registration algorithm. Is this possible in ITK?
> Likewise, would it be possible to calculate how an overlying grid deforms
> due to this registration? Finally, I would like to export the positions of
> this deformed set of points (and preferably the deformed floating images).
> Can I do this in a Matlab compatible format or do I have to write a mex
> wrapper for this?
>
>
>
> Many thanks,
>
>
>
> Best regards,
>
> Brecht
>
>
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.html
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>
>


-- 
John Jan Drozd, Ph.D.
Post-Doctoral Fellow
Imaging Research Laboratories
Robarts Research Institute
The University of Western Ontario
London, ON, Canada
http://imaging.robarts.ca/~jdrozd <http://imaging.robarts.ca/%7Ejdrozd>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20101126/5d19c5fe/attachment.htm>
-------------- next part --------------
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: ImageReadWrite.cxx,v $
  Language:  C++
  Date:      $Date: 2009-03-17 20:36:50 $
  Version:   $Revision: 1.20 $

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

#ifdef __BORLANDC__
#define ITK_LEAN_AND_MEAN
#endif

//  Software Guide : BeginLatex
//
//  The classes responsible for reading and writing images are located at the
//  beginning and end of the data processing pipeline. These classes are
//  known as data sources (readers) and data sinks (writers).
//  Generally speaking they are referred to as filters, although readers have
//  no pipeline input and writers have no pipeline output.
//
//  The reading of images is managed by the class \doxygen{ImageFileReader}
//  while writing is performed by the class \doxygen{ImageFileWriter}. These
//  two classes are independent of any particular file format. The actual low
//  level task of reading and writing specific file formats is done behind
//  the scenes by a family of classes of type \doxygen{ImageIO}.
//
//  The first step for performing reading and writing is to include the
//  following headers.
//
//  \index{itk::ImageFileReader|textbf}
//  \index{itk::ImageFileReader!header}
//
//  \index{itk::ImageFileWriter|textbf}
//  \index{itk::ImageFileWriter!header}
//
//  Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
// Software Guide : EndCodeSnippet


#include "itkImage.h"


int main( int argc, char ** argv )
{
  // Verify the number of parameters in the command line
  if( argc < 1 )
    {
    std::cerr << "Usage: " << std::endl;
    //std::cerr << argv[0] << " inputImageFile  outputImageFile " << std::endl;
    std::cerr << argv[0] << std::endl;
    return EXIT_FAILURE;
    }


  //  Software Guide : BeginLatex
  //
  //  Then, as usual, a decision must be made about the type of pixel used to
  //  represent the image processed by the pipeline. Note that when reading
  //  and writing images, the pixel type of the image \textbf{is not
  //  necessarily} the same as the pixel type stored in the file.  Your
  //  choice of the pixel type (and hence template parameter) should be
  //  driven mainly by two considerations:
  //
  //  \begin{itemize}
  //  \item It should be possible to cast the file pixel type in the file to
  //  the pixel type you select. This casting will be performed using the
  //  standard C-language rules, so you will have to make sure that the
  //  conversion does not result in information being lost.
  //  \item The pixel type in memory should be appropriate to the type of
  //  processing you intended to apply on the images. 
  //  \end{itemize}
  //
  //  A typical selection for medical images is illustrated in
  //  the following lines.
  //
  //  Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet

  //PNG supports unsigned char and unsigned short
  //typedef short      PixelType;
  typedef unsigned short      PixelType;
  const   unsigned int        Dimension = 2;
  typedef itk::Image< PixelType, Dimension >    ImageType;
  // Software Guide : EndCodeSnippet


  //  Software Guide : BeginLatex
  //
  //  Note that the dimension of the image in memory should match the one of
  //  the image in file. There are a couple of special cases in which this
  //  condition may be relaxed, but in general it is better to ensure that both
  //  dimensions match.
  //
  //  We can now instantiate the types of the reader and writer. These two
  //  classes are parameterized over the image type.
  //
  //  \index{itk::ImageFileReader!Instantiation}
  //  \index{itk::ImageFileWriter!Instantiation}
  //
  //  Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
  typedef itk::ImageFileReader< ImageType >  ReaderType;
  typedef itk::ImageFileWriter< ImageType >  WriterType;
  // Software Guide : EndCodeSnippet


  //  Software Guide : BeginLatex
  //
  //  Then, we create one object of each type using the New() method and
  //  assigning the result to a \doxygen{SmartPointer}.
  //
  //  \index{itk::ImageFileReader!New()}
  //  \index{itk::ImageFileWriter!New()}
  //  \index{itk::ImageFileReader!SmartPointer}
  //  \index{itk::ImageFileWriter!SmartPointer}
  //
  //  Software Guide : EndLatex 

  // Software Guide : BeginCodeSnippet
  ReaderType::Pointer reader = ReaderType::New();
  WriterType::Pointer writer = WriterType::New();
  // Software Guide : EndCodeSnippet


  // Here we recover the file names from the command line arguments
  //
  /*
  const char * inputFilename  = argv[1];
  const char * outputFilename = argv[2];
  */

  //need to specify the path
  //const char * inputFilename  = "BrainMidSagittalSlice.png";
  const char * inputFilename  = "/trumpet/downloads/3DSlicerDec22009/Slicer3-lib/Insight/Examples/Data/BrainMidSagittalSlice.png";

  const char * outputFilename = "BrainMidSagittalSlice_output.png";


  //  Software Guide : BeginLatex
  //
  //  The name of the file to be read or written is passed with the
  //  SetFileName() method. 
  //
  //  \index{itk::ImageFileReader!SetFileName()}
  //  \index{itk::ImageFileWriter!SetFileName()}
  //  \index{SetFileName()!itk::ImageFileReader}
  //  \index{SetFileName()!itk::ImageFileWriter}
  //
  //  Software Guide : EndLatex 

  // Software Guide : BeginCodeSnippet
  reader->SetFileName( inputFilename  );
  writer->SetFileName( outputFilename );
  // Software Guide : EndCodeSnippet


  //  Software Guide : BeginLatex
  //
  //  We can now connect these readers and writers to filters to create a
  //  pipeline. For example, we can create a short pipeline by passing
  //  the output of the reader directly to the input of the writer.
  //
  //  Software Guide : EndLatex 

  // Software Guide : BeginCodeSnippet
  writer->SetInput( reader->GetOutput() );
  // Software Guide : EndCodeSnippet


  //  Software Guide : BeginLatex
  //
  //  At first view, this may seem as a quite useless program, but it is
  //  actually implementing a powerful file format conversion tool! The
  //  execution of the pipeline is triggered by the invocation of the
  //  \code{Update()} methods in one of the final objects. In this case, the final
  //  data pipeline object is the writer. It is a wise practice of defensive
  //  programming to insert any \code{Update()} call inside a \code{try/catch} block
  //  in case exceptions are thrown during the execution of the pipeline.
  //
  //  Software Guide : EndLatex 


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


  //  Software Guide : BeginLatex
  //
  //  Note that exceptions should only be caught by pieces of code that know
  //  what to do with them. In a typical application this \code{catch} block
  //  should probably reside on the GUI code. The action on the \code{catch}
  //  block could inform the user about the failure of the IO operation.
  //
  //  The IO architecture of the toolkit makes it possible to avoid explicit
  //  specification of the file format used to read or write
  //  images.\footnote{In this example no file format is specified; this
  //  program can be used as a general file conversion utility.}  The object
  //  factory mechanism enables the ImageFileReader and ImageFileWriter to
  //  determine (at run-time) with which file format it is working
  //  with. Typically, file formats are chosen based on the filename
  //  extension, but the architecture supports arbitrarily complex processes
  //  to determine whether a file can be read or written. Alternatively, the
  //  user can specify the data file format by explicit instantiation and
  //  assignment the appropriate \doxygen{ImageIO} subclass.
  //
  //  For historical reasons and as a convenience to the user, the
  //  \doxygen{ImageFileWriter} also has a Write() method that is aliased to
  //  the \code{Update()} method. You can in principle use either of them but
  //  \code{Update()} is recommended since Write() may be deprecated in the future.
  //
  //  Software Guide : EndLatex 

  return EXIT_SUCCESS;
}
-------------- next part --------------
PROJECT(ImageReadWrite)
INCLUDE_REGULAR_EXPRESSION("^.*$")

# Find ITK.
FIND_PACKAGE(ITK REQUIRED)
#IF(ITK_FOUND)
#  INCLUDE(${ITK_USE_FILE})
#ENDIF(ITK_FOUND)

SET(BASELINE ${ITK_DATA_ROOT}/Baseline/Registration)
SET(TEMP ${ITK_BINARY_DIR}/Testing/Temporary)

SET(REGISTRATION_EXAMPLES ${CXX_TEST_PATH}/RegistrationExamples)
SET(REGISTRATION_EXAMPLESO ${CXX_TEST_PATH}/RegistrationExamplesO)
SET(REGISTRATION_EXAMPLESO2 ${CXX_TEST_PATH}/RegistrationExamplesO2)
SET(REGISTRATION_EXAMPLES2 ${CXX_TEST_PATH}/RegistrationExamples2)
SET(REGISTRATION_EXAMPLES3 ${CXX_TEST_PATH}/RegistrationExamples3)
SET(REGISTRATION_EXAMPLES4 ${CXX_TEST_PATH}/RegistrationExamples4)
SET(REGISTRATION_EXAMPLES5 ${CXX_TEST_PATH}/RegistrationExamples5)
SET(REGISTRATION_EXAMPLES6 ${CXX_TEST_PATH}/RegistrationExamples6)
SET(REGISTRATION_EXAMPLES7 ${CXX_TEST_PATH}/RegistrationExamples7)
SET(REGISTRATION_EXAMPLES8 ${CXX_TEST_PATH}/RegistrationExamples8)
SET(REGISTRATION_EXAMPLES9 ${CXX_TEST_PATH}/RegistrationExamples9)
SET(REGISTRATION_EXAMPLES10 ${CXX_TEST_PATH}/RegistrationExamples10)
SET(REGISTRATION_EXAMPLES11 ${CXX_TEST_PATH}/RegistrationExamples11)
SET(REGISTRATION_EXAMPLES12 ${CXX_TEST_PATH}/RegistrationExamples12)
SET(REGISTRATION_EXAMPLES13 ${CXX_TEST_PATH}/RegistrationExamples13)
SET(REGISTRATION_EXAMPLES14 ${CXX_TEST_PATH}/RegistrationExamples14)

include_directories (/trumpet/downloads/3DSlicer/Slicer3-lib/Insight/Code/IO
/trumpet/downloads/3DSlicer/Slicer3-lib/Insight/Code/Common 
/trumpet/downloads/3DSlicer/Slicer3-lib/Insight-build
/trumpet/downloads/3DSlicer/Slicer3-lib/Insight/Utilities/vxl/vcl
/trumpet/downloads/3DSlicer/Slicer3-lib/Insight-build/Utilities/vxl/vcl
/trumpet/downloads/3DSlicer/Slicer3-lib/Insight/Utilities/vxl/core
/trumpet/downloads/3DSlicer/Slicer3-lib/Insight-build/Utilities/vxl/core
/trumpet/downloads/3DSlicer/Slicer3-lib/Insight-build/Utilities
/trumpet/downloads/3DSlicer/Slicer3-lib/Insight/Code/BasicFilters
/trumpet/downloads/3DSlicer/Slicer3-lib/Insight/Code/Review
/trumpet/downloads/3DSlicer/Slicer3-lib/Insight/Code/Algorithms
/trumpet/downloads/3DSlicer/Slicer3-lib/Insight/Code/Numerics/Statistics
/trumpet/downloads/3DSlicer/Slicer3-lib/Insight/Code/Numerics/FEM
/trumpet/downloads/3DSlicer/Slicer3-lib/Insight/Code/Numerics
/trumpet/downloads/3DSlicer/Slicer3-lib/Insight/Code/SpatialObject
/trumpet/downloads/3DSlicer/Slicer3-lib/Insight/Utilities/MetaIO
/trumpet/downloads/3DSlicer/Slicer3-lib/Insight/Utilities
/trumpet/downloads/3DSlicer/Slicer3-lib/Insight/Utilities/gdcm/src
/trumpet/downloads/3DSlicer/Slicer3-lib/Insight-build/Utilities/gdcm
/trumpet/downloads/3DSlicer/Slicer3-lib/Insight/Testing/Code/BasicFilters)

link_directories (/trumpet/downloads/3DSlicer/Slicer3-lib/Insight-build/bin)

ADD_EXECUTABLE(ImageReadWrite ImageReadWrite.cxx )
TARGET_LINK_LIBRARIES(ImageReadWrite ITKIO ITKNumerics)








More information about the Insight-users mailing list