Ubuntu 24.04安裝OpenCV4和环境配置
8 min readApr 26, 2020
安装cmake(编译器)和依赖库
sudo apt install cmake gcc g++
支持python
sudo apt install -y python3-dev python3-numpy
sudo apt install -y libavcodec-dev libavformat-dev libswscale-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libgtk2.0-dev libgtk-3-dev libpng-dev libjpeg-dev libopenexr-dev libtiff-dev libwebp-dev
下载源文件
git clone https://github.com/opencv/opencv
git clone https://github.com/opencv/opencv_contrib
進入opencv的文件夾创建一个编译文件夹build,并进入
mkdir build && cd build/
開始make
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D WITH_FFMPEG=ON \
-D WITH_GTK=ON \
-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.12 \
-D PYTHON_INCLUDE_DIR=/usr/include/python3.12 \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
進行make編譯
make -j16
sudo make install
(可選項) 如果需要opencv的cuda功能,請用下面的指令make,但是會時間非常久!!!
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D WITH_FFMPEG=ON \
-D WITH_CUDA=ON \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_OPENGL=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D WITH_GTK=ON \
-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.12 \
-D PYTHON_INCLUDE_DIR=/usr/include/python3.12 \
-D OPENCV_DNN_CUDA=ON \
-D CUDA_ARCH_BIN=8.6 \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
因爲我這裡的python是選擇安裝啊Anaconda3的目錄下,請自行修改,另外根據你自己的GPU選擇適合的CUDA_ARCH_BIN,RTX 3090可以選擇8.6
配置環境
將opencv4.pc文件複製到pkgconfig文件夾裡
sudo cp /usr/lib/x86_64-linux-gnu/pkgconfig/opencv4.pc /usr/lib/pkgconfig/
下面可以直接去測試看看了,如果不行再做下面剩下的測試
$ sudo find / -iname opencv4.pc/usr/lib/x86_64-linux-gnu/pkgconfig/opencv4.pc
/usr/lib/pkgconfig/opencv4.pc
/usr/local/lib/pkgconfig/opencv4.pc
find: ‘/run/user/1000/gvfs’: Permission denied
将/usr/local/lib/pkgconfig/路径加入PKG_CONFIG_PATH:
$ sudo vim /etc/profile.d/pkgconfig.sh
在文件中加入下面一行:
export
PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
保存退出后,使用命令激活:
$source /etc/profile
验证是否配置成功:
$ pkg-config --libs opencv4
-L/usr/local/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_videoio -lopencv_viz -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_dnn -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
动态库环境
配置OpenCV动态库环境 — — 程序执行时加载动态库*.so的路径。
$ sudo vim /etc/ld.so.conf.d/opencv4.conf
在该文件(可能是空文件)末尾加上:
/usr/local/lib
再执行以下命令使刚才配置的路径生效:
$ sudo ldconfig
python-opencv环境
找到编译好的python cv库:
$ sudo find / -iname cv2*.so
/usr/lib/python3.12/site-packages/cv2/python-3.12/cv2.cpython-312-x86_64-linux-gnu.so
cv2.cpython-312m-x86_64-linux-gnu.so就是編譯好的python3的opencv庫,把它複製到python解释器/path/to/site-packages目錄下,之後就可以在該python解釋器中使用python-opencv了。
鏈接到Anaconda創建的python3解釋器中:
$ ln -s /usr/lib/python3.12/dist-packages/cv2/python-3.12/cv2.cpython-312-x86_64-linux-gnu.so ~/anaconda3/lib/python3.12/site-packages/cv2.so
测试OpenCV
cd 到/opencv/samples/cpp/example_cmake目录下,查看当前目录内容:
$ mkdir build && cd build
$ cmake ..
$ make
$ ./opencv_example