ResizeImage
Repository source: ResizeImage
Description¶
Resize an image using a sinc interpolator. Without command line arguments, the example resizes a synthetic image. An image file can be passed on the command lines. The new dimensions can also be passed as well as an integer specifying the window for sinc interpolator. See vtkImageSincInterpolator for details. A -1 turns off interpolation.
Several window functions are provided. See this article for a description of window functions.
vtkImageResize maintains the physical size of the image.
Note
This example was inspired by a question asked by Qiang Wang.
Seealso
The paper "Some windows with very good sidelobe behavior" describes the windows implemented in vtkImageSincInterpolator.
Other languages
See (PythonicAPI)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
ResizeImage.cxx
#include <vtkCamera.h>
#include <vtkImageActor.h>
#include <vtkImageCanvasSource2D.h>
#include <vtkImageData.h>
#include <vtkImageMapper3D.h>
#include <vtkImageReader2.h>
#include <vtkImageReader2Factory.h>
#include <vtkImageResize.h>
#include <vtkImageSincInterpolator.h>
#include <vtkInteractorStyleImage.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <array>
int main(int argc, char* argv[])
{
vtkNew<vtkNamedColors> colors;
vtkSmartPointer<vtkImageData> imageData;
int newSize[2] = {200, 10};
int windowFunction = 0;
// Verify input arguments
// e.g. Gourds2.jpg 1280 1024 5
if (argc > 1)
{
// Read the image
vtkNew<vtkImageReader2Factory> readerFactory;
vtkSmartPointer<vtkImageReader2> reader;
reader.TakeReference(readerFactory->CreateImageReader2(argv[1]));
reader->SetFileName(argv[1]);
reader->Update();
imageData = reader->GetOutput();
if (argc > 3)
{
newSize[0] = atoi(argv[2]);
newSize[1] = atoi(argv[3]);
}
if (argc > 4)
{
windowFunction = atoi(argv[4]);
}
}
else
{
std::array<double, 3> drawColor1{0, 0, 0};
auto color1 = colors->GetColor3ub("Gray").GetData();
std::array<double, 3> drawColor2{0, 0, 0};
auto color2 = colors->GetColor3ub("Aquamarine").GetData();
std::array<double, 3> drawColor3{0, 0, 0};
auto color3 = colors->GetColor3ub("Violet").GetData();
for (auto i = 0; i < 3; ++i)
{
drawColor1[i] = color1[i];
drawColor2[i] = color2[i];
drawColor3[i] = color3[i];
}
vtkNew<vtkImageCanvasSource2D> canvasSource;
canvasSource->SetExtent(0, 100, 0, 100, 0, 0);
canvasSource->SetScalarTypeToUnsignedChar();
canvasSource->SetNumberOfScalarComponents(3);
canvasSource->SetDrawColor(drawColor1.data());
canvasSource->FillBox(0, 100, 0, 100);
canvasSource->SetDrawColor(drawColor2.data());
canvasSource->FillTriangle(10, 10, 25, 10, 25, 25);
canvasSource->SetDrawColor(drawColor3.data());
canvasSource->FillTube(75, 75, 0, 75, 5.0);
canvasSource->Update();
imageData = canvasSource->GetOutput();
}
vtkNew<vtkImageSincInterpolator> interpolator;
interpolator->UseWindowParameterOn();
if (windowFunction >= 0 && windowFunction <= 10)
{
interpolator->SetWindowFunction(windowFunction);
}
vtkNew<vtkImageResize> resize;
resize->SetInputData(imageData);
resize->SetInterpolator(interpolator);
resize->SetOutputDimensions(newSize[0], newSize[1], 1);
resize->InterpolateOn();
if (windowFunction < 0)
{
resize->InterpolateOff();
std::cout << "Using nearest neighbor interpolation" << std::endl;
;
}
else
{
std::cout << "Using window function : "
<< interpolator->GetWindowFunctionAsString() << std::endl;
;
}
// Create an image actor to display the image
vtkNew<vtkImageActor> imageActor;
imageActor->GetMapper()->SetInputConnection(resize->GetOutputPort());
imageActor->InterpolateOff();
// Setup renderer
vtkNew<vtkRenderer> renderer;
renderer->AddActor(imageActor);
renderer->SetBackground(colors->GetColor3d("Burlywood").GetData());
renderer->ResetCamera();
renderer->GetActiveCamera()->Dolly(5.0);
renderer->ResetCameraClippingRange();
// Setup render window
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(1280, 1024);
renderWindow->SetWindowName("ResizeImage");
// Setup render window interactor
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
vtkNew<vtkInteractorStyleImage> style;
renderWindowInteractor->SetInteractorStyle(style);
// Render and start interaction
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->Render();
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(ResizeImage)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
IOImage
ImagingCore
ImagingSources
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "ResizeImage: 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(ResizeImage MACOSX_BUNDLE ResizeImage.cxx )
target_link_libraries(ResizeImage PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS ResizeImage
MODULES ${VTK_LIBRARIES}
)
Download and Build ResizeImage¶
Click here to download ResizeImage and its CMakeLists.txt file. Once the tarball ResizeImage.tar has been downloaded and extracted,
cd ResizeImage/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:
./ResizeImage
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.