Ubuntu20.04linux下编译安装opencvpython
时间: 2023-11-12 18:08:06 浏览: 138
arm 鲁班猫LubanCat-RK3588 Ubuntu20.04 源码编译安装opencv20.04
以下是在 Ubuntu 20.04 系统中编译安装 OpenCV-Python 的步骤:
1. 安装依赖项
```bash
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential cmake git libgtk2.0-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 libdc1394-22-dev
```
2. 克隆 OpenCV 仓库
```bash
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout 4.5.2
```
3. 克隆 OpenCV-contrib 仓库(可选)
```bash
cd ..
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 4.5.2
```
4. 编译 OpenCV
```bash
cd ../opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3 \
-D PYTHON3_EXECUTABLE=/usr/bin/python3 \
-D PYTHON3_INCLUDE_DIR=/usr/include/python3.8 \
-D PYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.8.so \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include \
-D BUILD_opencv_python3=YES \
-D BUILD_opencv_python2=NO \
-D BUILD_EXAMPLES=NO \
-D BUILD_TESTS=NO \
-D BUILD_PERF_TESTS=NO ..
make -j8
sudo make install
sudo ldconfig
```
其中,`-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules` 参数是为了编译 OpenCV-contrib 模块,如果不需要可以省略。
5. 测试 OpenCV-Python
```bash
python3
>>> import cv2
>>> cv2.__version__
```
如果输出的版本号是 4.5.2,则说明安装成功。
以上是在 Ubuntu 20.04 系统中编译安装 OpenCV-Python 的步骤,希望能帮助到你。
阅读全文