ITK/Examples/ImageProcessing/AbsImageFilter: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
(Created page with "==AbsImageFilter.cxx== <source lang="cpp"> #include "itkImage.h" #include "itkImageFileWriter.h" #include "itkAbsImageFilter.h" #include <itkImageToVTKImageFilter.h> #include ...")
 
No edit summary
Line 3: Line 3:
#include "itkImage.h"
#include "itkImage.h"
#include "itkImageFileWriter.h"
#include "itkImageFileWriter.h"
 
#include "itkRescaleIntensityImageFilter.h"
#include "itkAbsImageFilter.h"
#include "itkAbsImageFilter.h"


Line 15: Line 15:
#include "vtkRenderer.h"
#include "vtkRenderer.h"


typedef itk::Image<unsigned char, 2>  ImageType;
typedef itk::Image<unsigned char, 2>  UnsignedCharImageType;
typedef itk::Image<float, 2>  FloatImageType;


void CreateImage(ImageType::Pointer image);
void CreateImage(FloatImageType::Pointer image);


int main(int, char *[])
int main(int, char *[])
{
{
   ImageType::Pointer image = ImageType::New();
   FloatImageType::Pointer image = FloatImageType::New();
   CreateImage(image);
   CreateImage(image);


   typedef itk::AbsImageFilter <ImageType, ImageType>
  // Rescale the image so that it can be displayed
  typedef itk::RescaleIntensityImageFilter< FloatImageType, UnsignedCharImageType > FloatRescaleCastFilterType;
  FloatRescaleCastFilterType::Pointer rescaleFilter = FloatRescaleCastFilterType::New();
  rescaleFilter->SetInput(image);
  rescaleFilter->SetOutputMinimum(0);
  rescaleFilter->SetOutputMaximum(255);
  rescaleFilter->Update();
 
  // Take the absolute value of the image
   typedef itk::AbsImageFilter <FloatImageType, UnsignedCharImageType>
           AbsImageFilterType;
           AbsImageFilterType;


Line 32: Line 42:
   absFilter->Update();
   absFilter->Update();


   // Visualize first image
  typedef itk::RescaleIntensityImageFilter< UnsignedCharImageType, UnsignedCharImageType > RescaleFilterType;
   typedef itk::ImageToVTKImageFilter<ImageType> ConnectorType;
  RescaleFilterType::Pointer absRescaleFilter = RescaleFilterType::New();
   ConnectorType::Pointer connector = ConnectorType::New();
  absRescaleFilter->SetInput(absFilter->GetOutput());
   connector->SetInput(image);
  absRescaleFilter->SetOutputMinimum(0);
  absRescaleFilter->SetOutputMaximum(255);
  absRescaleFilter->Update();
 
   // Visualize input image
   typedef itk::ImageToVTKImageFilter<UnsignedCharImageType> ConnectorType;
   ConnectorType::Pointer originalConnector = ConnectorType::New();
   originalConnector->SetInput(rescaleFilter->GetOutput());


   vtkSmartPointer<vtkImageActor> actor =
   vtkSmartPointer<vtkImageActor> actor =
     vtkSmartPointer<vtkImageActor>::New();
     vtkSmartPointer<vtkImageActor>::New();
   actor->SetInput(connector->GetOutput());
   actor->SetInput(originalConnector->GetOutput());


   // Visualize flipped image
   // Visualize abs image
   ConnectorType::Pointer absConnector = ConnectorType::New();
   ConnectorType::Pointer absConnector = ConnectorType::New();
   absConnector->SetInput(absFilter->GetOutput());
   absConnector->SetInput(absRescaleFilter->GetOutput());


   vtkSmartPointer<vtkImageActor> absActor =
   vtkSmartPointer<vtkImageActor> absActor =
Line 94: Line 111:
}
}


void CreateImage(ImageType::Pointer image)
void CreateImage(FloatImageType::Pointer image)
{
{
   // Create an image with 2 connected components
   // Create an image with negative values
   ImageType::RegionType region;
   FloatImageType::RegionType region;
   ImageType::IndexType start;
   FloatImageType::IndexType start;
   start[0] = 0;
   start[0] = 0;
   start[1] = 0;
   start[1] = 0;


   ImageType::SizeType size;
   FloatImageType::SizeType size;
  unsigned int NumRows = 200;
   size[0] = 200;
  unsigned int NumCols = 300;
   size[1] = 300;
   size[0] = NumRows;
   size[1] = NumCols;


   region.SetSize(size);
   region.SetSize(size);
Line 114: Line 129:
   image->Allocate();
   image->Allocate();


   // Make a square
   itk::ImageRegionIterator<FloatImageType> imageIterator(image,region);
  for(unsigned int r = 20; r < 80; r++)
  {
      for(unsigned int c = 20; c < 80; c++)
      {
          ImageType::IndexType pixelIndex;
          pixelIndex[0] = r;
          pixelIndex[1] = c;


          image->SetPixel(pixelIndex, 150);
  while(!imageIterator.IsAtEnd())
      }
    {
   }
    imageIterator.Set(imageIterator.GetIndex()[0] - imageIterator.GetIndex()[1]);
    ++imageIterator;
    }
    
}
}



Revision as of 17:25, 6 November 2010

AbsImageFilter.cxx

<source lang="cpp">

  1. include "itkImage.h"
  2. include "itkImageFileWriter.h"
  3. include "itkRescaleIntensityImageFilter.h"
  4. include "itkAbsImageFilter.h"
  1. include <itkImageToVTKImageFilter.h>
  1. include "vtkImageViewer.h"
  2. include "vtkRenderWindowInteractor.h"
  3. include "vtkSmartPointer.h"
  4. include "vtkImageActor.h"
  5. include "vtkInteractorStyleImage.h"
  6. include "vtkRenderer.h"

typedef itk::Image<unsigned char, 2> UnsignedCharImageType; typedef itk::Image<float, 2> FloatImageType;

void CreateImage(FloatImageType::Pointer image);

int main(int, char *[]) {

 FloatImageType::Pointer image = FloatImageType::New();
 CreateImage(image);
 // Rescale the image so that it can be displayed
 typedef itk::RescaleIntensityImageFilter< FloatImageType, UnsignedCharImageType > FloatRescaleCastFilterType;
 FloatRescaleCastFilterType::Pointer rescaleFilter = FloatRescaleCastFilterType::New();
 rescaleFilter->SetInput(image);
 rescaleFilter->SetOutputMinimum(0);
 rescaleFilter->SetOutputMaximum(255);
 rescaleFilter->Update();
 // Take the absolute value of the image
 typedef itk::AbsImageFilter <FloatImageType, UnsignedCharImageType>
         AbsImageFilterType;
 AbsImageFilterType::Pointer absFilter
         = AbsImageFilterType::New ();
 absFilter->SetInput(image);
 absFilter->Update();
 typedef itk::RescaleIntensityImageFilter< UnsignedCharImageType, UnsignedCharImageType > RescaleFilterType;
 RescaleFilterType::Pointer absRescaleFilter = RescaleFilterType::New();
 absRescaleFilter->SetInput(absFilter->GetOutput());
 absRescaleFilter->SetOutputMinimum(0);
 absRescaleFilter->SetOutputMaximum(255);
 absRescaleFilter->Update();
 // Visualize input image
 typedef itk::ImageToVTKImageFilter<UnsignedCharImageType> ConnectorType;
 ConnectorType::Pointer originalConnector = ConnectorType::New();
 originalConnector->SetInput(rescaleFilter->GetOutput());
 vtkSmartPointer<vtkImageActor> actor =
   vtkSmartPointer<vtkImageActor>::New();
 actor->SetInput(originalConnector->GetOutput());
 // Visualize abs image
 ConnectorType::Pointer absConnector = ConnectorType::New();
 absConnector->SetInput(absRescaleFilter->GetOutput());
 vtkSmartPointer<vtkImageActor> absActor =
   vtkSmartPointer<vtkImageActor>::New();
 absActor->SetInput(absConnector->GetOutput());
 // There will be one render window
 vtkSmartPointer<vtkRenderWindow> renderWindow =
   vtkSmartPointer<vtkRenderWindow>::New();
 renderWindow->SetSize(600, 300);
 vtkSmartPointer<vtkRenderWindowInteractor> interactor =
   vtkSmartPointer<vtkRenderWindowInteractor>::New();
 interactor->SetRenderWindow(renderWindow);
 // Define viewport ranges
 // (xmin, ymin, xmax, ymax)
 double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
 double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};
 // Setup both renderers
 vtkSmartPointer<vtkRenderer> leftRenderer =
   vtkSmartPointer<vtkRenderer>::New();
 renderWindow->AddRenderer(leftRenderer);
 leftRenderer->SetViewport(leftViewport);
 leftRenderer->SetBackground(.6, .5, .4);
 vtkSmartPointer<vtkRenderer> rightRenderer =
   vtkSmartPointer<vtkRenderer>::New();
 renderWindow->AddRenderer(rightRenderer);
 rightRenderer->SetViewport(rightViewport);
 rightRenderer->SetBackground(.4, .5, .6);
 // Add the sphere to the left and the cube to the right
 leftRenderer->AddActor(actor);
 rightRenderer->AddActor(absActor);
 leftRenderer->ResetCamera();
 rightRenderer->ResetCamera();
 renderWindow->Render();
 vtkSmartPointer<vtkInteractorStyleImage> style =
   vtkSmartPointer<vtkInteractorStyleImage>::New();
 interactor->SetInteractorStyle(style);
 interactor->Start();
 return EXIT_SUCCESS;

}

void CreateImage(FloatImageType::Pointer image) {

 // Create an image with negative values
 FloatImageType::RegionType region;
 FloatImageType::IndexType start;
 start[0] = 0;
 start[1] = 0;
 FloatImageType::SizeType size;
 size[0] = 200;
 size[1] = 300;
 region.SetSize(size);
 region.SetIndex(start);
 image->SetRegions(region);
 image->Allocate();
 itk::ImageRegionIterator<FloatImageType> imageIterator(image,region);
 while(!imageIterator.IsAtEnd())
   {
   imageIterator.Set(imageIterator.GetIndex()[0] - imageIterator.GetIndex()[1]);
   ++imageIterator;
   }
 

}

</source>

CMakeLists.txt

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

PROJECT(AbsImageFilter)

include_directories(/home/doriad/src/ITK/Wrapping/WrapITK/ExternalProjects/ItkVtkGlue/src/)

FIND_PACKAGE(VTK REQUIRED) INCLUDE(${VTK_USE_FILE})

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

ADD_EXECUTABLE(AbsImageFilter AbsImageFilter.cxx) TARGET_LINK_LIBRARIES(AbsImageFilter vtkHybrid ITKBasicFilters ITKCommon ITKIO)

</source>