ImageRotate
Repository source: ImageRotate
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
ImageRotate.cxx
#include <vtkImageData.h>
#include <vtkImageReader2.h>
#include <vtkImageReader2Factory.h>
#include <vtkImageReslice.h>
#include <vtkImageViewer2.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkTransform.h>
int main(int argc, char* argv[])
{
vtkNew<vtkNamedColors> colors;
// Verify command line arguments.
if (argc < 2)
{
std::cout << "Usage: " << argv[0] << " InputFilename e.g. Gourds.png"
<< std::endl;
return EXIT_FAILURE;
}
double angle = 45;
if (argc > 2)
{
angle = atof(argv[2]);
}
// Read file
vtkNew<vtkImageReader2Factory> readerFactory;
vtkSmartPointer<vtkImageReader2> reader;
reader.TakeReference(readerFactory->CreateImageReader2(argv[1]));
reader->SetFileName(argv[1]);
reader->Update();
double bounds[6];
reader->GetOutput()->GetBounds(bounds);
// Rotate about the center of the image.
vtkNew<vtkTransform> transform;
// Compute the center of the image.
double center[3];
center[0] = (bounds[1] + bounds[0]) / 2.0;
center[1] = (bounds[3] + bounds[2]) / 2.0;
center[2] = (bounds[5] + bounds[4]) / 2.0;
// Rotate about the center
transform->Translate(center[0], center[1], center[2]);
transform->RotateWXYZ(angle, 0, 0, 1);
transform->Translate(-center[0], -center[1], -center[2]);
// Reslice does all of the work.
vtkNew<vtkImageReslice> reslice;
reslice->SetInputConnection(reader->GetOutputPort());
reslice->SetResliceTransform(transform);
reslice->SetInterpolationModeToCubic();
reslice->SetOutputSpacing(reader->GetOutput()->GetSpacing()[0],
reader->GetOutput()->GetSpacing()[1],
reader->GetOutput()->GetSpacing()[2]);
reslice->SetOutputOrigin(reader->GetOutput()->GetOrigin()[0],
reader->GetOutput()->GetOrigin()[1],
reader->GetOutput()->GetOrigin()[2]);
reslice->SetOutputExtent(
reader->GetOutput()
->GetExtent()); // Use a larger extent than the
// original image's to prevent clipping.
// Visualize
vtkNew<vtkImageViewer2> imageViewer;
imageViewer->SetInputConnection(reslice->GetOutputPort());
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
imageViewer->SetupInteractor(renderWindowInteractor);
imageViewer->GetRenderer()->SetBackground(
colors->GetColor3d("RoyalBlue").GetData());
imageViewer->GetRenderWindow()->SetWindowName("ImageRotate");
imageViewer->Render();
imageViewer->GetRenderer()->ResetCamera();
imageViewer->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(ImageRotate)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
CommonTransforms
IOImage
ImagingCore
InteractionImage
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "ImageRotate: 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(ImageRotate MACOSX_BUNDLE ImageRotate.cxx )
target_link_libraries(ImageRotate PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS ImageRotate
MODULES ${VTK_LIBRARIES}
)
Download and Build ImageRotate¶
Click here to download ImageRotate and its CMakeLists.txt file. Once the tarball ImageRotate.tar has been downloaded and extracted,
cd ImageRotate/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:
./ImageRotate
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.