ubuntu16.04 install opencv、Sohpus、pcl、OctoMap、VTK
1、opencv
1.1 查看 opencv 版本
pkg-config --modversion opencv
ROS自带OpenCV路径:set(OpenCV_DIR /opt/ros/kinetic/share/OpenCV-3.3.1-dev)
1.2 安装 opencv
依赖性项:
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt install python-pip
$ pip install numpy
$ sudo apt-get install build-essential libgtk-3-dev
$ sudo apt install ccache
$ sudo apt install default-jdk
$ sudo apt install python3-pip
$ pip3 install numpy
$ sudo apt install libatlas-base-dev liblapacke-dev
$ sudo apt-get -y install libopencv-dev build-essential cmake git libgtk2.0-dev pkg-config python-dev python-numpy libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev unzip
(1)安装 opencv2.4.9
下载 opencv2.4.9
点击 Sources 下载 opencv2.4.9
将 opencv-2.4.9.zip 文件解压,并切换到 opencv2.4.9 目录下
unzip opencv-2.4.9.zip -d ~/
cd opencv-2.4.9/
默认配置编译安装,默认安装路径/usr/local
mkdir build
cd build
cmake ..
make
sudo make install
(2)安装 opencv3.2.0 作为备用版本
下载 opencv3.2.0
点击 Sources 下载 opencv3.2.0
将 opencv-3.2.0.zip 文件解压到家目录下,并切换到 opencv-3.2.0 目录下
unzip opencv-3.2.0.zip -d ~/
cd opencv-3.2.0/
自定义编译安装,自定义安装路径/usr/local/opencv3.2.0(注意不要将自定义路径设为/usr,无效) 一定要改 cmake 的路径( 后面会提到)
mkdir build
cd build
cmake -D CMAKE_INSTALL_PREFIX=/usr/local/opencv3.2.0 ..
#cmake -D WITH_CUDA=OFF -D CMAKE_INSTALL_PREFIX=/usr/local/opencv3.2.0 .. #不编译CUDA库
make
sudo make install
(2)安装opencv_contrib模块
opencv3.4.4+opencv_contrib-3.4.4
(a).安装依赖项
#一般前面安装成功了就不需要这一步
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt-get install build-essential qt5-default ccache libv4l-dev libavresample-dev libgphoto2-dev libopenblas-base libopenblas-dev doxygen openjdk-8-jdk pylint libvtk6-dev
sudo apt-get install pkg-config
(b).解压完后将opencv_contrib.zip提取到opencv目录下,同时在该目录下新建一个文件夹build:
sudo cp -r opencv_contrib-3.4.4 opencv-3.4.4 #复制opencv_contrib到opencv目录下
cd opencv-3.4.4
sudo mkdir build
(c).现在进入到opencv-3.3.1目录下,查看文件结构:ls
3rdparty cmake data LICENSE platforms
apps CMakeLists.txt doc modules README.md
build CONTRIBUTING.md include opencv_contrib-3.3.1 samples
(d).进入build目录,并且执行cmake生成makefile文件:
安装过程下载失败的文件,可以在build/CMakeDownlodLog.txt中查看,并一一下载,然后按照路径放到build响应的文件夹下。 再cmake.
cd build
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/home/files/opencv-3.4.4/opencv_contrib-3.4.4/modules/ ..
# 多版本安装则选择下面这一句指定路径
#sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local/opencv3.4.4 -D OPENCV_EXTRA_MODULES_PATH=/home/fb/tools/opencv-3.4.4/opencv_contrib/modules/ ..
#*如果要使用sift,surf算法因为涉及到专利问题所以上面的命令应该是:
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local/opencv-3.4.15 -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_EXTRA_MODULES_PATH=/home/q/software/opencv-3.4.15/opencv_contrib-3.4.15/modules/ ..
sudo make -j8
sudo make install
报错:fatal error: stdlib.h: No such file or directory
解决:在cmake 编译的时候,-D ENABLE_PRECOMPILED_HEADERS=OFF
,即可成功。
报错:fatal error: hdf5.h: No such file or directory
解决:在modules/python/common.cmake
文件中将
set(PYTHON_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../")
ocv_add_module(${MODULE_NAME} BINDINGS)
改为:
set(PYTHON_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../")
find_package(HDF5)
include_directories(${HDF5_INCLUDE_DIRS})
ocv_add_module(${MODULE_NAME} BINDINGS)
报错:error: expected unqualified-id before ‘>’ token
解决:
将opencv_contrib/modules/tracking/include/opencv2/tracking/onlineMIL.hpp 第57行的
#define sign(s) ((s > 0 ) ? 1 : ((s<0) ? -1 : 0)) 给剪切掉
再讲将上面一段代码复制到opencv_contrib/modules/tracking/src/onlineMIL.cpp 中预定义的位置即可
接着再执行 make -j8
(3)安装新的 opencv 需要注意两件事
1、 更改 cmake 的路径
2、更改指令 cmake -D CMAKE_INSTALL_PREFIX=/usr/local/opencv3.2.0 …
更改 opencv 安装路径: 这一步很重要, 尤其对于多个版本 opencv 切换 不然会覆盖 ros 自带的 opencv
更改CMakeLists.txt 的路径 17 到 30 行
if(NOT CMAKE_TOOLCHAIN_FILE)
#it _must_ go before project(OpenCV) in order to work
if(WIN32)
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH
"Installation Directory")
else()
set(CMAKE_INSTALL_PREFIX "/usr/local" CACHE PATH "Installation Directory")
endif()
else(NOT CMAKE_TOOLCHAIN_FILE)
#Android: set output folder to ${CMAKE_BINARY_DIR}
set( LIBRARY_OUTPUT_PATH_ROOT ${CMAKE_BINARY_DIR} CACHE PATH "root for
library output, set this to change where android libs are compiled to" )
# any crosscompiling
set(CMAKE_INSTALL_PREFIX "/usr/local/opencv248" CACHE PATH "Installation
Directory") #更改 opencv 安装路径
#set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH
"Installation Directory")
endif(NOT CMAKE_TOOLCHAIN_FILE)
更改 make 的地址
mkdir build
cd build
cmake -D CMAKE_INSTALL_PREFIX=/usr/local/opencv3.2.0 .. //更改一下路径
make
sudo make install
1.3 卸载 opencv
sudo rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2/usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV/usr/local/bin/opencv* /usr/local/lib/libopencv*
运行程序,提示没有的文件,删除即可 指令中的路径即可删除文件 opencv.pc ,路径如下:
:/usr/local/lib/pkgconfig
1.4 opencv 版本切换
更改CMakeLists.txt 文件
# cmake version
cmake_minimum_required(VERSION 2.8)
# project name
project(test)
# find opencv
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
//下面的是特定的 cpp 文件
# add cpp file
add_executable(test test.cpp)
target_link_libraries(test ${OpenCV_LIBS})
或者 使用 3.2
# cmake version
cmake_minimum_required(VERSION 2.8)
# find opencv3 OpenCVConfig.cmake
set(OpenCV_DIR "/usr/local/opencv3.2.0/share/OpenCV")
# project name
project(test)
# find opencv
find_package(OpenCV 3.2.0 REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
# add cpp file
add_executable(test test.cpp)
target_link_libraries(test ${OpenCV_LIBS})
2、Sophus
2.1、Sophus库简介:
Eigen库是一个开源的C++线性代数库,它提供了快速的有关矩阵的线性代数运算,还包括解方程等功能。但是Eigen库提供了集合模块,但没有提供李代数的支持。一个较好的李群和李代数的库是Sophus库,它很好的支持了SO(3),so(3),SE(3)和se(3)。Sophus库是基于Eigen基础上开发的,继承了Eigen库中的定义的各个类。因此在使用Eigen库中的类时,既可以使用Eigen命名空间,也可以使用Sophus命名空间。比如
Eigen::Matrix3d和Sophus::Matrix3d
Eigen::Vector3d和Sophus::Vector3d
此外,为了方便说明SE(4)和se(4),Sophus库还typedef了Vector4d、Matrix4d、Vector6d和Matrix6d等,即:
Sophus::Vector4d
Sophus::Matrix4d
Sophus::Vector6d
Sophus::Matrix6d
2.2、Sophus安装方式:
git clone https://github.com/strasdat/Sophus.git
cd Sophus
git checkout a621ff
mkdir build
cd build
cmake ..
make
sudo make install
3、PCL
3.1 、安装依赖项
sudo apt-get update
sudo apt-get install git build-essential linux-libc-dev
sudo apt-get install cmake cmake-gui
sudo apt-get install libusb-1.0-0-dev libusb-dev libudev-dev
sudo apt-get install mpi-default-dev openmpi-bin openmpi-common
sudo apt-get install libflann1.8 libflann-dev
sudo apt-get install libeigen3-dev
sudo apt-get install libboost-all-dev
sudo apt-get install libvtk5.10-qt4 libvtk5.10 libvtk5-dev #1
sudo apt-get install libqhull* libgtest-dev
sudo apt-get install freeglut3-dev pkg-config
sudo apt-get install libxmu-dev libxi-dev
sudo apt-get install mono-complete
sudo apt-get install qt-sdk openjdk-8-jdk openjdk-8-jre #2
3.2、 Build Point Cloud Library
视电脑性能而定,编译时间可能会比较长。更新:后面在程序调用的时候出现错误 No rule to make target ‘/usr/local/lib/libpcl_io.so’, needed by ‘joinmap’. Stop.相应的还有其他几个库文件。问题就是没有编译到系统库文件路径里面去。解决办法:在build时不建立build文件夹,直接在PCL安装的根目录里面编译安装。)
git clone https://github.com/PointCloudLibrary/pcl.git
cd pcl
mkdir build #省去
cd build #省去
cmake -DCMAKE_BUILD_TYPE=None -DCMAKE_INSTALL_PREFIX=/usr \ -DBUILD_GPU=ON -DBUILD_apps=ON -DBUILD_examples=ON \ -DCMAKE_INSTALL_PREFIX=/usr ..
sudo make -j10
sudo make install
3.3、卸载掉ros的东西后的解决方法
安装过程会自动卸载掉一些ROS相关的文件。catkin_make 基本编译不了东西,解决方法,重新安装ros:
sudo apt-get update
sudo apt-get install ros-kinetic-desktop-full
这两句话只是安装了之前卸掉的包,并没有完全安装ros,很快就装好。
3.4、 测试PCL
/home/fb/tools/pcl/test 安装路径下面有test文件夹,里面有***.pcd文件。可以用pcl_viewer ***.pcd
进行测试。
4.安装OctoMap
OctoMap库实现了一个三维网格占用映射方法,提供数据结构和映射算法在C ++特别适合于机器人。
sudo apt-get install libqglviewer-dev-qt4
git clone https://github.com/OctoMap/octomap
cd octomap
mkdir build && cd build
cmake ..
make
sudo make install
5.安装VTK
5.1.安装QT
Qt是为多个屏幕创建创新设备,现代UI和应用程序的更快,更智能的方式。
chmod +x qt-opensource-linux-x64-5.8.0.run
./qt-opensource-linux-x64-5.8.0.run
5.2.安装VTK
可视化工具包VTK是一个开源的免费软件系统,用于3D计算机图形,图像处理和可视化。
cd VTK-8.1.1
mkdir build && cd build #需要安装QT
cmake -DVTK_QT_VERSION:STRING=5 -DQT_QMAKE_EXECUTABLE:PATH=[PATH]/Qt5.8.0/5.8/gcc_64/bin/qmake -DVTK_Group_Qt:BOOL=ON -DCMAKE_PREFIX_PATH:PATH=[PATH]/Qt5.8.0/5.8/gcc_64/lib/cmake -DBUILD_SHARED_LIBS:BOOL=ON ..
make