Skip to content

RGBToHSI

Repository source: RGBToHSI


Description

Convert RGB channels to HSI channels. See this article for a description of the HSI colormodel.

Seealso

RGBToYIQ and RGBToHSV

Question

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

Code

RGBToHSI.cxx

#include <vtkCamera.h>
#include <vtkImageActor.h>
#include <vtkImageExtractComponents.h>
#include <vtkImageMapper3D.h>
#include <vtkImageRGBToHSI.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[])
{
  vtkNew<vtkNamedColors> colors;

  // Verify command line arguments.
  if (argc != 2)
  {
    std::cerr << "Usage: " << argv[0] << "image e.g. Gourds2.jpg" << std::endl;
    return EXIT_FAILURE;
  }

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

  vtkNew<vtkImageRGBToHSI> hsiFilter;
  hsiFilter->SetInputConnection(reader->GetOutputPort());

  vtkNew<vtkImageExtractComponents> extractHFilter;
  extractHFilter->SetInputConnection(hsiFilter->GetOutputPort());
  extractHFilter->SetComponents(0);

  vtkNew<vtkImageExtractComponents> extractSFilter;
  extractSFilter->SetInputConnection(hsiFilter->GetOutputPort());
  extractSFilter->SetComponents(1);

  vtkNew<vtkImageExtractComponents> extractIFilter;
  extractIFilter->SetInputConnection(hsiFilter->GetOutputPort());
  extractIFilter->SetComponents(2);

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

  vtkNew<vtkImageActor> hActor;
  hActor->GetMapper()->SetInputConnection(extractHFilter->GetOutputPort());

  vtkNew<vtkImageActor> sActor;
  sActor->GetMapper()->SetInputConnection(extractSFilter->GetOutputPort());

  vtkNew<vtkImageActor> iActor;
  iActor->GetMapper()->SetInputConnection(extractIFilter->GetOutputPort());

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

  // Shared camera.
  vtkNew<vtkCamera> sharedCamera;

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

  vtkNew<vtkRenderer> hRenderer;
  hRenderer->SetViewport(hViewport);
  hRenderer->AddActor(hActor);
  hRenderer->SetActiveCamera(sharedCamera);
  hRenderer->SetBackground(colors->GetColor3d("MistyRose").GetData());

  vtkNew<vtkRenderer> sRenderer;
  sRenderer->SetViewport(sViewport);
  sRenderer->AddActor(sActor);
  sRenderer->SetActiveCamera(sharedCamera);
  sRenderer->SetBackground(colors->GetColor3d("LavenderBlush").GetData());

  vtkNew<vtkRenderer> iRenderer;
  iRenderer->SetViewport(iViewport);
  iRenderer->AddActor(iActor);
  iRenderer->SetActiveCamera(sharedCamera);
  iRenderer->SetBackground(colors->GetColor3d("Lavender").GetData());

  // Setup render window.
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->SetSize(1000, 250);
  renderWindow->SetWindowName("RGBToHSI");
  renderWindow->AddRenderer(inputRenderer);
  renderWindow->AddRenderer(hRenderer);
  renderWindow->AddRenderer(sRenderer);
  renderWindow->AddRenderer(iRenderer);
  inputRenderer->ResetCamera();

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

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

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

Download and Build RGBToHSI

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

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

./RGBToHSI

WINDOWS USERS

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