CenterAnImage
Repository source: CenterAnImage
Description¶
This moves (0,0) from the bottom left corner of the image to the center of the image.
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
CenterAnImage.cxx
#include <vtkBorderRepresentation.h>
#include <vtkBorderWidget.h>
#include <vtkCommand.h>
#include <vtkImageActor.h>
#include <vtkImageChangeInformation.h>
#include <vtkImageData.h>
#include <vtkImageMapper3D.h>
#include <vtkImageReader2.h>
#include <vtkImageReader2Factory.h>
#include <vtkInteractorStyleImage.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty2D.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <iomanip>
#include <iostream>
#include <sstream>
namespace {
class vtkBorderCallback : public vtkCommand
{
public:
vtkBorderCallback()
{
}
static vtkBorderCallback* New()
{
return new vtkBorderCallback;
}
virtual void Execute(vtkObject* caller, unsigned long, void*);
void SetRenderer(vtkSmartPointer<vtkRenderer> renderer)
{
this->Renderer = renderer;
}
void SetImageActor(vtkSmartPointer<vtkImageActor> actor)
{
this->ImageActor = actor;
}
private:
vtkSmartPointer<vtkRenderer> Renderer;
vtkSmartPointer<vtkImageActor> ImageActor;
};
} // namespace
int main(int argc, char* argv[])
{
vtkNew<vtkNamedColors> colors;
if (argc != 2)
{
std::cerr << "Usage: " << argv[0]
<< " Required parameters: Filename e.g. Ox.jpg" << std::endl;
return EXIT_FAILURE;
}
// Read the image.
vtkNew<vtkImageReader2Factory> readerFactory;
vtkSmartPointer<vtkImageReader2> imageReader;
imageReader.TakeReference(readerFactory->CreateImageReader2(argv[1]));
imageReader->SetFileName(argv[1]);
imageReader->Update();
// Shift the image center to (0,0).
int dims[3];
imageReader->GetOutput()->GetDimensions(dims);
vtkNew<vtkImageChangeInformation> changeInformation;
changeInformation->SetInputConnection(imageReader->GetOutputPort());
changeInformation->CenterImageOn();
changeInformation->Update();
vtkSmartPointer<vtkImageData> image = changeInformation->GetOutput();
vtkNew<vtkImageActor> imageActor;
imageActor->GetMapper()->SetInputData(image);
vtkNew<vtkRenderWindow> renderWindow;
vtkNew<vtkRenderWindowInteractor> interactor;
vtkNew<vtkInteractorStyleImage> style;
interactor->SetInteractorStyle(style);
vtkNew<vtkBorderWidget> borderWidget;
borderWidget->SetInteractor(interactor);
static_cast<vtkBorderRepresentation*>(borderWidget->GetRepresentation())
->GetBorderProperty()
->SetColor(colors->GetColor3d("Chartreuse").GetData());
borderWidget->SelectableOff();
interactor->SetRenderWindow(renderWindow);
// Setup both renderers.
vtkNew<vtkRenderer> renderer;
renderer->SetBackground(colors->GetColor3d("Peru").GetData());
renderWindow->AddRenderer(renderer);
renderer->AddActor(imageActor);
renderer->ResetCamera();
vtkNew<vtkBorderCallback> borderCallback;
borderCallback->SetRenderer(renderer);
borderCallback->SetImageActor(imageActor);
borderWidget->AddObserver(vtkCommand::InteractionEvent, borderCallback);
borderWidget->On();
renderWindow->SetWindowName("CenterAnImage");
renderWindow->Render();
interactor->Start();
return EXIT_SUCCESS;
}
namespace {
void vtkBorderCallback::Execute(vtkObject* caller, unsigned long, void*)
{
// Use this to format the output.
auto fmt = [](const double& x) {
std::ostringstream os;
os << std::fixed << std::setprecision(2) << std::setw(8) << x;
return os.str();
};
vtkBorderWidget* borderWidget = reinterpret_cast<vtkBorderWidget*>(caller);
// Get the world coordinates of the two corners of the box.
vtkCoordinate* lowerLeftCoordinate =
static_cast<vtkBorderRepresentation*>(borderWidget->GetRepresentation())
->GetPositionCoordinate();
double* lowerLeft =
lowerLeftCoordinate->GetComputedWorldValue(this->Renderer);
std::cout << "Lower left coordinate: " << fmt(lowerLeft[0]) << ","
<< fmt(lowerLeft[1]) << "," << fmt(lowerLeft[2]) << std::endl;
vtkCoordinate* upperRightCoordinate =
static_cast<vtkBorderRepresentation*>(borderWidget->GetRepresentation())
->GetPosition2Coordinate();
double* upperRight =
upperRightCoordinate->GetComputedWorldValue(this->Renderer);
std::cout << "Upper right coordinate: " << fmt(upperRight[0]) << ","
<< fmt(upperRight[1]) << "," << fmt(upperRight[2]) << std::endl;
double* bounds = this->ImageActor->GetBounds();
double xmin = bounds[0];
double xmax = bounds[1];
double ymin = bounds[2];
double ymax = bounds[3];
if ((lowerLeft[0] > xmin) && (upperRight[0] < xmax) &&
(lowerLeft[1] > ymin) && (upperRight[1] < ymax))
{
// std::cout << "box is inside image" << std::endl;
// std::cout << "xmin: " << xmin << " xmax: " << xmax << " ymin: " << ymin
// << " ymax: " << ymax << std::endl;
}
else
{
std::cout << "box is NOT inside image" << std::endl;
}
}
} // namespace
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(CenterAnImage)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
IOImage
ImagingCore
InteractionStyle
InteractionWidgets
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "CenterAnImage: 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(CenterAnImage MACOSX_BUNDLE CenterAnImage.cxx )
target_link_libraries(CenterAnImage PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS CenterAnImage
MODULES ${VTK_LIBRARIES}
)
Download and Build CenterAnImage¶
Click here to download CenterAnImage and its CMakeLists.txt file. Once the tarball CenterAnImage.tar has been downloaded and extracted,
cd CenterAnImage/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:
./CenterAnImage
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.