首页 > 代码库 > OpenCV Tutorials —— Creating Widgets

OpenCV Tutorials —— Creating Widgets

Explanation

  • Extend Widget3D class to create a new 3D widget.
  • Assign a VTK actor to the widget.
  • Set color of the widget.
  • Construct a triangle widget and display it in the window.

 

Code

#include <opencv2/viz/vizcore.hpp>#include <opencv2/viz/widget_accessor.hpp>#include <iostream>#include <vtkPoints.h>#include <vtkTriangle.h>#include <vtkCellArray.h>#include <vtkPolyData.h>#include <vtkPolyDataMapper.h>#include <vtkIdList.h>#include <vtkActor.h>#include <vtkProp.h>using namespace cv;using namespace std;/** * @class WTriangle * @brief Defining our own 3D Triangle widget */class WTriangle : public viz::Widget3D{    public:        WTriangle(const Point3f &pt1, const Point3f &pt2, const Point3f &pt3, const viz::Color & color = viz::Color::white());};/** * @function WTriangle::WTriangle */WTriangle::WTriangle(const Point3f &pt1, const Point3f &pt2, const Point3f &pt3, const viz::Color & color){    // Create a triangle    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();    points->InsertNextPoint(pt1.x, pt1.y, pt1.z);    points->InsertNextPoint(pt2.x, pt2.y, pt2.z);    points->InsertNextPoint(pt3.x, pt3.y, pt3.z);    vtkSmartPointer<vtkTriangle> triangle = vtkSmartPointer<vtkTriangle>::New();    triangle->GetPointIds()->SetId(0,0);    triangle->GetPointIds()->SetId(1,1);    triangle->GetPointIds()->SetId(2,2);    vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();    cells->InsertNextCell(triangle);    // Create a polydata object    vtkSmartPointer<vtkPolyData> polyData = http://www.mamicode.com/vtkSmartPointer::New();"Creating Widgets");    /// Create a triangle widget    WTriangle tw(Point3f(0.0,0.0,0.0), Point3f(1.0,1.0,1.0), Point3f(0.0,1.0,0.0), viz::Color::red());    /// Show widget in the visualizer window    myWindow.showWidget("TRIANGLE", tw);    /// Start event loop    myWindow.spin();    return 0;}

OpenCV Tutorials —— Creating Widgets