1.更新一下
sudo apt-get update
sudo apt-get upgrade
tar -zxvf cmake-3.22.1.tar.gz
先安装ubuntu基础的Cmake(如果本来就有的话就不需要)
sudo apt-get install cmake
然后进入解压文件夹下
cd cmake-3.22.1
mkdir build
cd build
cmake ..
make
sudo make install
sudo apt-get install cmake-qt-gui#Cmake的图形界面,可以不装也行
出现的问题:ould NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY OPENSSL_INCLUDE_DIR)
解决方法:
终端输入:
apt install openssl
apt install libssl-dev
1.opencv下载
opencv下载:opencv
下载Sourcr code(tar.gz)
2.下载opencv_contrib
对应版本下载即可:opencv_contrib
3.安装opencv依赖文件
sudo apt-get install build-essential
sudo apt-get install git libgtk-3-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
遇到问题:E: 无法定位软件包 libjasper-dev
解决方法:E: 无法定位软件包 libjasper-dev解决方法
4.解压并编译
tar -zxvf opencv-4.5.4.tar.gz
tar -zxvf opencv_contrib-4.5.4.tar.gz
将OpenCV-contrib文件解压后放入opencv解压后的文件夹,并修改contrib文件夹名字为opencv_contrib
cd opencv-4.5.4/
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ..
也可以将OPENCV_EXTRA_MODULES_PATH=…/opencv_contrib/modules …修改为自己的路径,例如我的改法写入绝对路径。
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/home/cj/opencv454/opencv-4.5.4/opencv_contrib-4.5.4/modules ..
如果不需要contrib模块,则输入:
cmake -D CMAKE_INSTALL_PREFIX=/usr/local -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D ENABLE_CXX11=1 -D OPENCV_ENABLE_NONFREE=True ..
如果如要GPU加速和contrib模块,则输入:
cmake -D CMAKE_INSTALL_PREFIX=/usr/local -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D ENABLE_CXX11=1 -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules -D OPENCV_ENABLE_NONFREE=True -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON -D WITH_CUDA=ON -D WITH_TBB=ON -D ENABLE_FAST_MATH=1 -D WITH_OPENMP=ON -D WITH_CUFFT=ON -D WITH_CUBLAS=ON -D CUDA_FAST_MATH=1 -D CUDA_NVCC_FLAGS="-D_FORCE_INLINES" ..
输入
make -j$(nproc)
sudo make install
5.配置环境
新开一个终端
sudo gedit /etc/ld.so.conf.d/OpenCV.conf
打开是一个空文档,写入/usr/local/lib
输入:
sudo ldconfig
出现问题:/sbin/ldconfig.real: /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8 不是符号链接
解决办法:
sudo ldconfig -v
在终端输出中找到/usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8 不是符号链接这样的提示,终端输入:替换成自己的就行。
sudo ln -sf /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.2.2 /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8
sudo ln -sf /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.2.2 /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_ops_train.so.8
sudo ln -sf /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.2.2 /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8
sudo ln -sf /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.2.2 /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8
sudo ln -sf /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn.so.8.2.2 /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn.so.8
sudo ln -sf /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.2.2 /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8
sudo ln -sf /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.2.2 /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_adv_train.so.8
直到再次输入:
sudo ldconfig
sudo gedit /etc/bash.bashrc
加入
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
终端输入
source /etc/bash.bashrc
sudo updatedb
如果报错:sudo: updatedb:找不到命令
先安装:
sudo apt-get install mlocate
再次输入
sudo updatedb
结束!
进入前面的build文件夹下,打开终端输入:
cd ../samples/cpp/example_cmake
cmake .
make
./opencv_example