在ubuntu22.04如何用命令行安装opencv
时间: 2024-12-06 20:23:50 浏览: 16
ubuntu22.04离线安装gcc g++ make cmake opencv
在Ubuntu 22.04上通过命令行安装OpenCV通常需要几个步骤,因为默认的软件包源可能不包含最新版本的OpenCV。以下是安装OpenCV的基本过程:
1. 更新软件包列表并添加PPA(Personal Package Archive):
```bash
sudo apt update
sudo add-apt-repository ppa:itk-ppa/ppa
```
2. 安装依赖项:
```bash
sudo apt-get install -y build-essential cmake libjpeg-dev libtiff-dev libjasper-dev libpng-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libgtk-3-dev libatlas-base-dev gfortran python3-dev python3-pip
```
3. 获取OpenCV源码:
```bash
wget https://github.com/opencv/opencv/archive/4.x.zip
unzip 4.x.zip
cd opencv-4.x
```
4. 配置并编译OpenCV:
```bash
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D OPENCV_EXTRA_MODULES_PATH=/path/to/your/opencv_contrib/modules -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF ..
make -j$(nproc)
```
记得将`/path/to/your/opencv_contrib/modules`替换为你的OpenCV贡献模块路径,如果没有贡献模块则忽略。
5. 安装OpenCV:
```bash
sudo make install
```
6. 最后,安装Python接口:
```bash
pip3 install numpy
cd ..
python3 setup.py install
```
阅读全文