ITK/Examples/Cxx/Developer/ImageSource: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
(Created page with "==ImageSourceExample.cxx== <source lang="cp"> #include "itkImage.h" #include "itkImageFileWriter.h" #include "ImageFilter.h" int main(int, char*[]) { // Setup types typedef...")
 
No edit summary
Line 1: Line 1:
==ImageSourceExample.cxx==
==ImageSourceExample.cxx==
<source lang="cp">
<source lang="cpp">
#include "itkImage.h"
#include "itkImage.h"
#include "itkImageFileWriter.h"
#include "itkImageFileWriter.h"
Line 27: Line 27:


==ImageSource.h==
==ImageSource.h==
<source lang="cp">
<source lang="cpp">
#ifndef __itkImageFilter_h
#ifndef __itkImageFilter_h
#define __itkImageFilter_h
#define __itkImageFilter_h
Line 74: Line 74:


==ImageSource.txx==
==ImageSource.txx==
<source lang="cp">
<source lang="cpp">
#ifndef __itkImageFilter_txx
#ifndef __itkImageFilter_txx
#define __itkImageFilter_txx
#define __itkImageFilter_txx

Revision as of 19:28, 7 November 2010

ImageSourceExample.cxx

<source lang="cpp">

  1. include "itkImage.h"
  2. include "itkImageFileWriter.h"
  1. include "ImageFilter.h"

int main(int, char*[]) {

 // Setup types
 typedef itk::Image<unsigned char, 2>   ImageType;
 typedef itk::ImageFilter<ImageType>  FilterType;
 // Create and the filter
 FilterType::Pointer filter = FilterType::New();
 filter->Update();
 
 typedef  itk::ImageFileWriter< ImageType  > WriterType;
 WriterType::Pointer writer = WriterType::New();
 writer->SetFileName("Test.jpg");
 writer->SetInput(filter->GetOutput());
 writer->Update();
 
 return EXIT_SUCCESS;

} </source>

ImageSource.h

<source lang="cpp">

  1. ifndef __itkImageFilter_h
  2. define __itkImageFilter_h
  1. include "itkImageSource.h"

namespace itk { template< class TOutputImage> class ImageFilter:public ImageSource< TOutputImage > { public:

 /** Standard class typedefs. */
 typedef ImageFilter             Self;
 typedef ImageSource< TOutputImage > Superclass;
 typedef SmartPointer< Self >        Pointer;
 /** Method for creation through the object factory. */
 itkNewMacro(Self);
 /** Run-time type information (and related methods). */
 itkTypeMacro(ImageFilter, ImageSource);

protected:

 ImageFilter(){}
 ~ImageFilter(){}
 /** Does the real work. */
 virtual void GenerateData();

private:

 ImageFilter(const Self &); //purposely not implemented
 void operator=(const Self &);  //purposely not implemented

}; } //namespace ITK


  1. ifndef ITK_MANUAL_INSTANTIATION
  2. include "ImageFilter.txx"
  3. endif


  1. endif // __itkImageFilter_h

</source>

ImageSource.txx

<source lang="cpp">

  1. ifndef __itkImageFilter_txx
  2. define __itkImageFilter_txx
  1. include "ImageFilter.h"
  1. include "itkObjectFactory.h"

namespace itk {

template< class TOutputImage> void ImageFilter< TOutputImage>

GenerateData()

{

 typename TOutputImage::Pointer output = this->GetOutput();
 typename TOutputImage::RegionType region;
 typename TOutputImage::IndexType start;
 start[0] = 0;
 start[1] = 0;
 typename TOutputImage::SizeType size;
 size[0] = 200;
 size[1] = 300;
 region.SetSize(size);
 region.SetIndex(start);
 output->SetRegions(region);
 output->Allocate();
 itk::ImageRegionIterator<TOutputImage> imageIterator(output,output->GetLargestPossibleRegion());
 while(!imageIterator.IsAtEnd())
   {
   if(imageIterator.GetIndex()[0] == imageIterator.GetIndex()[1])
     {
     imageIterator.Set(255);
     }
   else
     {
     imageIterator.Set(0);
     }
   ++imageIterator;
   }

}

}// end namespace


  1. endif

</source>

CMakeLists.txt

<source lang="cmake"> cmake_minimum_required(VERSION 2.6)

PROJECT(ImageSource)

FIND_PACKAGE(ITK REQUIRED) INCLUDE(${ITK_USE_FILE})

ADD_EXECUTABLE(ImageSource ImageSourceExample.cxx) TARGET_LINK_LIBRARIES(ImageSource ITKBasicFilters ITKIO ITKCommon)

</source>