BorderPixelSize
VTKExamples/Cxx/Images/BorderPixelSize
Code¶
BorderPixelSize.cxx
#include <vtkImageData.h> #include <vtkSmartPointer.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkInteractorStyleImage.h> #include <vtkRenderer.h> #include <vtkImageMapper.h> #include <vtkImageSliceMapper.h> #include <vtkImageSlice.h> #include <vtkImageProperty.h> static void CreateRandomImage(vtkImageData* image, const unsigned int dimension); int main(int, char *[]) { // Big image vtkSmartPointer<vtkImageData> image = vtkSmartPointer<vtkImageData>::New(); CreateRandomImage(image, 50); vtkSmartPointer<vtkImageSliceMapper> imageSliceMapper = vtkSmartPointer<vtkImageSliceMapper>::New(); imageSliceMapper->SetInputData(image); imageSliceMapper->BorderOn(); // This line tells the mapper to draw the full border pixels. vtkSmartPointer<vtkImageSlice> imageSlice = vtkSmartPointer<vtkImageSlice>::New(); imageSlice->SetMapper(imageSliceMapper); imageSlice->GetProperty()->SetInterpolationTypeToNearest(); vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New(); renderer->AddViewProp(imageSlice); renderer->ResetCamera(); vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->AddRenderer(renderer); vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); vtkSmartPointer<vtkInteractorStyleImage> style = vtkSmartPointer<vtkInteractorStyleImage>::New(); renderWindowInteractor->SetInteractorStyle(style); renderWindowInteractor->SetRenderWindow(renderWindow); renderWindowInteractor->Initialize(); renderWindowInteractor->Start(); return EXIT_SUCCESS; } void CreateRandomImage(vtkImageData* image, const unsigned int dimension) { image->SetDimensions(dimension, dimension, 1); image->SetOrigin(.5,.5,0); image->AllocateScalars(VTK_UNSIGNED_CHAR,3); for(unsigned int x = 0; x < dimension; x++) { for(unsigned int y = 0; y < dimension; y++) { unsigned char* pixel = static_cast<unsigned char*>(image->GetScalarPointer(x,y,0)); pixel[0] = rand() % 255; pixel[1] = rand() % 255; pixel[2] = rand() % 255; } } image->Modified(); }
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR) project(BorderPixelSize) find_package(VTK COMPONENTS vtkCommonCore vtkCommonDataModel vtkInteractionStyle vtkRenderingCore vtkRenderingFreeType vtkRenderingOpenGL2 QUIET) if (NOT VTK_FOUND) message("Skipping BorderPixelSize: ${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(BorderPixelSize MACOSX_BUNDLE BorderPixelSize.cxx ) target_link_libraries(BorderPixelSize PRIVATE ${VTK_LIBRARIES}) else () # include all components add_executable(BorderPixelSize MACOSX_BUNDLE BorderPixelSize.cxx ) target_link_libraries(BorderPixelSize PRIVATE ${VTK_LIBRARIES}) # vtk_module_autoinit is needed vtk_module_autoinit( TARGETS BorderPixelSize MODULES ${VTK_LIBRARIES} ) endif ()
Download and Build BorderPixelSize¶
Click here to download BorderPixelSize and its CMakeLists.txt file. Once the tarball BorderPixelSize.tar has been downloaded and extracted,
cd BorderPixelSize/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:
./BorderPixelSize
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.