VTK/Examples/Cxx/Images/DrawOnAnImage
From KitwarePublic
This example draws a circle in the center of the input image.
Drawing.cxx
#include <vtkSmartPointer.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkRenderer.h> #include <vtkJPEGReader.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] << " InputFilename(jpg)" << std::endl; return EXIT_FAILURE; } // Parse input arguments std::string inputFilename = argv[1]; // Read the image vtkSmartPointer<vtkJPEGReader> jPEGReader = vtkSmartPointer<vtkJPEGReader>::New(); jPEGReader->SetFileName ( inputFilename.c_str() ); jPEGReader->Update(); vtkImageData* image = jPEGReader->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(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(jPEGReader->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 renderWindowInteractor->Initialize(); renderWindowInteractor->Start(); return EXIT_SUCCESS; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(Drawing) FIND_PACKAGE(VTK REQUIRED) INCLUDE(${VTK_USE_FILE}) ADD_EXECUTABLE(Drawing Drawing.cxx) TARGET_LINK_LIBRARIES(Drawing vtkHybrid)