DrawOnAnImage
VTKEx/Cxx/Images/DrawOnAnImage
Description¶
This example draws a circle in the center of the input image using vtkImageCanvasSource2D 's DrawCircle method.
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¶
DrawOnAnImage.cxx
#include <vtkSmartPointer.h>
#include <vtkImageReader2Factory.h>
#include <vtkImageReader2.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkImageData.h>
#include <vtkImageMapper3D.h>
#include <vtkImageViewer2.h>
#include <vtkImageCanvasSource2D.h>
#include <vtkImageBlend.h>
int main ( int argc, char* argv[] )
{
//Verify input arguments
if ( argc != 2 )
{
std::cout << "Usage: " << argv[0]
<< " InputImageFilename" << std::endl;
return EXIT_FAILURE;
}
// Read the image
vtkSmartPointer<vtkImageReader2Factory> readerFactory =
vtkSmartPointer<vtkImageReader2Factory>::New();
vtkSmartPointer<vtkImageReader2> imgReader;
imgReader.TakeReference(
readerFactory->CreateImageReader2(argv[1]));
imgReader->SetFileName(argv[1]);
imgReader->Update();
vtkImageData* image = imgReader->GetOutput();
// Find center of image
int center[2];
center[0] = (image->GetExtent()[1] + image->GetExtent()[0]) / 2;
center[1] = (image->GetExtent()[3] + image->GetExtent()[2]) / 2;
// Pick a radius for the circle
int radius;
radius = (image->GetExtent()[1] < image->GetExtent()[3]) ?
image->GetExtent()[1] * 2 / 5 : image->GetExtent()[3] * 2 / 5;
// Draw a circle in the center of the image
vtkSmartPointer<vtkImageCanvasSource2D> drawing =
vtkSmartPointer<vtkImageCanvasSource2D>::New();
drawing->SetNumberOfScalarComponents(3);
drawing->SetScalarTypeToUnsignedChar();
drawing->SetExtent(image->GetExtent());
drawing->SetDrawColor(0.0, 0.0, 0.0);
drawing->FillBox(image->GetExtent()[0], image->GetExtent()[1],
image->GetExtent()[2], image->GetExtent()[3]);
drawing->SetDrawColor(255.0, 255.0, 255.0);
drawing->DrawCircle(center[0], center[1], radius);
// Combine the images (blend takes multiple connections on the 0th
// input port)
vtkSmartPointer<vtkImageBlend> blend =
vtkSmartPointer<vtkImageBlend>::New();
blend->AddInputConnection(imgReader->GetOutputPort());
blend->AddInputConnection(drawing->GetOutputPort());
blend->SetOpacity(0,.6);
blend->SetOpacity(1,.4);
// Display the result
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkImageViewer2> imageViewer =
vtkSmartPointer<vtkImageViewer2>::New();
imageViewer->SetInputConnection(blend->GetOutputPort());
imageViewer->SetSize(640, 512);
imageViewer->SetupInteractor(renderWindowInteractor);
imageViewer->GetRenderer()->ResetCamera();
imageViewer->GetRenderer()->SetBackground(1,0,0); //red
imageViewer->GetRenderWindow()->Render();
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(DrawOnAnImage)
find_package(VTK COMPONENTS
vtkvtkCommonCore
vtkvtkCommonDataModel
vtkvtkIOImage
vtkvtkImagingCore
vtkvtkImagingSources
vtkvtkInteractionImage
vtkvtkInteractionStyle
vtkvtkRenderingContextOpenGL2
vtkvtkRenderingCore
vtkvtkRenderingFreeType
vtkvtkRenderingGL2PSOpenGL2
vtkvtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
message("Skipping DrawOnAnImage: ${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(DrawOnAnImage MACOSX_BUNDLE DrawOnAnImage.cxx )
target_link_libraries(DrawOnAnImage PRIVATE ${VTK_LIBRARIES})
else ()
# include all components
add_executable(DrawOnAnImage MACOSX_BUNDLE DrawOnAnImage.cxx )
target_link_libraries(DrawOnAnImage PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS DrawOnAnImage
MODULES ${VTK_LIBRARIES}
)
endif ()
Download and Build DrawOnAnImage¶
Click here to download DrawOnAnImage and its CMakeLists.txt file. Once the tarball DrawOnAnImage.tar has been downloaded and extracted,
cd DrawOnAnImage/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:
./DrawOnAnImage
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.