Colored2DImageFusion
Repository source: Colored2DImageFusion
Description¶
This example use the vtkImageBlend class for a simple fusion of two 2D images with different color maps for each of the images.
- Contributed by: Tim Hutz
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
Colored2DImageFusion.cxx
#include <vtkActor.h>
#include <vtkDataSetMapper.h>
#include <vtkImageBlend.h>
#include <vtkImageMapToColors.h>
#include <vtkImageReader2.h>
#include <vtkImageReader2Factory.h>
#include <vtkLookupTable.h>
#include <vtkNamedColors.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
int main(int argc, char* argv[])
{
vtkNew<vtkNamedColors> colors;
// Parse input arguments.
if (argc < 3)
{
std::cerr << "Usage: " << argv[0]
<< " Input1Filename Input2Filename e.g. Ox.jpg Gourds2.jpg"
<< std::endl;
return EXIT_FAILURE;
}
vtkNew<vtkLookupTable> imgFirstColorMap;
imgFirstColorMap->SetRange(0.0, 255.0);
imgFirstColorMap->SetHueRange(0.0, 0.1);
imgFirstColorMap->SetValueRange(0.4, 0.8);
imgFirstColorMap->Build();
vtkNew<vtkLookupTable> imgSecondColorMap;
imgSecondColorMap->SetRange(0.0, 255.0);
imgSecondColorMap->SetHueRange(0.67, 0.68);
imgSecondColorMap->SetValueRange(0.4, 0.8);
imgSecondColorMap->Build();
vtkNew<vtkImageReader2Factory> readerFactory;
vtkSmartPointer<vtkImageReader2> imgReader;
imgReader.TakeReference(readerFactory->CreateImageReader2(argv[1]));
imgReader->SetFileName(argv[1]);
vtkSmartPointer<vtkImageReader2> imgReaderMoving;
imgReaderMoving.TakeReference(readerFactory->CreateImageReader2(argv[2]));
imgReaderMoving->SetFileName(argv[2]);
vtkNew<vtkImageMapToColors> firstColorMapper;
firstColorMapper->SetInputConnection(imgReader->GetOutputPort());
firstColorMapper->SetLookupTable(imgFirstColorMap);
vtkNew<vtkImageMapToColors> secondColorMapper;
secondColorMapper->SetInputConnection(imgReaderMoving->GetOutputPort());
secondColorMapper->SetLookupTable(imgSecondColorMap);
vtkNew<vtkImageBlend> imgBlender;
imgBlender->SetOpacity(0, 0.5);
imgBlender->SetOpacity(1, 0.5);
imgBlender->AddInputConnection(firstColorMapper->GetOutputPort());
imgBlender->AddInputConnection(secondColorMapper->GetOutputPort());
vtkNew<vtkDataSetMapper> imgDataSetMapper;
imgDataSetMapper->SetInputConnection(imgBlender->GetOutputPort());
vtkNew<vtkActor> imgActor;
imgActor->SetMapper(imgDataSetMapper);
vtkNew<vtkRenderer> imgRenderer;
imgRenderer->AddActor(imgActor);
imgRenderer->SetBackground(colors->GetColor3d("BurlyWood").GetData());
vtkNew<vtkRenderWindow> imgRenderWindow;
imgRenderWindow->AddRenderer(imgRenderer);
imgRenderWindow->SetWindowName("Colored2DImageFusion");
vtkNew<vtkRenderWindowInteractor> imgInteractor;
imgInteractor->SetRenderWindow(imgRenderWindow);
imgRenderWindow->Render();
imgInteractor->Initialize();
imgInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(Colored2DImageFusion)
find_package(VTK COMPONENTS
CommonColor
CommonCore
IOImage
ImagingCore
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "Colored2DImageFusion: 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(Colored2DImageFusion MACOSX_BUNDLE Colored2DImageFusion.cxx )
target_link_libraries(Colored2DImageFusion PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS Colored2DImageFusion
MODULES ${VTK_LIBRARIES}
)
Download and Build Colored2DImageFusion¶
Click here to download Colored2DImageFusion and its CMakeLists.txt file. Once the tarball Colored2DImageFusion.tar has been downloaded and extracted,
cd Colored2DImageFusion/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:
./Colored2DImageFusion
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.