ImageText
Description¶
This example demonstrates how to draw text onto an image vtkImageData. It uses vtkImageBlend to blend the text and the image. For another example using vtkImageBlend, see CombineImages.
Seealso
DrawText for an example of drawing text, also in 2D, but into the render window rather than into a vtkImageData object.
Question
If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum
Code¶
ImageText.cxx
#include <vtkImageBlend.h>
#include <vtkImageData.h>
#include <vtkPointData.h>
#include <vtkPNGWriter.h>
#include <vtkSmartPointer.h>
#include <vtkFreeTypeTools.h>
#include <vtkTextProperty.h>
#include <vtkImageCanvasSource2D.h>
#include <vtkStdString.h>
int main(int argc, char* argv[])
{
// Create a black image with a red circle of radius 50 centered at (60, 60)
vtkSmartPointer<vtkImageCanvasSource2D> drawing =
vtkSmartPointer<vtkImageCanvasSource2D>::New();
drawing->SetScalarTypeToUnsignedChar(); // PNGWriter requires unsigned char (or unsigned short)
drawing->SetNumberOfScalarComponents(3);
drawing->SetExtent(0, 150, 0, 120, 0, 0); // xmin, xmax, ymin, ymax, zmin, zmax
drawing->SetDrawColor(0, 0, 255); // red
drawing->FillBox(0, 150, 0, 120);
drawing->SetDrawColor(255, 0, 0); // red
drawing->DrawCircle(60, 60, 50); // parameters: x, y, radius
// Create an image of text
vtkFreeTypeTools *freeType = vtkFreeTypeTools::GetInstance();
vtkSmartPointer<vtkTextProperty> textProperty = vtkSmartPointer<vtkTextProperty>::New();
textProperty->SetColor(1.0, 1.0, 0.0); // yellow
textProperty->SetFontSize(24);
textProperty->SetOrientation(25);
vtkStdString text("Test String");
vtkSmartPointer<vtkImageData> textImage = vtkSmartPointer<vtkImageData>::New();
freeType->RenderString(textProperty, text, 70, textImage.GetPointer());
// Combine the images
vtkSmartPointer<vtkImageBlend> blend =
vtkSmartPointer<vtkImageBlend>::New();
blend->AddInputConnection(drawing->GetOutputPort());
blend->AddInputData(textImage);
blend->SetOpacity(0, 1.0); // background image: 50% opaque
blend->SetOpacity(1, 1.0); // text: 100% opaque
blend->Update();
vtkSmartPointer<vtkPNGWriter> writer =
vtkSmartPointer<vtkPNGWriter>::New();
writer->SetFileName("output.png");
writer->SetInputConnection(blend->GetOutputPort());
writer->Write();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(ImageText)
find_package(VTK COMPONENTS
vtkvtkCommonCore
vtkvtkCommonDataModel
vtkvtkIOImage
vtkvtkImagingCore
vtkvtkImagingSources
vtkvtkInteractionStyle
vtkvtkRenderingContextOpenGL2
vtkvtkRenderingCore
vtkvtkRenderingFreeType
vtkvtkRenderingGL2PSOpenGL2
vtkvtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
message("Skipping ImageText: ${VTK_NOT_FOUND_MESSAGE}")
return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
# old system
include(${VTK_USE_FILE})
add_executable(ImageText MACOSX_BUNDLE ImageText.cxx )
target_link_libraries(ImageText PRIVATE ${VTK_LIBRARIES})
else ()
# include all components
add_executable(ImageText MACOSX_BUNDLE ImageText.cxx )
target_link_libraries(ImageText PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS ImageText
MODULES ${VTK_LIBRARIES}
)
endif ()
Download and Build ImageText¶
Click here to download ImageText and its CMakeLists.txt file. Once the tarball ImageText.tar has been downloaded and extracted,
cd ImageText/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:
./ImageText
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.