Skip to content

Flip

vtk-examples/Cxx/Images/Flip


Description

This example demonstrates how to flip an image over an axis.

Need a better source image (non-symmetrical!).

Other languages

See (Java)

Question

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

Code

Flip.cxx

#include <vtkImageActor.h>
#include <vtkImageCast.h>
#include <vtkImageFlip.h>
#include <vtkImageMandelbrotSource.h>
#include <vtkImageMapper3D.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> source;
  source->Update();

  vtkNew<vtkImageCast> castSource;
  castSource->SetOutputScalarTypeToUnsignedChar();
  castSource->SetInputConnection(source->GetOutputPort());
  castSource->Update();

  vtkNew<vtkImageFlip> flipXFilter;
  flipXFilter->SetFilteredAxis(0); // flip x axis
  flipXFilter->SetInputConnection(source->GetOutputPort());
  flipXFilter->Update();

  vtkNew<vtkImageCast> castXFilter;
  castXFilter->SetOutputScalarTypeToUnsignedChar();
  castXFilter->SetInputConnection(flipXFilter->GetOutputPort());
  castXFilter->Update();

  vtkNew<vtkImageFlip> flipYFilter;
  flipYFilter->SetFilteredAxis(1); // flip y axis
  flipYFilter->SetInputConnection(source->GetOutputPort());
  flipYFilter->Update();

  vtkNew<vtkImageCast> castYFilter;
  castYFilter->SetOutputScalarTypeToUnsignedChar();
  castYFilter->SetInputConnection(flipYFilter->GetOutputPort());
  castYFilter->Update();

  vtkNew<vtkImageFlip> flipZFilter;
  flipZFilter->SetFilteredAxis(2); // flip z axis
  flipZFilter->SetInputConnection(source->GetOutputPort());
  flipZFilter->Update();

  vtkNew<vtkImageCast> castZFilter;
  castZFilter->SetOutputScalarTypeToUnsignedChar();
  castZFilter->SetInputConnection(flipZFilter->GetOutputPort());
  castZFilter->Update();

  // Create actors
  vtkNew<vtkImageActor> inputActor;
  inputActor->GetMapper()->SetInputConnection(castSource->GetOutputPort());

  vtkNew<vtkImageActor> xActor;
  xActor->GetMapper()->SetInputConnection(castXFilter->GetOutputPort());

  vtkNew<vtkImageActor> yActor;
  yActor->GetMapper()->SetInputConnection(castYFilter->GetOutputPort());

  vtkNew<vtkImageActor> zActor;
  zActor->GetMapper()->SetInputConnection(castZFilter->GetOutputPort());

  // Define viewport ranges.
  // (xmin, ymin, xmax, ymax)
  double inputViewport[4] = {0.0, 0.0, 0.25, 1.0};
  double xViewport[4] = {0.25, 0.0, 0.5, 1.0};
  double yViewport[4] = {0.5, 0.0, 0.75, 1.0};
  double zViewport[4] = {0.75, 0.0, 1.0, 1.0};

  // Setup renderers
  vtkNew<vtkRenderer> inputRenderer;
  inputRenderer->SetViewport(inputViewport);
  inputRenderer->AddActor(inputActor);
  inputRenderer->ResetCamera();
  inputRenderer->SetBackground(colors->GetColor3d("Snow").GetData());

  vtkNew<vtkRenderer> xRenderer;
  xRenderer->SetViewport(xViewport);
  xRenderer->AddActor(xActor);
  xRenderer->ResetCamera();
  xRenderer->SetBackground(colors->GetColor3d("Tomato").GetData());

  vtkNew<vtkRenderer> yRenderer;
  yRenderer->SetViewport(yViewport);
  yRenderer->AddActor(yActor);
  yRenderer->ResetCamera();
  yRenderer->SetBackground(colors->GetColor3d("Mint").GetData());

  vtkNew<vtkRenderer> zRenderer;
  zRenderer->SetViewport(zViewport);
  zRenderer->AddActor(zActor);
  zRenderer->ResetCamera();
  zRenderer->SetBackground(colors->GetColor3d("Peacock").GetData());

  // Setup render window.
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->SetSize(1000, 250);
  renderWindow->AddRenderer(inputRenderer);
  renderWindow->AddRenderer(xRenderer);
  renderWindow->AddRenderer(yRenderer);
  renderWindow->AddRenderer(zRenderer);
  renderWindow->SetWindowName("Flip");

  // Setup render window interactor.
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  vtkNew<vtkInteractorStyleImage> style;

  renderWindowInteractor->SetInteractorStyle(style);

  // Render and start interaction.
  renderWindowInteractor->SetRenderWindow(renderWindow);
  renderWindow->Render();
  renderWindowInteractor->Initialize();

  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(Flip)

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

if (NOT VTK_FOUND)
  message(FATAL_ERROR "Flip: 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(Flip MACOSX_BUNDLE Flip.cxx )
  target_link_libraries(Flip PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS Flip
  MODULES ${VTK_LIBRARIES}
)

Download and Build Flip

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

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

./Flip

WINDOWS USERS

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