Skip to content


# GaussianSmooth

VTKEx/Cxx/ImageProcessing/GaussianSmooth

### Description

Low-pass filters can be implemented as convolution with a Gaussian kernel. The Gaussian kernel displayed on top has been magnified for this figure.

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 VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

### Code

GaussianSmooth.cxx

    #include <vtkImageActor.h>
    #include <vtkImageCast.h>
    #include <vtkImageData.h>
    #include <vtkImageGaussianSmooth.h>
    #include <vtkImageMapper3D.h>
    #include <vtkImageNoiseSource.h>
    #include <vtkImageReader2.h>
    #include <vtkImageReader2Factory.h>
    #include <vtkInteractorStyleImage.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();

      // Process the
      vtkSmartPointer<vtkImageCast> cast =
        vtkSmartPointer<vtkImageCast>::New();
      cast->SetInputConnection(reader->GetOutputPort());
      cast->SetOutputScalarTypeToFloat();

      vtkSmartPointer<vtkImageGaussianSmooth> filter =
        vtkSmartPointer<vtkImageGaussianSmooth>::New();
      filter->SetDimensionality(2);
      filter->SetInputConnection(cast->GetOutputPort());
      filter->SetStandardDeviations(4.0, 4.0);
      filter->SetRadiusFactors(2.0, 2.0);

      // Create actors
      vtkSmartPointer<vtkNamedColors> colors =
        vtkSmartPointer<vtkNamedColors>::New();

      vtkSmartPointer<vtkImageActor> originalActor =
        vtkSmartPointer<vtkImageActor>::New();
      originalActor->GetMapper()->SetInputConnection(
        reader->GetOutputPort());

      vtkSmartPointer<vtkImageActor> filteredActor =
        vtkSmartPointer<vtkImageActor>::New();
      filteredActor->GetMapper()->SetInputConnection(
        filter->GetOutputPort());

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

      // Setup renderers
      vtkSmartPointer<vtkRenderer> originalRenderer =
        vtkSmartPointer<vtkRenderer>::New();
      originalRenderer->SetViewport(originalViewport);
      originalRenderer->AddActor(originalActor);
      originalRenderer->ResetCamera();
      originalRenderer->SetBackground(colors->GetColor3d("SlateGray").GetData());

      vtkSmartPointer<vtkRenderer> filteredRenderer =
        vtkSmartPointer<vtkRenderer>::New();
      filteredRenderer->SetViewport(filteredViewport);
      filteredRenderer->AddActor(filteredActor);
      filteredRenderer->ResetCamera();
      filteredRenderer->SetBackground(colors->GetColor3d("LightSlateGray").GetData());

      vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->SetSize(600, 300);
      renderWindow->AddRenderer(originalRenderer);
      renderWindow->AddRenderer(filteredRenderer);

      vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      vtkSmartPointer<vtkInteractorStyleImage> style =
        vtkSmartPointer<vtkInteractorStyleImage>::New();

      renderWindowInteractor->SetInteractorStyle(style);

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

      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(GaussianSmooth)

    find_package(VTK COMPONENTS
      vtkvtkCommonColor
      vtkvtkCommonCore
      vtkvtkCommonDataModel
      vtkvtkIOImage
      vtkvtkImagingCore
      vtkvtkImagingGeneral
      vtkvtkImagingSources
      vtkvtkInteractionStyle
      vtkvtkRenderingContextOpenGL2
      vtkvtkRenderingCore
      vtkvtkRenderingFreeType
      vtkvtkRenderingGL2PSOpenGL2
      vtkvtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping GaussianSmooth: ${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(GaussianSmooth MACOSX_BUNDLE GaussianSmooth.cxx )
      target_link_libraries(GaussianSmooth PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(GaussianSmooth MACOSX_BUNDLE GaussianSmooth.cxx )
      target_link_libraries(GaussianSmooth PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS GaussianSmooth
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build GaussianSmooth

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

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

    ./GaussianSmooth

WINDOWS USERS

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