IdealHighPass
VTKExamples/Cxx/ImageProcessing/IdealHighPass
Description¶
Low-pass and high-pass filtering become trivial in the frequency domain. A portion of the pixels are simply masked or attenuated. This example shows a high pass Butterworth filter that attenuates the frequency domain image with the function out(i, j) = 1 / (1 + pow(CutOff/Freq(i,j), 2*Order))
.
The gradual attenuation of the filter is important. The ideal high-pass filter, shown in the same exaample, simply masks a set of pixels in the frequency domain. The abrupt transition causes a ringing effect in the spatial domain.
Info
See this figure in Chapter 10 the VTK Textbook.
Other Languages
See (Python)
Question
If you have a simple question about this example contact us at VTKExamplesProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum
Code¶
IdealHighPass.cxx
#include <vtkCamera.h> #include <vtkImageActor.h> #include <vtkImageButterworthHighPass.h> #include <vtkImageExtractComponents.h> #include <vtkImageFFT.h> #include <vtkImageIdealHighPass.h> #include <vtkImageMapper3D.h> #include <vtkImageMapToWindowLevelColors.h> #include <vtkImageProperty.h> #include <vtkImageReader2.h> #include <vtkImageReader2Factory.h> #include <vtkImageRFFT.h> #include <vtkInteractorStyleImage.h> #include <vtkNamedColors.h> #include <vtkRenderer.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkSmartPointer.h> int main (int argc, char *argv[]) { // Verify input arguments if ( argc < 2 ) { std::cout << "Usage: " << argv[0] << " Filename" << std::endl; return EXIT_FAILURE; } // Read the image vtkSmartPointer<vtkImageReader2Factory> readerFactory = vtkSmartPointer<vtkImageReader2Factory>::New(); vtkSmartPointer<vtkImageReader2> reader; reader.TakeReference( readerFactory->CreateImageReader2(argv[1])); reader->SetFileName(argv[1]); reader->Update(); vtkSmartPointer<vtkImageFFT> fft = vtkSmartPointer<vtkImageFFT>::New(); fft->SetInputConnection(reader->GetOutputPort()); vtkSmartPointer<vtkImageIdealHighPass> idealHighPass = vtkSmartPointer<vtkImageIdealHighPass>::New(); idealHighPass->SetInputConnection(fft->GetOutputPort()); idealHighPass->SetXCutOff(0.1); idealHighPass->SetYCutOff(0.1); vtkSmartPointer<vtkImageRFFT> idealRfft = vtkSmartPointer<vtkImageRFFT>::New(); idealRfft->SetInputConnection(idealHighPass->GetOutputPort()); vtkSmartPointer<vtkImageExtractComponents> idealReal = vtkSmartPointer<vtkImageExtractComponents>::New(); idealReal->SetInputConnection(idealRfft->GetOutputPort()); idealReal->SetComponents(0); vtkSmartPointer<vtkImageButterworthHighPass> butterworthHighPass = vtkSmartPointer<vtkImageButterworthHighPass>::New(); butterworthHighPass->SetInputConnection(fft->GetOutputPort()); butterworthHighPass->SetXCutOff(0.1); butterworthHighPass->SetYCutOff(0.1); vtkSmartPointer<vtkImageRFFT> butterworthRfft = vtkSmartPointer<vtkImageRFFT>::New(); butterworthRfft->SetInputConnection(butterworthHighPass->GetOutputPort()); vtkSmartPointer<vtkImageExtractComponents> butterworthReal = vtkSmartPointer<vtkImageExtractComponents>::New(); butterworthReal->SetInputConnection(butterworthRfft->GetOutputPort()); butterworthReal->SetComponents(0); // Create actors vtkSmartPointer<vtkNamedColors> colors = vtkSmartPointer<vtkNamedColors>::New(); vtkSmartPointer<vtkImageMapToWindowLevelColors> idealColor = vtkSmartPointer<vtkImageMapToWindowLevelColors>::New(); idealColor->SetWindow(500); idealColor->SetLevel(0); idealColor->SetInputConnection(idealReal->GetOutputPort()); vtkSmartPointer<vtkImageActor> idealActor = vtkSmartPointer<vtkImageActor>::New(); idealActor->GetMapper()->SetInputConnection(idealColor->GetOutputPort()); idealActor->GetProperty()->SetInterpolationTypeToNearest(); vtkSmartPointer<vtkImageMapToWindowLevelColors> butterworthColor = vtkSmartPointer<vtkImageMapToWindowLevelColors>::New(); butterworthColor->SetWindow(500); butterworthColor->SetLevel(0); butterworthColor->SetInputConnection(butterworthReal->GetOutputPort()); vtkSmartPointer<vtkImageActor> butterworthActor = vtkSmartPointer<vtkImageActor>::New(); butterworthActor->GetMapper()->SetInputConnection(butterworthColor->GetOutputPort()); butterworthActor->GetProperty()->SetInterpolationTypeToNearest(); // Setup renderers vtkSmartPointer<vtkRenderer> idealRenderer = vtkSmartPointer<vtkRenderer>::New(); idealRenderer->SetViewport(0.0, 0.0, 0.5, 1.0); idealRenderer->AddActor(idealActor); idealRenderer->ResetCamera(); idealRenderer->SetBackground(colors->GetColor3d("SlateGray").GetData()); vtkSmartPointer<vtkRenderer> butterworthRenderer = vtkSmartPointer<vtkRenderer>::New(); butterworthRenderer->SetViewport(0.5, 0.0, 1.0, 1.0); butterworthRenderer->AddActor(butterworthActor); butterworthRenderer->SetActiveCamera(idealRenderer->GetActiveCamera()); butterworthRenderer->SetBackground(colors->GetColor3d("LightSlateGray").GetData()); vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->SetSize(600, 300); renderWindow->AddRenderer(idealRenderer); renderWindow->AddRenderer(butterworthRenderer); vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); vtkSmartPointer<vtkInteractorStyleImage> style = vtkSmartPointer<vtkInteractorStyleImage>::New(); renderWindowInteractor->SetInteractorStyle(style); renderWindowInteractor->SetRenderWindow(renderWindow); idealRenderer->GetActiveCamera()->Dolly(1.4); idealRenderer->ResetCameraClippingRange(); renderWindow->Render(); renderWindowInteractor->Initialize(); renderWindowInteractor->Start(); return EXIT_SUCCESS; }
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR) project(IdealHighPass) find_package(VTK COMPONENTS vtkCommonColor vtkCommonCore vtkIOImage vtkImagingColor vtkImagingCore vtkImagingFourier vtkInteractionStyle vtkRenderingContextOpenGL2 vtkRenderingCore vtkRenderingFreeType vtkRenderingGL2PSOpenGL2 vtkRenderingOpenGL2 QUIET) if (NOT VTK_FOUND) message("Skipping IdealHighPass: ${VTK_NOT_FOUND_MESSAGE}") return () endif() message (STATUS "VTK_VERSION: ${VTK_VERSION}") if (VTK_VERSION VERSION_LESS "8.90.0") # old system include(${VTK_USE_FILE}) add_executable(IdealHighPass MACOSX_BUNDLE IdealHighPass.cxx ) target_link_libraries(IdealHighPass PRIVATE ${VTK_LIBRARIES}) else () # include all components add_executable(IdealHighPass MACOSX_BUNDLE IdealHighPass.cxx ) target_link_libraries(IdealHighPass PRIVATE ${VTK_LIBRARIES}) # vtk_module_autoinit is needed vtk_module_autoinit( TARGETS IdealHighPass MODULES ${VTK_LIBRARIES} ) endif ()
Download and Build IdealHighPass¶
Click here to download IdealHighPass and its CMakeLists.txt file. Once the tarball IdealHighPass.tar has been downloaded and extracted,
cd IdealHighPass/build
If VTK is installed:
cmake ..
If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:
cmake -DVTK_DIR:PATH=/home/me/vtk_build ..
Build the project:
make
and run it:
./IdealHighPass
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.