目录结构

opencv-4.0.1
    |_____opencv_contrib-4.0.1/modules
    |_____build

编译命令

mkdir build
cd build

sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libv4l-dev

sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

sudo apt-get install libeigen3-dev libgoogle-glog-dev libgflags-dev

#(sudo apt-get install python-dev python-numpy)
#it is better to configure your own python library for this testing, 
#as in below cmake command

cmake -DCMAKE_INSTALL_PREFIX=/opt/opencv400 -DCMAKE_BUILD_TYPE=Release \
-DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.0.1/modules \
-DPYTHON_EXECUTABLE=python3 \
-DPYTHON_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-DPYTHON_LIBRARY=$(python3 -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))")  ..

make -j8

make install

说明

我这里是加了extra modules的,注意目录结构要和你的cmake命令中的EXTRA_MODULES_PATH目录对应。

库文件最好是先装上,不然会有很多报错。OpenCV-SFM模块还信赖eigen/glog/gflags,如果需要该模块的话这两个包也要装上。

在执行cmake ...命令前,build下面应该是空的,不然遗留下来的东西有可能冲突,也会报错。
-DCMAKE_INSTALL_PREFIX=你想要安装opencv的路径

Python的路径这么设置是因为碰到了报错:

Could NOT find PythonLibs (missing:  PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS)

具体可以参考:https://stackoverflow.com/questions/24174394/cmake-is-not-able-to-find-python-libraries
另外我试了下,-DPYTHON_INCLUDE_DIR=/home/user01/anaconda3/include/python3.7m/ ,编译也没有报错。网上也有人说如果找不到python.h就应该使用这种形式,嗯,还是自己编译的时候试试吧!

一般编译尽量使用release版本,如果你要debug,建议还是windows下的VC环境要方便得多(VC调试器实在太强大了)。