ubuntu20.04使用官网的方法安装opencv
时间: 2024-05-13 21:13:08 浏览: 133
ubuntu下编译安装opencv的方法
在Ubuntu20.04上安装OpenCV,可以按照以下步骤进行:
1.更新Ubuntu软件包
```shell
sudo apt-get update
sudo apt-get upgrade
```
2.安装依赖项
```shell
sudo apt-get install build-essential cmake git pkg-config libgtk-3-dev \
libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \
gfortran openexr libatlas-base-dev python3-dev python3-numpy \
libtbb2 libtbb-dev libdc1394-22-dev libopenexr-dev \
libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
```
3.下载OpenCV源代码
```shell
cd ~/
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout 4.5.2
```
4.下载OpenCV_contrib源代码
```shell
cd ~/
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 4.5.2
```
5.编译和安装OpenCV
```shell
cd ~/opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_TBB=ON \
-D WITH_OPENEXR=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D PYTHON_DEFAULT_EXECUTABLE=$(which python3) \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_DOCS=OFF \
-D BUILD_JAVA=OFF \
-D BUILD_opencv_java=OFF \
-D BUILD_opencv_python_bindings_generator=OFF ..
make -j$(nproc)
sudo make install
```
以上步骤包括下载源代码、配置编译环境、编译和安装OpenCV,其中第5步是最关键的步骤。
如果遇到最后一步报错"Package opencv was not found in the pkg-config search path.",可以按照以下步骤解决:
1.切换到/usr/local/lib/pkgconfig目录
```shell
cd /usr/local/lib/pkgconfig
```
2.在该目录下创建opencv.pc文件
```shell
sudo touch opencv.pc
```
3.使用vim编辑opencv.pc文件
```shell
sudo vim opencv.pc
```
4.将以下内容粘贴到opencv.pc文件中并保存
```
prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include/opencv4
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.5.2
Libs: -L${libdir} -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_objdetect -lopencv_androidcamera -lopencv_optflow -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_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_face3d -lopencv_plot -lopencv_videostab -lopencv_video -lopencv_ml -lopencv_ximgproc -lopencv_videoio -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_video -I${includedir}
```
5.重新执行最后一步即可。
阅读全文