Skip to content

CombiningRGBChannels

vtk-examples/Cxx/Images/CombiningRGBChannels

Description

This example creates three images of a square. Each square slightly overlaps the previous square. These three layers are then interpreted as the R, G, and B channel of an RGB image, by the use of vtkImageAppendComponents.

Note

You MUST add 3 inputs or no output will be produced.

Seealso

For the reverse operation, see ExtractComponents.

Question

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

Code

CombiningRGBChannels.cxx

#include <vtkImageActor.h>
#include <vtkImageAppendComponents.h>
#include <vtkImageCanvasSource2D.h>
#include <vtkImageMapper3D.h>
#include <vtkInteractorStyleImage.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

#include <array>

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

  std::array<double, 4> drawColor1{0, 0, 0, 0};
  std::array<double, 4> drawColor2{0, 0, 0, 0};
  auto color1 = colors->GetColor4ub("Black").GetData();
  auto color2 = colors->GetColor4ub("Red").GetData();
  for (auto i = 0; i < 4; ++i)
  {
    drawColor1[i] = color1[i];
    drawColor2[i] = color2[i];
  }
  // Set the alpha to 0
  drawColor1[3] = 0;
  drawColor2[3] = 0;

  // Create an image (will be used as the R channel)
  vtkNew<vtkImageCanvasSource2D> red;
  red->SetScalarTypeToUnsignedChar();
  red->SetNumberOfScalarComponents(1);
  red->SetExtent(0, 100, 0, 100, 0, 0);
  red->SetDrawColor(drawColor1.data());
  red->FillBox(0, 100, 0, 100);
  red->SetDrawColor(drawColor2.data());
  red->FillBox(20, 40, 20, 40);
  red->Update();

  // Create an image (will be used as the G channel)
  vtkNew<vtkImageCanvasSource2D> green;
  green->SetScalarTypeToUnsignedChar();
  green->SetNumberOfScalarComponents(1);
  green->SetExtent(0, 100, 0, 100, 0, 0);
  green->SetDrawColor(drawColor1.data());
  green->FillBox(0, 100, 0, 100);
  green->SetDrawColor(drawColor2.data());
  green->FillBox(30, 50, 30, 50);
  green->Update();

  // Create an image (will be used as the B channel)
  vtkNew<vtkImageCanvasSource2D> blue;
  blue->SetScalarTypeToUnsignedChar();
  blue->SetNumberOfScalarComponents(1);
  blue->SetExtent(0, 100, 0, 100, 0, 0);
  blue->SetDrawColor(drawColor1.data());
  blue->FillBox(0, 100, 0, 100);
  blue->SetDrawColor(drawColor2.data());
  blue->FillBox(40, 60, 40, 60);
  blue->Update();

  vtkNew<vtkImageAppendComponents> appendFilter;
  appendFilter->SetInputConnection(0, red->GetOutputPort());
  appendFilter->AddInputConnection(0, green->GetOutputPort());
  appendFilter->AddInputConnection(0, blue->GetOutputPort());
  appendFilter->Update();

  // Create actors
  vtkNew<vtkImageActor> redActor;
  redActor->GetMapper()->SetInputConnection(red->GetOutputPort());

  vtkNew<vtkImageActor> greenActor;
  greenActor->GetMapper()->SetInputConnection(green->GetOutputPort());

  vtkNew<vtkImageActor> blueActor;
  blueActor->GetMapper()->SetInputConnection(blue->GetOutputPort());

  vtkNew<vtkImageActor> combinedActor;
  combinedActor->GetMapper()->SetInputConnection(appendFilter->GetOutputPort());

  // Define viewport ranges
  // (xmin, ymin, xmax, ymax)
  double redViewport[4] = {0.0, 0.0, 0.25, 1.0};
  double greenViewport[4] = {0.25, 0.0, 0.5, 1.0};
  double blueViewport[4] = {0.5, 0.0, 0.75, 1.0};
  double combinedViewport[4] = {0.75, 0.0, 1.0, 1.0};

  // Setup renderers
  vtkNew<vtkRenderer> redRenderer;
  redRenderer->SetViewport(redViewport);
  redRenderer->AddActor(redActor);
  redRenderer->ResetCamera();
  redRenderer->SetBackground(colors->GetColor3d("Tomato").GetData());

  vtkNew<vtkRenderer> greenRenderer;
  greenRenderer->SetViewport(greenViewport);
  greenRenderer->AddActor(greenActor);
  greenRenderer->ResetCamera();
  greenRenderer->SetBackground(colors->GetColor3d("Mint").GetData());

  vtkNew<vtkRenderer> blueRenderer;
  blueRenderer->SetViewport(blueViewport);
  blueRenderer->AddActor(blueActor);
  blueRenderer->ResetCamera();
  blueRenderer->SetBackground(colors->GetColor3d("Peacock").GetData());

  vtkNew<vtkRenderer> combinedRenderer;
  combinedRenderer->SetViewport(combinedViewport);
  combinedRenderer->AddActor(combinedActor);
  combinedRenderer->ResetCamera();
  combinedRenderer->SetBackground(colors->GetColor3d("Snow").GetData());

  // Setup render window
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->SetSize(1000, 250);
  renderWindow->AddRenderer(redRenderer);
  renderWindow->AddRenderer(greenRenderer);
  renderWindow->AddRenderer(blueRenderer);
  renderWindow->AddRenderer(combinedRenderer);
  renderWindow->SetWindowName("CombiningRGBChannels");

  // 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(CombiningRGBChannels)

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

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

Download and Build CombiningRGBChannels

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

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

./CombiningRGBChannels

WINDOWS USERS

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