博主尝试了一次在ubuntu16.04上的anaconda环境下编译opencv4.0.0,编译的过程比以前编译opencv3.4.2时出奇的顺利,分享给同学们。
注意事项:ubuntu16.04+anaconda+opencv-4.0.0+opencv_contrib-4.0.0
1.首先是安装Opencv依赖项
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev libxvidcore-dev libx264-dev
2.git下来需要的opencv源码
到https://github.com/opencv/opencv和https://github.com/opencv/opencv_contrib
选择下在4.0.0版本的opencv和opencv_contrib
然后直接解压到根目录中
3.安装anaconda,直接下载安装
4.进入到opencv根目录下
mkdir bulid && cd bulid
5.source进入conda环境下
source activate tf36
6.重点来了,这一步也是非常关键的一步,参考了很多都有问题不可行
cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_V4L=ON -D WITH_OPENGL=ON -D WITH_VTK=ON .. -D CMAKE_BUILD_TYPE=RELEASE -D OPENCV_EXTRA_MODULES_PATH=/home/fengzicai/Downloads/opencv_contrib-4.0.0/modules -D WITH_IPP=OFF -D PYTHON_DEFAULT_EXECUTABLE=/home/fengzicai/anaconda3/envs/tf36/lib/python3.6 -D BUILD_opencv_python3=ON -D BUILD_opencv_python2=OFF -D PYTHON3_EXCUTABLE=/home/fengzicai/anaconda3/envs/tf36/lib/python3.6 -D PYTHON3_INCLUDE_DIR=/home/fengzicai/anaconda3/envs/tf36/include/python3.6m -D PYTHON3_LIBRARY=/home/fengzicai/anaconda3/envs/tf36/lib/libpython3.6m.so.1.0 -D PYTHON_NUMPY_PATH=/home/fengzicai/anaconda3/envs/tf36/lib/python3.6/site-packages -D BUILD_EXAMPLES=ON -D BUILD_LIBPROTOBUF_FROM_SOURCES=ON -D BUILD_opencv_hdf=OFF ..
7.上一步确保cmake成功,会出现
configuring down,generating down,building files has been written…
8.编译安装opencv
make -j8
sudo make install
sudo ldconfig
9.重点来了
找到" cv2.cpython-36m-x86_64-linux-gnu.so "文件的路径:
sudo find / -name "cv2.cpython-36m-x86_64-linux-gnu.so"
比如我是
/usr/local/python/cv2/python-3.6/cv2.cpython-36m-x86_64-linux-gnu.so
10.然后将这个文件替换成cv2.so
sudo su
cd /usr/local/python/cv2/python-3.6/
cp cv2.cpython-36m-x86_64-linux-gnu.so cv2.so
11.之后将该cv2.so文件与anaconda路径下建立软链接
cd /home/fengzicai/anaconda3/envs/tf36/lib/python3.6/site-packages
ln -s /usr/local/python/cv2/python-3.6/cv2.cpython-36m-x86_64-linux-gnu.so cv2.so
12.经过一下下的等待,然后结束,键入Python,import cv,成功。查看版本
python
import cv2
cv2.__version__
13.问题总结:
(1)
[modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o] Error 1
CMakeFiles/Makefile2:20921: recipe for target 'modules/python3/CMakeFiles/opencv_python3.dir/all' failed
make[1]: *** [modules/python3/CMakeFiles/opencv_python3.dir/all] Error 2
Makefile:162: recipe for target 'all' failed
make: ***
[all] Error 2
解决方法:
在opencv-4.0.0/文件夹下运行
python ./modules/python/src2/gen2.py ./build/modules/python_bindings_generator
(2)
make[1]: *** [modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
报错原因是由于网络的问题,文件face_landmark_model.dat不能下载!
参考:https://github.com/opencv/opencv/issues/9196
解决方法:单独从网上下载
从 contrib_face_alignment下载地址进行face_landmark_model.dat的下载;