Displaying 3D Images Using OpenCV 4.2 Viz Module

Displaying 3D Images Using OpenCV 4.2 Viz Module

In OpenCV 3D vision, if you need to display three-dimensional data or images, you need to use the viz module. The viz module is the 3D display module of OpenCV, and the official release version of OpenCV does not include this module, so we need to compile it ourselves using cmake.

This article does not provide detailed steps for Cmake; it mainly introduces potential issues encountered during the process:

1. The path for the contrib module OPENCV_EXTRA_MODULES_PATH must always use forward slashes ‘/’.

Displaying 3D Images Using OpenCV 4.2 Viz Module

2. The viz module depends on VTK (Visualization Toolkit), which needs to be downloaded from https://vtk.org/download/ and compiled using cmake. The steps can be done using default settings, which is relatively simple. Here we use the latest version 8.2.0, and after cmake, the following Install file is generated:

Displaying 3D Images Using OpenCV 4.2 Viz Module

Then check the option WITH_VTK in cmake (this is checked by default in OpenCV 4.2) and set VTK_DIR to lib/cmake/vtk-8.2, as shown in the figure below:

Displaying 3D Images Using OpenCV 4.2 Viz Module

3. Other steps are relatively simple; first config, then generate, and then generate ALL_BUILD. The generated files are only for the project Install, resulting in folders like bin, etc, include, x64, where you can see that the OpenCV2 folder now contains viz.hpp. Then check if other files like lib/dll are generated successfully. Here, I package it into opencv_world420.dll/lib.

Displaying 3D Images Using OpenCV 4.2 Viz Module

Next, you can write a demo to test if the compilation was successful. You can create a new project and configure the package, including directories, library directories, additional dependencies, and then add the following test code:

// Viz3D.cpp : This file contains the "main" function. The program execution starts and ends here.//#include "pch.h"#include <iostream>#include <opencv2/viz.hpp>#include <opencv2/calib3d.hpp>#include <iostream>using namespace cv;using namespace std;
int main(){  /// Create a window  viz::Viz3d myWindow("Coordinate Frame");  /// Add coordinate axes  myWindow.showWidget("Coordinate Widget", viz::WCoordinateSystem());  /// Add line to represent (1,1,1) axis  viz::WLine axis(Point3f(-1.0f, -1.0f, -1.0f), Point3f(1.0f, 1.0f, 1.0f));  axis.setRenderingProperty(viz::LINE_WIDTH, 4.0);  myWindow.showWidget("Line Widget", axis);  /// Construct a cube widget  viz::WCube cube_widget(Point3f(0.5, 0.5, 0.0), Point3f(0.0, 0.0, -0.5), true, viz::Color::blue());  cube_widget.setRenderingProperty(viz::LINE_WIDTH, 4.0);  /// Display widget (update if already displayed)  myWindow.showWidget("Cube Widget", cube_widget);  /// Rodrigues vector  Mat rot_vec = Mat::zeros(1, 3, CV_32F);  float translation_phase = 0.0, translation = 0.0;  while (!myWindow.wasStopped())  {    //* Rotation using rodrigues     /// Rotate around (1,1,1)    rot_vec.at<float>(0, 0) += CV_PI * 0.01f;    rot_vec.at<float>(0, 1) += CV_PI * 0.01f;    rot_vec.at<float>(0, 2) += CV_PI * 0.01f;    /// Shift on (1,1,1)    translation_phase += CV_PI * 0.01f;    translation = sin(translation_phase);    Mat rot_mat;    Rodrigues(rot_vec, rot_mat);    /// Construct pose    Affine3f pose(rot_mat, Vec3f(translation, translation, translation));    myWindow.setWidgetPose("Cube Widget", pose);    myWindow.spinOnce(1, true);  }  return 0;}

During runtime, in addition to placing opencv_world420.dll in the project directory or adding it to the environment variable, you also need to add the bin directory of VTK (D:\VTK_Install\bin) to the environment variable to get the runtime results. The output of the above code is as follows:

Displaying 3D Images Using OpenCV 4.2 Viz Module

Animation effect:

Of course, you can also display three-dimensional graphics similar to the following effects:

Displaying 3D Images Using OpenCV 4.2 Viz Module

Displaying 3D Images Using OpenCV 4.2 Viz Module

Leave a Comment

×