Skip to content

IdealHighPass

vtk-examples/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.

Other languages

See (Python)

Question

If you have a question about this example, 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 <vtkImageMapToWindowLevelColors.h>
#include <vtkImageMapper3D.h>
#include <vtkImageProperty.h>
#include <vtkImageRFFT.h>
#include <vtkImageReader2.h>
#include <vtkImageReader2Factory.h>
#include <vtkInteractorStyleImage.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>

int main(int argc, char* argv[])
{
  // Verify input arguments
  if (argc < 2)
  {
    std::cout << "Usage: " << argv[0] << " Filename e.g. fullhead15.png"
              << std::endl;
    return EXIT_FAILURE;
  }

  // Read the image
  vtkNew<vtkImageReader2Factory> readerFactory;
  vtkSmartPointer<vtkImageReader2> reader;
  reader.TakeReference(readerFactory->CreateImageReader2(argv[1]));
  reader->SetFileName(argv[1]);
  reader->Update();

  vtkNew<vtkImageFFT> fft;
  fft->SetInputConnection(reader->GetOutputPort());

  vtkNew<vtkImageIdealHighPass> idealHighPass;
  idealHighPass->SetInputConnection(fft->GetOutputPort());
  idealHighPass->SetXCutOff(0.1);
  idealHighPass->SetYCutOff(0.1);

  vtkNew<vtkImageRFFT> idealRfft;
  idealRfft->SetInputConnection(idealHighPass->GetOutputPort());

  vtkNew<vtkImageExtractComponents> idealReal;
  idealReal->SetInputConnection(idealRfft->GetOutputPort());
  idealReal->SetComponents(0);

  vtkNew<vtkImageButterworthHighPass> butterworthHighPass;
  butterworthHighPass->SetInputConnection(fft->GetOutputPort());
  butterworthHighPass->SetXCutOff(0.1);
  butterworthHighPass->SetYCutOff(0.1);

  vtkNew<vtkImageRFFT> butterworthRfft;
  butterworthRfft->SetInputConnection(butterworthHighPass->GetOutputPort());

  vtkNew<vtkImageExtractComponents> butterworthReal;
  butterworthReal->SetInputConnection(butterworthRfft->GetOutputPort());
  butterworthReal->SetComponents(0);

  // Create actors
  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkImageMapToWindowLevelColors> idealColor;
  idealColor->SetWindow(500);
  idealColor->SetLevel(0);
  idealColor->SetInputConnection(idealReal->GetOutputPort());

  vtkNew<vtkImageActor> idealActor;
  idealActor->GetMapper()->SetInputConnection(idealColor->GetOutputPort());
  idealActor->GetProperty()->SetInterpolationTypeToNearest();

  vtkNew<vtkImageMapToWindowLevelColors> butterworthColor;
  butterworthColor->SetWindow(500);
  butterworthColor->SetLevel(0);
  butterworthColor->SetInputConnection(butterworthReal->GetOutputPort());

  vtkNew<vtkImageActor> butterworthActor;
  butterworthActor->GetMapper()->SetInputConnection(
      butterworthColor->GetOutputPort());
  butterworthActor->GetProperty()->SetInterpolationTypeToNearest();

  // Setup renderers
  vtkNew<vtkRenderer> idealRenderer;
  idealRenderer->SetViewport(0.0, 0.0, 0.5, 1.0);
  idealRenderer->AddActor(idealActor);
  idealRenderer->ResetCamera();
  idealRenderer->SetBackground(colors->GetColor3d("SlateGray").GetData());

  vtkNew<vtkRenderer> butterworthRenderer;
  butterworthRenderer->SetViewport(0.5, 0.0, 1.0, 1.0);
  butterworthRenderer->AddActor(butterworthActor);
  butterworthRenderer->SetActiveCamera(idealRenderer->GetActiveCamera());
  butterworthRenderer->SetBackground(
      colors->GetColor3d("LightSlateGray").GetData());

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->SetSize(600, 300);
  renderWindow->SetWindowName("IdealHighPass");
  renderWindow->AddRenderer(idealRenderer);
  renderWindow->AddRenderer(butterworthRenderer);

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  vtkNew<vtkInteractorStyleImage> style;

  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.