ImageStencil
Repository source: ImageStencil
Description¶
This example creates a red image and a green image. It creates a mask that is half on and half off. It then combines the images according to the mask using an ImageStencil.
Seealso
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
ImageStencil.cxx
#include <vtkImageActor.h>
#include <vtkImageData.h>
#include <vtkImageMapper3D.h>
#include <vtkImageStencil.h>
// #include <vtkImageStencilData.h>
#include <vtkImageToImageStencil.h>
#include <vtkInteractorStyleImage.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
namespace {
void CreateColorImage(vtkImageData*, unsigned int channel);
void CreateMask(vtkImageData*);
} // namespace
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
vtkNew<vtkImageData> image1;
CreateColorImage(image1, 0); // Create a red image.
vtkNew<vtkImageData> image2;
CreateColorImage(image2, 1); // Create a green image.
vtkNew<vtkImageData> mask;
CreateMask(mask);
// vtkNew<vtkImageStencilData> stencilData;
vtkNew<vtkImageToImageStencil> imageToImageStencil;
imageToImageStencil->SetInputData(mask);
imageToImageStencil->ThresholdByUpper(122);
vtkNew<vtkImageStencil> stencil;
stencil->SetInputConnection(2, imageToImageStencil->GetOutputPort());
stencil->ReverseStencilOn();
stencil->SetBackgroundInputData(image2);
stencil->SetInputData(image1);
stencil->Update();
// Create an actor.
vtkNew<vtkImageActor> actor;
actor->GetMapper()->SetInputConnection(stencil->GetOutputPort());
// Setup renderer.
vtkNew<vtkRenderer> renderer;
renderer->AddActor(actor);
renderer->ResetCamera();
renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());
// Setup render window.
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("ImageStencil");
// Setup render window interactor.
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
vtkNew<vtkInteractorStyleImage> style;
renderWindowInteractor->SetInteractorStyle(style);
// Render and start interaction.
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->Render();
;
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
namespace {
void CreateColorImage(vtkImageData* image, const unsigned int channel)
{
unsigned int dim = 20;
image->SetDimensions(dim, dim, 1);
image->AllocateScalars(VTK_UNSIGNED_CHAR, 3);
for (unsigned int x = 0; x < dim; x++)
{
for (unsigned int y = 0; y < dim; y++)
{
unsigned char* pixel =
static_cast<unsigned char*>(image->GetScalarPointer(x, y, 0));
pixel[0] = 0;
pixel[1] = 0;
pixel[2] = 0;
pixel[channel] = 255;
}
}
image->Modified();
}
void CreateMask(vtkImageData* image)
{
unsigned int dim = 20;
image->SetDimensions(dim, dim, 1);
image->AllocateScalars(VTK_UNSIGNED_CHAR, 1);
for (unsigned int x = 0; x < dim; x++)
{
for (unsigned int y = 0; y < dim; y++)
{
unsigned char* pixel =
static_cast<unsigned char*>(image->GetScalarPointer(x, y, 0));
if (x < dim / 2)
{
pixel[0] = 0;
}
else
{
pixel[0] = 255;
}
}
}
image->Modified();
}
} // namespace
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(ImageStencil)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
ImagingCore
ImagingStencil
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "ImageStencil: 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(ImageStencil MACOSX_BUNDLE ImageStencil.cxx )
target_link_libraries(ImageStencil PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS ImageStencil
MODULES ${VTK_LIBRARIES}
)
Download and Build ImageStencil¶
Click here to download ImageStencil and its CMakeLists.txt file. Once the tarball ImageStencil.tar has been downloaded and extracted,
cd ImageStencil/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:
./ImageStencil
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.