ITK  6.0.0
Insight Toolkit
SphinxExamples/src/Bridge/VtkGlue/VTKImageToITKImage/Code.cxx
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include <itkImage.h>
#include "vtkSmartPointer.h"
#include "vtkPNGReader.h"
#include <vtkImageLuminance.h>
#ifdef ENABLE_QUICKVIEW
# include "QuickView.h"
#endif
int
main(int argc, char * argv[])
{
if (argc < 2)
{
std::cerr << "Required: filename" << std::endl;
return EXIT_FAILURE;
}
vtkSmartPointer<vtkPNGReader> reader = vtkSmartPointer<vtkPNGReader>::New();
reader->SetFileName(argv[1]);
// reader->SetNumberOfScalarComponents(1); //doesn't seem to work - use ImageLuminance instead
reader->Update();
// Must convert image to grayscale because itkVTKImageToImageFilter only accepts single channel images
vtkSmartPointer<vtkImageLuminance> luminanceFilter = vtkSmartPointer<vtkImageLuminance>::New();
luminanceFilter->SetInputConnection(reader->GetOutputPort());
luminanceFilter->Update();
using ImageType = itk::Image<unsigned char, 2>;
using VTKImageToImageType = itk::VTKImageToImageFilter<ImageType>;
auto vtkImageToImageFilter = VTKImageToImageType::New();
vtkImageToImageFilter->SetInput(luminanceFilter->GetOutput());
// vtkImageToImageFilter->SetInput(reader->GetOutput());
vtkImageToImageFilter->Update();
auto image = ImageType::New();
image->Graft(vtkImageToImageFilter->GetOutput()); // Need to do this because QuickView can't accept const
#ifdef ENABLE_QUICKVIEW
QuickView viewer;
viewer.AddImage(image.GetPointer()); // Need to do this because QuickView can't accept smart pointers
viewer.Visualize();
#endif
return EXIT_SUCCESS;
}
itkImage.h
QuickView.h
QuickView::AddImage
void AddImage(TImage *, bool FlipVertical=true, std::string Description="")
QuickView
A convenient class to render itk images with vtk.
Definition: QuickView.h:111
itkVTKImageToImageFilter.h
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
New
static Pointer New()
itk::VTKImageToImageFilter
Converts a VTK image into an ITK image and plugs a VTK data pipeline to an ITK datapipeline.
Definition: itkVTKImageToImageFilter.h:51
QuickView::Visualize
void Visualize(bool interact=true)