环境:
Ubuntu18.04
python:3.6.9
opencv:4.6.0
sudo apt-get install cmake
cmake -version
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 libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
sudo apt-get install libxvidcore-dev libx264-dev #处理视频所需的包
sudo apt-get install libatlas-base-dev gfortran # 优化opencv功能
sudo apt-get install ffmpeg
第三行命令安装libjasper-dev时遇到报错
unable to ;ocate package libjasper-dev
解决:
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
新建文件夹,下载并解压OpenCV、OpenCV_contrib,以4.1.1为例
这里将opencv_contirb-4.1.1文件夹放在opencv-4.1.1文件夹里
cd opencv-4.1.1 #以自己路径和文件名为准
unzip opencv-4.1.1.zip
unzip opencv-4.1.1.zip
进入解压后的opencv-4.1.1文件夹, 新建一个名为build的文件夹用来存放cmake编译时产生的临时文件
mkdir build
cd build
配置cmake编译参数
sudo cmake -D CMAKE_BUILD_TYPE=Release -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3 -D PYTHON_INCLUDE_DIR=/usr/include/python3.6 -D PYTHON_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python3.6m -D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include/ -D OPENCV_GENERATE_PKGCONFIG=ON -D BUILD_opencv_python2=OFF -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=~/repos/opencv-4.1.1/opencv_contrib-4.1.1/modules ..
注意:最后一句话的opencv扩展模型路径要对应改到你自己先前建立的文件路径下
出现如下提示即为成功
Configuring done
Generating done
参数简介及编译过程中报错解决方案请参考:https://waao666.blog.csdn.net/article/details/84197532
sudo make
报错 No space left on device
尝试:https://blog.csdn.net/qq_35451572/article/details/80778388无果
最终尝试外接U盘扩容(挂载)的方式https://blog.csdn.net/weixin_44244190/article/details/126934960
具体如下:
sudo su #root权限
fdisk -l #查看磁盘分区,U盘还没挂载一般会显示在最后
cd /mnt && mkdir /mnt/usb #将U盘挂载在/mnt目录下的USB文件夹中
mount /dev/sdb4 /mnt/usb #将U盘设备(/dev/sdb4)挂载到/mnt/usb目录下,此后软件直接安装在这里
#如果挂载完成以后,使用完毕,需要卸载,执行:
umount /mnt/usb
完成上述步骤后,重新开始编译即可。
下面进行安装
sudo make install
安装完成后运行
python3
import cv2
报错后尝试
pip3 install opencv-python
sudo apt install python3-pip
成功后重新运行
pip3 install opencv-python
发现下载速度很慢,只有十几K每秒,于是使用的清华大学的镜像包
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python
下载完成后报错ModuleNotFoundError: No module named 'skbuild'
参考https://blog.csdn.net/GungnirsPledge/article/details/108566415
pip3 install --upgrade pip
然后重新
pip install opencv-python
报错
WARNING: The scripts f2py, f2py3 and f2py3.6 are installed in '/home/nano/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
原因:未添加到环境变量
解决:
参考https://blog.csdn.net/LYiiiiiii/article/details/119239206
vi ~/.bashrc
export PATH=/home/ubuntu/.local/bin/:$PATH #根据warning具体的路径进行替换
source ~/.bashrc
附编辑方式:
进入编辑模式 i
退出编辑模式 Esc
保存退出 :wq ( :不可忽略)
至此,所有问题解决,重新测试即可
python3
import cv2
cv2.__version__
输出版本号即成功。
参考文献
https://waao666.blog.csdn.net/article/details/84197532
https://blog.csdn.net/qq_37612550/article/details/110398392
https://blog.csdn.net/qq_39708147/article/details/112911526
https://blog.csdn.net/weixin_45052870/article/details/125627476