ubuntu 16.04 + Qt5.7.1+VTK6.3.0 + opencv2.4.13-viz(3D視覺化)安裝教程
ubuntu 16.04 + Qt5.7.1+VTK6.3.0 + opencv2.4.13-viz(3D視覺化)安裝教程
This section describes 3D visualization window as well as classes and methods that are used to interact with it.
3D visualization window (see Viz3d) is used to display widgets (see Widget), and it provides several methods to interact with scene and widgets.Vtk,(visualization toolkit)是一個開源的免費軟體系統,主要用於三維計算機圖形學、影象處理和視覺化。Vtk是在面向物件原理的基礎上設計和實現的,它的核心是用C++構建的,包含有大約250,000行程式碼,2000多個類。
OpenGL
(全寫Open Graphics Library)是指定義了一個跨程式語言、跨平臺的程式設計介面規格的專業的圖形程式介面。它用於三維影象(二維的亦可),是一個功能強大,呼叫方便的底層圖形庫。
OpenGL™ 是行業領域中最為廣泛接納的 2D/3D 圖形 API,其自誕生至今已催生了各種計算機平臺及裝置上的數千優秀應用程式。OpenGL™ 是獨立於視窗作業系統或其它作業系統的,亦是網路透明的。在包含CAD、內容創作、能源、娛樂、遊戲開發、製造業、製藥業及虛擬現實等行業領域中,OpenGL™ 幫助程式設計師實現在 PC、工作站、超級計算機等硬體裝置上的高效能、極具衝擊力的高視覺表現力圖形處理軟體的開發。
1 安裝視覺化cmake-gui
sudo apt-get install cmake-curses-gui
2 安裝Qt5
3 安裝X11
sudo apt-get install libx11-dev libxext-dev libxtst-dev libxrender-dev libxmu-dev libxmuu-dev
4 安裝OpenGL
sudo apt-get install build-essential libgl1-mesa-dev libglu1-mesa-dev libglut-dev
- 如果提示
libglut-dev
libglut-dev
:
sudo apt-get install freeglut3-dev
5 編譯VTK6.3
開啟
cmake-gui
介面在終端輸入:cmake-gui
在
cmake-gui
介面進行如下操作:- 1、設定
“where is the source code:”
的路徑為資料夾VTK-6.3.0
所在路徑,如/home/VTK-6.3.0
; - 2、設定
“where to build the binaries:”
的路徑為/home/VTK-6.3.0/build
,即在/home/VTK-6.3.0
的路徑下新建資料夾build
; - 3、點選
“Configure”
,在彈出對話方塊中選擇“Current Generator”為“Unix MakeFiles”
,完成之後會提示“Configuring done”
; - 4、由於安裝的是
Qt5
,而VTK-6.3.0
預設是Qt 4
, 故需要在第3步完成之後,找到Qt Version
,將4
改為5
,然後再次點選“Configure”
,完成之後會提示“Configuring done”
; - 5、點選
“Generate”
,完成之後會提示“Generating done”
; - 6、在終端進入到資料夾
build
的路徑下,輸入以下命令:
sudo make
sudo make install 7、圖示:
圖示可參考下面opencv的圖示
以上步驟完成以後,VTK-6.3.0安裝完成。
- 1、設定
6 編譯opencv2.4.13
開啟
cmake-gui
介面在終端輸入:cmake-gui
在
cmake-gui
介面進行如下操作:- 1、設定
“where is the source code:”
的路徑為資料夾opencv-2.4.13
所在的路徑,如/home/opencv-2.4.13
; - 2、設定
“where to build the binaries:”
的路徑為/home/opencv-2.4.13/build
,即在/home/opencv-2.4.13
的路徑下新建資料夾build
; - 3、點選
“Configure”
,完成之後會提示“Configuring done”
; - 4、將
"WITH_VTK"
勾選,然後再次點選“Configure”
,完成之後會提示“Configuring done”
; - 5、點選
“Generate”
,完成之後會提示“Generating done”
; 6、在終端進入到資料夾build的路徑下,輸入以下命令:
sudo make
sudo make install7、圖示:
以上步驟完成以後,帶VTK-6.3.0的opencv安裝完成。
- 1、設定
7 配置Qt
- 開啟Qt以後,新建工程,將以下路徑新增“.pro”檔案中,詳細過程請參考以下連結第3節內容:
INCLUDEPATH += /usr/local/include \
/usr/local/include/opencv \
/usr/local/include/opencv2 \
/usr/local/include/vtk-6.3
LIBS += /usr/local/lib/libopencv_calib3d.so \
/usr/local/lib/libopencv_contrib.so \
/usr/local/lib/libopencv_core.so \
/usr/local/lib/libopencv_features2d.so \
/usr/local/lib/libopencv_flann.so \
/usr/local/lib/libopencv_gpu.so \
/usr/local/lib/libopencv_highgui.so \
#/usr/local/lib/libopencv_imgcodecs.so \
/usr/local/lib/libopencv_imgproc.so \
/usr/local/lib/libopencv_legacy.so \
/usr/local/lib/libopencv_ml.so \
/usr/local/lib/libopencv_nonfree.so \
/usr/local/lib/libopencv_objdetect.so \
/usr/local/lib/libopencv_ocl.so \
/usr/local/lib/libopencv_photo.so \
#/usr/local/lib/libopencv_shape.so \
/usr/local/lib/libopencv_stitching.so \
/usr/local/lib/libopencv_superres.so \
/usr/local/lib/libopencv_video.so \
/usr/local/lib/libopencv_videostab.so \
/usr/local/lib/libopencv_viz.so
8 配置OpenCV.conf
9 Viz-3D
視覺化測試
- 測試程式碼:
將以下程式碼直接複製到Qt工程裡面,點選執行即可。
#include <QCoreApplication>
#include <opencv2/opencv.hpp>
//#include <opencv2/viz.hpp>
#include <opencv2/viz/viz3d.hpp>
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;
}
3D效果截圖: