VTK/Examples/Developers/vtkImageAlgorithm Filter
From KitwarePublic
< VTK | Examples | Developers(Redirected from VTK Examples vtkImageAlgorithm Filter)
This example demonstrates how to setup the pipeline for a filter that takes a vtkImageData as input and produces a vtkImageData as output. This particular example creates a 2x2 image and fills it with "2"s. The filter changes the (0,0) element to a 5.
Contents |
vtkImageAlgorithmFilterExample.cxx
#include <vtkSmartPointer.h> #include "vtkImageAlgorithmFilter.h" #include "vtkImageData.h" void PrintImage(vtkImageData* image); int main (int argc, char *argv[]) { vtkSmartPointer<vtkImageData> input = vtkSmartPointer<vtkImageData>::New(); // Setup the image input->SetDimensions(2,2,1); input->SetNumberOfScalarComponents(1); // Fill every entry of the image data with "2.0" int* dims = input->GetDimensions(); for (int y=0; y<dims[1]; y++) { for (int x=0; x<dims[0]; x++) { input->SetScalarComponentFromDouble(x,y,0,0,2.0); } } std::cout << "Input image: " << std::endl; PrintImage(input); vtkSmartPointer<vtkImageAlgorithmFilter> filter = vtkSmartPointer<vtkImageAlgorithmFilter>::New(); filter->SetInput(input); filter->Update(); vtkImageData* output = filter->GetOutput(); std::cout << "Output image: " << std::endl; PrintImage(output); return 0; } void PrintImage(vtkImageData* image) { int* dims = image->GetDimensions(); for (int y=0; y<dims[1]; y++) { for (int x=0; x<dims[0]; x++) { double v = image->GetScalarComponentAsDouble(x,y,0,0); std::cout << v << " "; } std::cout << std::endl; } }
vtkImageAlgorithmFilter.h
#ifndef __vtkImageAlgorithmFilter_h #define __vtkImageAlgorithmFilter_h #include "vtkImageAlgorithm.h" class vtkImageAlgorithmFilter : public vtkImageAlgorithm { public: static vtkImageAlgorithmFilter *New(); vtkTypeMacro(vtkImageAlgorithmFilter,vtkImageAlgorithm); vtkImageAlgorithmFilter(){} protected: int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *); private: vtkImageAlgorithmFilter(const vtkImageAlgorithmFilter&); // Not implemented. void operator=(const vtkImageAlgorithmFilter&); // Not implemented. }; #endif
vtkImageAlgorithmFilter.cxx
#include "vtkImageAlgorithmFilter.h" #include "vtkImageData.h" #include "vtkObjectFactory.h" #include "vtkStreamingDemandDrivenPipeline.h" #include "vtkInformationVector.h" #include "vtkInformation.h" #include "vtkDataObject.h" #include "vtkSmartPointer.h" vtkStandardNewMacro(vtkImageAlgorithmFilter); int vtkImageAlgorithmFilter::RequestData(vtkInformation *vtkNotUsed(request), vtkInformationVector **inputVector, vtkInformationVector *outputVector) { // Get the info objects vtkInformation *inInfo = inputVector[0]->GetInformationObject(0); vtkInformation *outInfo = outputVector->GetInformationObject(0); // Get the input and ouptut vtkImageData *input = vtkImageData::SafeDownCast( inInfo->Get(vtkDataObject::DATA_OBJECT())); vtkImageData *output = vtkImageData::SafeDownCast( outInfo->Get(vtkDataObject::DATA_OBJECT())); vtkSmartPointer<vtkImageData> image = vtkSmartPointer<vtkImageData>::New(); image->ShallowCopy(input); image->SetScalarComponentFromDouble(0,0,0,0, 5.0); output->ShallowCopy(image); // Without these lines, the output will appear real but will not work as the input to any other filters output->SetExtent(input->GetExtent()); output->SetUpdateExtent(output->GetExtent()); output->SetWholeExtent(output->GetExtent()); return 1; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(vtkImageAlgorithmFilterExample) FIND_PACKAGE(VTK REQUIRED) INCLUDE(${VTK_USE_FILE}) ADD_EXECUTABLE(vtkImageAlgorithmFilterExample vtkImageAlgorithmFilterExample.cxx vtkImageAlgorithmFilter.cxx) TARGET_LINK_LIBRARIES(vtkImageAlgorithmFilterExample vtkHybrid)