ubuntu18.04安装opencv4.5.5
时间: 2024-12-26 09:13:57 浏览: 4
在Ubuntu 18.04上安装OpenCV 4.5.5需要一些额外步骤,因为官方包可能不会包含这个版本。以下是详细的安装过程:
1. **更新系统**:
```
sudo apt-get update && sudo apt-get upgrade
```
2. **安装依赖库**:
OpenCV通常依赖于许多其他库,如`cmake`, `build-essential`, `git`, 和 `pkg-config`。运行以下命令安装它们:
```
sudo apt-3-dev \
libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
libxvidcore-dev x264 v4l-utils libjpeg-dev libpng-dev libtiff-dev \
libwebp-dev python3-pip libtbb2 libtbb-dev libdc1394-22-dev \
libopenblas-dev liblapack-dev gfortran libeigen3-dev libhdf5-dev
```
3. **下载和解压OpenCV源码**:
```
wget https://github.com/opencv/opencv/archive/4.5.5.tar.gz
tar -xzvf opencv-4.5.5.tar.gz
cd opencv-4.5.5
```
4. **配置编译选项**:
切换到OpenCV目录并创建一个新的CMakeLists.txt文件,例如:
```sh
mkdir build
cd build
cp ../CMakeLists.txt .
```
打开`CMakeLists.txt`,将`PYTHON3_LIBRARY`设置为你系统的Python路径(通常是`python3`),然后添加以下行:
```cpp
set(PYTHON3_EXECUTABLE /usr/bin/python3)
find_package(Python3 REQUIRED)
include_directories(${PYTHON3_INCLUDE_DIR})
link_directories(${PYTHON3_LIBRARY_DIRS})
add_subdirectory ..
```
5. **构建OpenCV**:
```
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D OPENCV_PYTHON3_INSTALL_PATH=/usr/local/lib/python3/dist-packages/opencv4 \
-D PYTHON3_EXECUTABLE=/usr/bin/python3 ..
```
如果没有错误,运行:
```
make -j$(nproc)
sudo make install
```
6. **验证安装**:
最后,检查是否成功安装了OpenCV:
```sh
python3 -c "import cv2; print(cv2.__version__)"
```
阅读全文