1.下载并解压 OpenCV-4.0.0及OpenCV_contrib-4.0.0源码至同一个文件夹。
2.添加opencv所需要的依赖库
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
3.因为之前安装过Opencv3.4,我们将OpenCV的prefix目录安装在/usr/local/opencv4.0.0,在终端进入至OpenCV-4.0.0,输入:
mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local/opencv4.0.0 -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.0.0/modules/ ..
4.当显示Configure Done后, 输入:
sudo make -j8
sudo make install
如果因为c++11不支持,打开opencv目录下的CMakeLists.txt,在开头添加:
set(CMAKE_CXX_FLAGS "-std=c++11")
5.在命令行执行安装命令:
sudo make install
1. 添加opencv的库路径。
在命令行输入:
sudo gedit /etc/ld.so.conf.d/opencv.conf
在opencv.conf中输入:
/usr/local/opencv4.0.0/lib
2. 添加pkg环境变量。
在命令行输入:
sudo gedit /etc/profile
然后在文本框末尾添加:
export PKG_CONFIG_PATH=/usr/local/opencv4.0.0/lib/pkgconfig:$PKG_CONFIG_PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/opencv4.0.0/lib
命令行输入:
source /etc/profile
3. 添加bash环境变量:
sudo gedit /etc/bash.bashrc
在文本末尾添加:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv4.0.0/lib/pkgconfig
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/opencv4.0.0/lib
命令行输入:
source /etc/bash.bashrc
4.更新系统缓存:
sudo ldconfig
sudo updatedb
5.查看opencv是否安装成功。
命令行输入
pkg-config --modversion opencv4
pkg-config --cflags opencv4
pkg-config --libs opencv4
至此,带有nonfree的OpenCV4.0.0已经配置完成
如果cmake … 找不到opencv路径,在CMakeLists.txt文件中添加:
set (CMAKE_PREFIX_PATH "/usr/local/opencv4.0.0")
CMakeLists.txt样板:
cmake_minimum_required(VERSION 2.8)
project(opencvtest)
set (CMAKE_PREFIX_PATH "/usr/local/opencv4.0.0")
find_package( OpenCV REQUIRED )
add_executable( opencvtest opencvtest.cpp )
target_link_libraries( opencvtest ${OpenCV_LIBS} )