当前位置:   article > 正文

ubuntu22.04编译安装opencv和opencv_contrib_ubuntu22.04安装opencv

ubuntu22.04安装opencv

1. 下载OpenCV和OpenCV源文件

opencv: https://opencv.org/releases/
opencv_contrib: https://github.com/opencv/opencv_contrib
注意opencv_contrib和opencv版本要对应
下载后将安装包解压到主目录,重命名为opencvopencv_contrib

2. 安装依赖项

安装g++, cmake, make, wget, unzip

sudo apt install -y g++ 
sudo apt install -y cmake 
sudo apt install -y make 
sudo apt install -y wget 
sudo apt install -y unzip
  • 1
  • 2
  • 3
  • 4
  • 5

安装opencv依赖的基本库

sudo apt-get install build-essential libgtk2.0-dev libgtk-3-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev
  • 1

3. 编译安装

如果提前安装了anaconda, 建议先把anaconda环境变量注释掉,并把anaconda文件夹移进回收站等装完再移出来(改软连接也行,但上述更无脑些)

主目录下打开终端

  1. 新建build
cd opencv
mkdir build
cd build
  • 1
  • 2
  • 3
  1. cmake配置
 cmake -DCMAKE_BUILD_TYPE=RELEASE \
    -DCMAKE_INSTALL_PREFIX=/usr/local \
    -DOPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules .. \
    -DWITH_CUDA=1 \
    -DCUDA_ARCH_BIN=8.6 \
    -DENABLE_FAST_MATH=1 \
    -DCUDA_FAST_MATH=1 \
    -DWITH_CUBLAS=1 \
    -DOPENCV_GENERATE_PKGCONFIG=1 \
    ..
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

注意:上面的DCUDA_ARCH_BIN指定GPU算力,在NVIDIA官网查询 https://developer.nvidia.com/cuda-gpus#compute,常用的30系显卡是8.6,40系显卡是8.9

  1. make编译
    可以先查一下最多用多少线程
nproc
  • 1

比如输出 16
那么就

make -j16
  • 1
  1. 用make进行安装
sudo make install 
  • 1

4. 环境配置

  1. 配置pkg-config环境
sudo gedit /etc/profile.d/pkgconfig.sh
  • 1

可能为空文件,在文件中加入

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
  • 1

保存并退出,source激活

source /etc/profile
  • 1

验证是否成功

pkg-config --libs opencv4
  • 1

输出类似下面这样即为成功

-L/usr/local/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_barcode -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cudabgsegm -lopencv_cudafeatures2d -lopencv_cudaobjdetect -lopencv_cudastereo -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -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_cudacodec -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_cudaoptflow -lopencv_optflow -lopencv_cudalegacy -lopencv_videoio -lopencv_cudawarping -lopencv_wechat_qrcode -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_dnn -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_cudaimgproc -lopencv_cudafilters -lopencv_imgproc -lopencv_cudaarithm -lopencv_core -lopencv_cudev
  • 1
  1. 配置动态库环境
sudo gedit /etc/ld.so.conf.d/opencv4.conf
  • 1

文件末尾加上

/usr/local/lib
  • 1

使配置生效

sudo ldconfig
  • 1

5. demo测试

打开新终端,进入到example_cmake目录

cd ~/opencv/samples/cpp/example_cmake
  • 1
cmake .
make
./opencv_example
  • 1
  • 2
  • 3

正常来说,应该有摄像头窗口界面;如果没有摄像头,出现“Hello OpenCV“也测试成功

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/119879
推荐阅读
相关标签
  

闽ICP备14008679号