Skip to content

ImageIdealHighPass

Repository source: ImageIdealHighPass


Question

If you have a question about this example, please use the VTK Discourse Forum

Code

ImageIdealHighPass.cxx

#include <vtkImageCast.h>
#include <vtkImageData.h>
#include <vtkImageEllipsoidSource.h>
#include <vtkImageExtractComponents.h>
#include <vtkImageFFT.h>
#include <vtkImageIdealHighPass.h>
#include <vtkImageMandelbrotSource.h>
#include <vtkImageRFFT.h>
#include <vtkImageShiftScale.h>
#include <vtkImageSlice.h>
#include <vtkImageSliceMapper.h>
#include <vtkInteractorStyleImage.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

int main(int, char*[])
{
  vtkNew<vtkNamedColors> colors;

  // Create an image
  vtkNew<vtkImageMandelbrotSource> mandelbrotSource;
  mandelbrotSource->Update();

  // Display the original image
  vtkNew<vtkImageCast> originalCastFilter;
  originalCastFilter->SetInputConnection(mandelbrotSource->GetOutputPort());
  originalCastFilter->SetOutputScalarTypeToUnsignedChar();
  originalCastFilter->Update();

  vtkNew<vtkImageSliceMapper> originalSliceMapper;
  originalSliceMapper->SetInputConnection(originalCastFilter->GetOutputPort());

  vtkNew<vtkImageSlice> originalSlice;
  originalSlice->SetMapper(originalSliceMapper);

  // Compute the FFT of the image
  vtkNew<vtkImageFFT> fftFilter;
  fftFilter->SetInputConnection(originalCastFilter->GetOutputPort());
  fftFilter->Update();

  // High pass filter the FFT
  vtkNew<vtkImageIdealHighPass> highPassFilter;
  highPassFilter->SetInputConnection(fftFilter->GetOutputPort());
  highPassFilter->SetXCutOff(.1);
  highPassFilter->SetYCutOff(.1);
  highPassFilter->Update();

  // Compute the IFFT of the high pass filtered image
  vtkNew<vtkImageRFFT> rfftFilter;
  rfftFilter->SetInputConnection(highPassFilter->GetOutputPort());
  rfftFilter->Update();

  vtkNew<vtkImageExtractComponents> extractRealFilter;
  extractRealFilter->SetInputConnection(rfftFilter->GetOutputPort());
  extractRealFilter->SetComponents(0);
  extractRealFilter->Update();

  vtkNew<vtkImageShiftScale> shiftScaleFilter;
  shiftScaleFilter->SetOutputScalarTypeToUnsignedChar();
  shiftScaleFilter->SetInputConnection(extractRealFilter->GetOutputPort());
  shiftScaleFilter->SetShift(
      -1.0f *
      extractRealFilter->GetOutput()
          ->GetScalarRange()[0]); // brings the lower bound to 0
  float oldRange = extractRealFilter->GetOutput()->GetScalarRange()[1] -
      extractRealFilter->GetOutput()->GetScalarRange()[0];
  float newRange =
      100; // We want the output [0,100] (the same as the original image)
  shiftScaleFilter->SetScale(newRange / oldRange);
  shiftScaleFilter->Update();

  // Cast the output back to unsigned char
  vtkNew<vtkImageCast> outputCastFilter;
  outputCastFilter->SetInputConnection(shiftScaleFilter->GetOutputPort());
  outputCastFilter->SetOutputScalarTypeToUnsignedChar();
  outputCastFilter->Update();

  // Display the high pass filtered image
  vtkNew<vtkImageSliceMapper> highPassSliceMapper;
  highPassSliceMapper->SetInputConnection(outputCastFilter->GetOutputPort());

  vtkNew<vtkImageSlice> highPassSlice;
  highPassSlice->SetMapper(highPassSliceMapper);

  // Define viewport ranges
  // (xmin, ymin, xmax, ymax)
  double originalViewport[4] = {0.0, 0.0, 0.5, 1.0};
  double highPassViewport[4] = {0.5, 0.0, 1.0, 1.0};

  // Setup renderers
  vtkNew<vtkRenderer> originalRenderer;
  originalRenderer->SetViewport(originalViewport);
  originalRenderer->AddViewProp(originalSlice);
  originalRenderer->ResetCamera();
  originalRenderer->SetBackground(.4, .5, .6);
  originalRenderer->SetBackground(colors->GetColor3d("Sienna").GetData());

  vtkNew<vtkRenderer> highPassRenderer;
  highPassRenderer->SetViewport(highPassViewport);
  highPassRenderer->AddViewProp(highPassSlice);
  highPassRenderer->ResetCamera();
  highPassRenderer->SetBackground(.4, .5, .7);
  highPassRenderer->SetBackground(colors->GetColor3d("RoyalBlue").GetData());

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->SetSize(600, 300);
  renderWindow->AddRenderer(originalRenderer);
  renderWindow->AddRenderer(highPassRenderer);
  renderWindow->SetWindowName("ImageIdealHighPass");

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

  renderWindowInteractor->SetInteractorStyle(style);

  renderWindowInteractor->SetRenderWindow(renderWindow);
  renderWindow->Render();
  renderWindowInteractor->Initialize();

  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(ImageIdealHighPass)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  ImagingCore
  ImagingFourier
  ImagingSources
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "ImageIdealHighPass: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(ImageIdealHighPass MACOSX_BUNDLE ImageIdealHighPass.cxx )
  target_link_libraries(ImageIdealHighPass PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS ImageIdealHighPass
  MODULES ${VTK_LIBRARIES}
)

Download and Build ImageIdealHighPass

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

cd ImageIdealHighPass/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:

./ImageIdealHighPass

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.