Ubuntu18.04怎么配置opencv的c语言环境
时间: 2024-10-16 14:04:36 浏览: 32
Ubuntu18.04安装opencv 3.2.0的解决方法
在Ubuntu 18.04上安装和配置OpenCV用于C语言环境通常需要几个步骤:
1. **更新系统**:
```sh
sudo apt-get update
sudo apt-get upgrade
```
2. **安装依赖库**:
```sh
sudo apt-get install build-essential cmake pkg-config 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-pip
```
3. **下载OpenCV源码**:
```
wget https://github.com/opencv/opencv/archive/4.5.2.tar.gz
wget https://github.com/opencv/opencv_contrib/archive/4.5.2.tar.gz
```
解压并创建目录:
```sh
tar -xzvf opencv-4.5.2.tar.gz opencv_contrib-4.5.2.tar.gz
mkdir opencv_build
cd opencv_build
```
4. **配置OpenCV**:
```sh
cmake ..
```
可能需要指定额外的选项,比如添加`-DCMAKE_BUILD_TYPE=Release`来优化编译。
5. **编译和安装**:
```sh
make -j4
sudo make install
```
6. **检查安装**:
```sh
opencv_version=$(pkg-config --modversion opencv)
echo "OpenCV version installed: $opencv_version"
```
7. **设置环境变量**(如果要在新终端窗口生效):
```bash
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
```
8. **验证是否安装成功**:
编写一个小示例程序测试OpenCV功能:
```c
#include <opencv2/opencv.h>
int main(int argc, char** argv) {
cv::Mat img = cv::imread("test.jpg");
if (img.empty()) {
std::cout << "Failed to load image" << std::endl;
} else {
std::cout << "Image loaded successfully!" << std::endl;
}
return 0;
}
```
然后尝试运行这个程序。
阅读全文