pip3 uninstall opencv-python
sudo apt purge libopencv*
sudo apt autoremove
sudo apt update
sudo apt install -y build-essential checkinstall cmake pkg-config yasm git gfortran
sudo apt install -y libjpeg8-dev libjasper-dev libtiff5-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine2-dev libv4l-dev
sudo apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgtk2.0-dev libtbb-dev libatlas-base-dev libfaac-dev libmp3lame-dev libtheora-dev libvorbis-dev libxvidcore-dev libopencore-amrnb-dev libopencore-amrwb-dev x264 v4l-utils
sudo apt install python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev
安装libpng12-0
sudo add-apt-repository ppa:linuxuprising/libpng12
sudo apt update
sudo apt install libpng12-0
若不慎安装了libpng12-dev
,与libpng12-0
冲突,则需要卸载libpng12-dev
sudo apt-get remove libpng12-dev
安装libjasper-dev
sudo add-apt-repository "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe"
sudo apt update
sudo apt install libjasper1 libjasper-dev
下载opencv
https://github.com/opencv/opencv_contrib/archive/refs/tags/4.5.5.zip
https://github.com/opencv/opencv/archive/refs/tags/4.5.5.zip
mkdir ~/opencv
cd ~/opencv
mkdir build &cd build
cd opencv-4.5.5
mkdir build
cmake \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=/usr/local \
-DOPENCV_ENABLE_NONFREE=1 \
-DBUILD_opencv_python2=1 \
-DBUILD_opencv_python3=1 \
-DWITH_FFMPEG=1 \
-DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda \
-DCUDA_ARCH_BIN=7.2 \
-DCUDA_ARCH_PTX=7.2 \
-DWITH_CUDA=1 \
-DENABLE_FAST_MATH=1 \
-DCUDA_FAST_MATH=1 \
-DWITH_CUBLAS=1 \
-DOPENCV_GENERATE_PKGCONFIG=1 \
-DOPENCV_EXTRA_MODULES_PATH=/home/nvidia/opencv/opencv_contrib-4.5.5/modules \
..
这里-DOPENCV_EXTRA_MODULES_PATH=/home/nvidia/opencv/opencv_contrib-4.5.5/modules \
需要改为相应的路径
make -j8
sudo make install
安装cv2
cd opencv-4.5.5/build/python_loader
sudo pip3 install .
https://blog.csdn.net/weixin_52303102/article/details/136846684,解决libjasper包安装不上问题
但import cv2时,还是会报no module "cv2"的报错,到build目录下,找到python_load文件夹,这个文件夹就是cv2编译的python package
cd opencv-4.5.5/build/python_loader
sudo pip3 install .
https://zhuanlan.zhihu.com/p/678890471,解决cv2报错的方法