ubuntu中安装opencv的c++和python
时间: 2024-04-28 10:20:38 浏览: 268
Ubuntu下安装opencv-python(详解)
5星 · 资源好评率100%
在Ubuntu中安装OpenCV的C++和Python可以按照以下步骤进行:
1. 打开终端,输入以下命令更新Ubuntu软件列表:
```
sudo apt-get update
```
2. 安装OpenCV所需的依赖库:
```
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
```
3. 下载OpenCV的源代码:
```
git clone https://github.com/opencv/opencv.git
```
4. 编译和安装OpenCV:
```
cd opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j4
sudo make install
sudo ldconfig
```
5. 测试OpenCV是否安装成功:
```
pkg-config --modversion opencv
```
如果显示了OpenCV的版本号,说明安装成功。
对于C++,可以在编译时链接OpenCV库文件,例如:
```
g++ -o program program.cpp `pkg-config --cflags --libs opencv`
```
对于Python,可以使用pip安装opencv-python包:
```
pip install opencv-python
```
然后在Python代码中导入OpenCV模块即可:
```python
import cv2
```
阅读全文