一文搞定Ubuntu编译安装OpenCV(附网盘链接)

文章目录

    • 1 下载
    • 2 安装依赖
    • 3 编译OpenCV
    • 4 配置环境
    • 5 测试
    • 相关传送门

1 下载

首先去opencv官网下载你需要的opencv包,本文下载的是opencv-3.4.7。
本文最大的福利来了,百度网盘链接分享给大家
链接: https://pan.baidu.com/s/1UZRP1zWhYlUdudY5a9nXXg
密码: 6amg

2 安装依赖

这是本人第一次安装时走的唯一弯路,当时抛出异常cv实例调用出错,这里要先下载gtk包

chiyukunpeng@chiyukunpeng:~$ sudo apt-get install libgtk2.0-dev

3 编译OpenCV

首先进入解压后的opencv安装包

chiyukunpeng@chiyukunpeng:~/download$ cd opencv-3.4.7/

创建一个新的编译目录

chiyukunpeng@chiyukunpeng:~/download/opencv-3.4.7$ mkdir opencv-build

进入编译目录之前,请删除CMakeCache.txt

chiyukunpeng@chiyukunpeng:~/download/opencv-3.4.7$ rm CMakeCache.txt

进入编译目录,编译opencv

chiyukunpeng@chiyukunpeng:~/download/opencv-3.4.7$ cd opencv-build/
chiyukunpeng@chiyukunpeng:~/download/opencv-3.4.7/opencv-build$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_GTK=ON -D WITH_OPENGL=ON ..
chiyukunpeng@chiyukunpeng:~/download/opencv-3.4.7/opencv-build$ make #这一步需要3-5分钟
chiyukunpeng@chiyukunpeng:~/download/opencv-3.4.7/opencv-build$ sudo make install #编译过程结束

4 配置环境

首先将opencv添加到路径,让系统可以找到它,输入下面的命令,打开opencv.conf文件(空文件),在文件末尾加上/usr/local/lib

chiyukunpeng@chiyukunpeng:~/download/opencv-3.4.7/opencv-build$ sudo gedit /etc/ld.so.conf.d/opencv.conf

然后执行下列命令使刚才的配置路径生效

chiyukunpeng@chiyukunpeng:~/download/opencv-3.4.7/opencv-build$ sudo ldconfig 

配置bash,在打开的文件末尾添加
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

chiyukunpeng@chiyukunpeng:~/download/opencv-3.4.7/opencv-build$ sudo gedit /etc/bash.bashrc 

然后执行下列命令使刚才的配置生效

chiyukunpeng@chiyukunpeng:~/download/opencv-3.4.7/opencv-build$ source /etc/bash.bashrc

更新

chiyukunpeng@chiyukunpeng:~/download/opencv-3.4.7/opencv-build$ sudo updatedb 

5 测试

先进入官方测试样例

chiyukunpeng@chiyukunpeng:~/download/opencv-3.4.7$ cd samples/cpp/example_cmake/

编译

chiyukunpeng@chiyukunpeng:~/download/opencv-3.4.7/samples/cpp/example_cmake$ cmake .
chiyukunpeng@chiyukunpeng:~/download/opencv-3.4.7/samples/cpp/example_cmake$ make

运行

chiyukunpeng@chiyukunpeng:~/download/opencv-3.4.7/samples/cpp/example_cmake$ ./opencv_example 

测试成功是这样的输出,且会弹出摄像头界面

Built with OpenCV 3.4.7
Capture is opened

相关传送门

计算机视觉工程师的万里长征路(附完整学习资料)

你可能感兴趣的:(CV环境搭建)