开发环境:
- Windows 11 家庭中文版
- Microsoft Visual Studio Community 2019
- VTK-9.3.0.rc0
- vtk-example
demo解决问题:移动图片到坐标轴的中心,创建一个vtkBoarderWidget控件,移动控件,计算控件与图片的包含关系
关键点:
- 代码中使用 vtkImageChangeInformation 类来对图像进行中心偏移处理,将图像中心移动到坐标原点 (0, 0)。
- vtkBoarderWidget、vtkImageData坐标包含计算:
  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;
  }
prj name: CenterAnImage
#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





![[深度学习] 卷积神经网络“卷“在哪里?](https://img-blog.csdnimg.cn/direct/468fb6de5e9d4190868dbb2d6298fbf7.png)














