前言
博主想要TX2上使用opencv中的viz模块,需要安装opencv_contrib,目前TX2的opencv版本是刷机的时候安装的opencv3.3.1,需要先卸载,再安装。后来发现opencv3.3.1版本中本含有viz模块,只是更高版本的opencv中viz模块是在opencv_contirb中。
步骤
1. 卸载TX2刷机的opencv版本;
sudo apt-get purge libopencv* sudo apt autoremove sudo apt-get update
2. 安装带有viz模块的opencv3.3.1版本;
2.1. 安装opencv的依赖库,必须在安装opencv之前安装好;
[compiler] sudo apt-get install build-essential [required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev [optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
viz模块
sudo apt-get install libvtk5-dev
2.2. 下载源码并解压,包括opencv-3.3.1和opencv_contrib-3.3.1,从github下载,注意两者的版本必须要一致;
/opt$ sudo unzip opencv-3.3.1.zip opencv_contrib-3.3.1.zip /opt$cd opencv-3.3.1 /opt/opencv-3.3.1$sudo mv ../opencv_contrib-3.3.1 ./ /opt/opencv-3.3.1$sudo mkdir build /opt/opencv-3.3.1$cd build
2.3. 编译安装;
/opt/opencv-3.3.1/build$ sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-3.3.1/modules/ -D WITH_VTK=ON .. /opt/opencv-3.3.1/build$sudo make -j8(wait long time) /opt/opencv-3.3.1/build$sudo make install
其中,
-D WITH_VTK=ON
是关于viz模块的;
问题:cmake的时候正常,make的时候出错,显著性的错误如下:
xfeatures2d/boostdesc: Download failed: 7;"Couldn't connect to server"
and
/opt/opencv-3.3.1/modules/stitching/include/opencv2/stitching/detail/matchers.hpp:52:42: fatal error: opencv2/xfeatures2d/cuda.hpp: No such file or directory compilation terminated.
step1:
Github上有网友指出是因为OpenCVDownload.cmake
在日志记录功能中出现问题,找到文件
~/opencv-3.3.1/cmake/OpenCVDownload.cmake
将以下代码
macro(ocv_download_log) file(APPEND "${OPENCV_DOWNLOAD_LOG}" "${ARGN} ") endmacro()
修改成
function(ocv_download_log) file(APPEND "${OPENCV_DOWNLOAD_LOG}" "${ARGN} ") endfunction()
step2:
cmake命令添加以下内容:
cmake -D BUILD_opencv_xfeatures2d=OFF
还有说HTTP的安装需要某个版本的cmake(博主没有进行这个问题的操作,具体可参考github的issue),其他的原因有可能是内存空间不足等。
问题
/opt/opencv-3.3.1/modules/cudacodec/src/precomp.hpp:60:37: fatal error: dynlink_nvcuvid.h: No such file or directory compilation terminated.
原因:这个问题与系统是否安装CUDA、安装CUDA的版本有关,可以使用-D WITH_CUDA=OFF解决该问题;
make这一步骤完成,build目录占了差不多11G的空间,不清楚build目录能否删除,只安装opencv而不安装contrib则build差不多3G的空间。
3. 测试opencv和viz模块;
CMakeLists.txt
cmake_minimum_required(VERSION 2.8) set(CMAKE_CXX_COMPILER "g++") set_property(GLOBAL PROPERTY USE_FOLDERS ON) list(APPEND CMAKE_CXX_FLAGS "-O3 -DEBUG -ffast-math -Wall -pthread -fopenmp -std=c++11") #-DNDEBUG project(test_viz) # OpenCV find_package(OpenCV 3.3.1 REQUIRED) include_directories(${OpenCV_INCLUDE_DIRS}) # message("OpenCV_INCLUDE_DIRS: ${OpenCV_INCLUDE_DIRS}") # message("OpenCV_LIBS: ${OpenCV_LIBS}") # VTK find_package(VTK 5.10 REQUIRED) include(${VTK_USE_FILE}) # message("VTK_USE_FILE: ${VTK_USE_FILE}") # message("VTK_LIBRARIES: ${VTK_LIBRARIES}") # PROJECT include_directories(include) aux_source_directory(src DIR_SRCS) add_executable(${PROJECT_NAME} ${DIR_SRCS}) target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} ${VTK_LIBRARIES})
test_code(可以参考github_viz_samples运行各个demo):
#include <iostream> #include <opencv2/viz.hpp> #include <opencv2/highgui.hpp> #include <opencv2/calib3d.hpp> using namespace std; using namespace cv; int main() { viz::Viz3d window("window"); window.showWidget("Coordinate", viz::WCoordinateSystem()); viz::WPlane plane(Size2d(2,2), viz::Color::white()); plane.setRenderingProperty(viz::LINE_WIDTH, 5); plane.setPose(Affine3f()); window.showWidget("plane", plane); Mat rvec = Mat::zeros(1, 3, CV_32F); while(!window.wasStopped()) { rvec.at<float>(0,0) = 0.f; rvec.at<float>(0,1) += CV_PI*0.01f; rvec.at<float>(0,2) = 0.f; Mat rmat; Rodrigues(rvec, rmat); Affine3f pose(rmat, Vec3f(0,0,0)); window.setWidgetPose("plane", pose); window.spinOnce(1, true); } }
其中,可能会用一些已有的文件,比如bunny.ply,编译之后所在目录为:
/opt/opencv-3.3.1/samples/cpp/tutorial_code/viz/
后记
可以正常使用opencv viz模块之后,博主想使用之前安装的PCL开源库,发现PCL不能正常使用,不能find PCL的配置文件,可能是安装的时候有冲突,比如VTK;
参考
1. Jetson TX2 卸载并安装OpenCV3.4.0;
2. linux系统卸载opencv;
3. linux系统opencv以及opencv_contrib的安装与使用;
4. opencv_installation_on_linux;
完