******* 哈哈,该方法安装的源码包含了freetype中文库*******
解决了安装后在usr目录下没有文件python-3.8/cv2.cpython-38-x86_64-linux-gnu.so的问题,python版的opencv也不用pip安装了,毕竟pip安装的有些库不包含,影响我们工作的效率,此笔记让你一劳永逸,奥里给,哈哈!
参考链接:
ubuntu系统编译安装OpenCV 4.4 - zfb132 - 博客园
C++版opencv库学习请参考我整理的长篇笔记:opencv4算法库学习笔记(5万多字超长干货——纪念奋战的自己)_打不死的小强,要强的博客-CSDN博客
具体步骤:
新建opencv_source文件夹,把opencv和opencv_contrib的源码都克隆到该目录下:
git clone -b 4.5.4 http:// https://gitee.com/mirrors/opencv
git clone -b 4.5.4 https://gitee.com/mirrors/opencv_contrib
在opencv源码目录(不是opencv_source父目录)下新建build文件夹,在此文件夹内编译,
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_VERBOSE_MAKEFILE=ON
-DCMAKE_INSTALL_PREFIX=/usr/local/opencv
-DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules -DOPENCV_ENABLE_NONFREE=ON
-DBUILD_opencv_python3=ON -DPYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3
-DWITH_1394=OFF -DWITH_FREETYPE=ON
-DWITH_IPP=ON -DWITH_TBB=ON -DWITH_OPENMP=ON -DWITH_PTHREADS_PF=ON
-DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DOPENCV_GENERATE_PKGCONFIG=ON
-DWITH_CUDA=OFF -DENABLE_FAST_MATH=ON -DCUDA_FAST_MATH=OFF -D WITH_CUBLAS=ON
-DPYTHON3_LIBRARY=$(python3 -c "from distutils.sysconfig import get_config_var;from os.path import dirname,join ; print(join(dirname(get_config_var('LIBPC')),get_config_var('LDLIBRARY')))")
-DPYTHON3_NUMPY_INCLUDE_DIRS=$(python3 -c "import numpy; print(numpy.get_include())")
-DPYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
make all -j$(grep -c ^processor /proc/cpuinfo)
sudo make install -j$(grep -c ^processor /proc/cpuinfo)
安装后如果没有出现文件/usr/lib/python3/dist-packages/cv2/python-3.8/cv2.cpython-38-x86_64-linux-gnu.so,那就先卸载opencv,
sudo make uninstall all
cd /usr/,彻底删除相关文件
sudo find . -name "opencv" | xargs sudo rm -rf
把build清空,从头再来一遍
查看系统Python环境的包路径
/usr/bin/python3 -c "import pip;print(pip)"
创建软链接使得/usr/bin/python3可以使用opencv
sudo ln -s /usr/lib/python3/dist-packages/cv2/python-3.8/cv2.cpython-38-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/cv2.so
测试安装结果
/usr/bin/python3 -c "import cv2;print(cv2.version)"
打印出 4.5.4就是ok了,这样就把python版和C++版opencv安装完成了
如果通过pip安装了opencv,先用以下命令卸载了再用
python -m pip uninstall opencv-python opencv-contrib-python