ITK/Examples/Broken/Images/GradientImageFilter: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
No edit summary
No edit summary
Line 159: Line 159:
</source>
</source>


{{ITKVTKCMakeLists|GradientImageFilter|}}
{{ITKVTKCMakeLists|{{SUBPAGENAME}}}}

Revision as of 16:34, 24 December 2012

GradientImageFilter.cxx

<source lang="cpp">

  1. include "itkImage.h"
  2. include "itkCovariantVector.h"
  3. include "itkImageFileReader.h"
  4. include "itkImageFileWriter.h"
  5. include "itkGradientImageFilter.h"
  1. include <itkImageToVTKImageFilter.h>
  1. include "vtkPointData.h"
  2. include "vtkFloatArray.h"
  3. include "vtkImageData.h"
  4. include "vtkRenderWindowInteractor.h"
  5. include "vtkRenderWindow.h"
  6. include "vtkSmartPointer.h"
  7. include "vtkImageActor.h"
  8. include "vtkActor.h"
  9. include "vtkInteractorStyleImage.h"
  10. include "vtkRenderer.h"
  11. include "vtkGlyph3DMapper.h"
  12. include "vtkArrowSource.h"

static void VectorImageToVTKImage(itk::Image<itk::CovariantVector<float, 2>, 2>::Pointer vectorImage, vtkImageData* VTKImage);

int main(int argc, char * argv[]) {

 // Verify command line arguments
 if( argc < 2 )
   {
   std::cerr << "Usage: " << std::endl;
   std::cerr << argv[0] << "inputImageFile" << std::endl;
   return EXIT_FAILURE;
   }
 // Parse command line arguments
 std::string inputFilename = argv[1];
 // Setup types
 typedef itk::Image< float,  2 >   FloatImageType;
 typedef itk::Image< unsigned char, 2 >   UnsignedCharImageType;
 // Create and setup a reader
 typedef itk::ImageFileReader< UnsignedCharImageType >  ReaderType;
 ReaderType::Pointer reader = ReaderType::New();
 reader->SetFileName( inputFilename.c_str() );
 
 // Create and setup a gradient filter
 typedef itk::GradientImageFilter<
     UnsignedCharImageType, float>  GradientFilterType;
 GradientFilterType::Pointer gradientFilter = GradientFilterType::New();
 gradientFilter->SetInput( reader->GetOutput() );
 gradientFilter->Update();
 // Visualize original image
 typedef itk::ImageToVTKImageFilter<UnsignedCharImageType> ConnectorType;
 ConnectorType::Pointer originalConnector = ConnectorType::New();
 originalConnector->SetInput(reader->GetOutput());
 vtkSmartPointer<vtkImageActor> originalActor =
   vtkSmartPointer<vtkImageActor>::New();
 originalActor->SetInput(originalConnector->GetOutput());
 // Visualize gradient
 vtkSmartPointer<vtkImageData> gradientImage =
   vtkSmartPointer<vtkImageData>::New();
 VectorImageToVTKImage(gradientFilter->GetOutput(), gradientImage);
 
 vtkSmartPointer<vtkArrowSource> arrowSource =
   vtkSmartPointer<vtkArrowSource>::New();
 
 vtkSmartPointer<vtkGlyph3DMapper> gradientMapper =
   vtkSmartPointer<vtkGlyph3DMapper>::New();
 gradientMapper->ScalingOn();
 gradientMapper->SetScaleFactor(.05);
 gradientMapper->SetSourceConnection(arrowSource->GetOutputPort());
 gradientMapper->SetInputConnection(gradientImage->GetProducerPort());
 gradientMapper->Update();
 vtkSmartPointer<vtkActor> gradientActor =
   vtkSmartPointer<vtkActor>::New();
 gradientActor->SetMapper(gradientMapper);
 // Visualize
 // 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<vtkRenderWindow> renderWindow =
   vtkSmartPointer<vtkRenderWindow>::New();
 renderWindow->SetSize(600,300);
 vtkSmartPointer<vtkRenderer> leftRenderer =
   vtkSmartPointer<vtkRenderer>::New();
 renderWindow->AddRenderer(leftRenderer);
 leftRenderer->SetViewport(leftViewport);
 vtkSmartPointer<vtkRenderer> rightRenderer =
   vtkSmartPointer<vtkRenderer>::New();
 renderWindow->AddRenderer(rightRenderer);
 rightRenderer->SetViewport(rightViewport);
 rightRenderer->SetBackground(1,0,0);
 leftRenderer->AddActor(originalActor);
 rightRenderer->AddActor(gradientActor);
 vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
   vtkSmartPointer<vtkRenderWindowInteractor>::New();
 vtkSmartPointer<vtkInteractorStyleImage> style =
   vtkSmartPointer<vtkInteractorStyleImage>::New();
 renderWindowInteractor->SetInteractorStyle(style);
 renderWindowInteractor->SetRenderWindow(renderWindow);
 renderWindowInteractor->Initialize();
 renderWindowInteractor->Start();
 return EXIT_SUCCESS;

}

void VectorImageToVTKImage(itk::Image<itk::CovariantVector<float, 2>, 2>::Pointer vectorImage, vtkImageData* VTKImage) {

 itk::Image<itk::CovariantVector<float, 2>, 2>::RegionType region = vectorImage->GetLargestPossibleRegion();
 itk::Image<itk::CovariantVector<float, 2>, 2>::SizeType imageSize = region.GetSize();
 VTKImage->SetExtent(0, imageSize[0] -1, 0, imageSize[1] - 1, 0, 0);
 vtkSmartPointer<vtkFloatArray> vectors =
   vtkSmartPointer<vtkFloatArray>::New();
 vectors->SetNumberOfComponents(3);
 vectors->SetNumberOfTuples(imageSize[0] * imageSize[1]);
 vectors->SetName("GradientVectors");
 int counter = 0;
 for(unsigned int j = 0; j < imageSize[1]; j++)
   {
   for(unsigned int i = 0; i < imageSize[0]; i++)
     {
     itk::Image<itk::CovariantVector<float, 2>, 2>::IndexType index;
     index[0] = i;
     index[1] = j;
     itk::Image<itk::CovariantVector<float, 2>, 2>::PixelType pixel = vectorImage->GetPixel(index);
   
     float v[2];
     v[0] = pixel[0];
     v[1] = pixel[1];
     v[2] = 0;
     vectors->InsertTupleValue(counter, v);
     counter++;
     }
   }
 //std::cout << region << std::endl;
 
 VTKImage->GetPointData()->SetVectors(vectors);

} </source>

CMakeLists.txt

<syntaxhighlight lang="cmake"> cmake_minimum_required(VERSION 3.9.5)

project(GradientImageFilter)

find_package(ITK REQUIRED) include(${ITK_USE_FILE}) if (ITKVtkGlue_LOADED)

 find_package(VTK REQUIRED)
 include(${VTK_USE_FILE})

else()

 find_package(ItkVtkGlue REQUIRED)
 include(${ItkVtkGlue_USE_FILE})
 set(Glue ItkVtkGlue)

endif()

add_executable(GradientImageFilter MACOSX_BUNDLE GradientImageFilter.cxx) target_link_libraries(GradientImageFilter

 ${Glue}  ${VTK_LIBRARIES} ${ITK_LIBRARIES})

</syntaxhighlight>

Download and Build GradientImageFilter

Click here to download GradientImageFilter. and its CMakeLists.txt file. Once the tarball GradientImageFilter.tar has been downloaded and extracted,

cd GradientImageFilter/build 
  • If ITK is installed:
cmake ..
  • If ITK is not installed but compiled on your system, you will need to specify the path to your ITK build:
cmake -DITK_DIR:PATH=/home/me/itk_build ..

Build the project,

make

and run it:

./GradientImageFilter

WINDOWS USERS PLEASE NOTE: Be sure to add the VTK and ITK bin directories to your path. This will resolve the VTK and ITK dll's at run time.

Building All of the Examples

Many of the examples in the ITK Wiki Examples Collection require VTK. You can build all of the the examples by following these instructions. If you are a new VTK user, you may want to try the Superbuild which will build a proper ITK and VTK.

ItkVtkGlue

ITK >= 4

For examples that use QuickView (which depends on VTK), you must have built ITK with Module_ITKVtkGlue=ON.

ITK < 4

Some of the ITK Examples require VTK to display the images. If you download the entire ITK Wiki Examples Collection, the ItkVtkGlue directory will be included and configured. If you wish to just build a few examples, then you will need to download ItkVtkGlue and build it. When you run cmake it will ask you to specify the location of the ItkVtkGlue binary directory.