ubuntu18.04安装opencv3.4.0

下载解压

由于学习YOLOv3-darknet,需要安装OpenCV3.4.0。
官网地址:https://opencv.org/releases.html
从opencv官网下载opencv3.4.0以及opencv_contrib压缩包。将下载的opencv3.4.0及opencv_contrib-3.4.0解压,并将opencv_contrib-3.4.0的文件夹放到opencv3.4.0的文件夹中。

环境配置

打开终端,输入如下指令:

sudo apt-get install build-essential
sudo apt-get install 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

若出现错误 E: Unable to locate package libjasper-dev,依次执行如下命令:

sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev

附加安装(可跳过)

由于YOLOv3-darknet进行视频检测需要显示图片,会遇到以下问题:

OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvShowImage, file /io/opencv/modules/highgui/src/window.cpp, line 593

所以还要安装以下两个文件:

sudo apt-get install libgtk2.0-dev pkg-config

安装成功之后,运行cmake对opencv进行编译,安装

cmake

cd opencv-3.4.0
mkdir build
# 创建编译文件
cd build
# 正常情况下使用如下cmake命令即可
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
# 但是,由于cuda10.0对环境有点不支持,可能需要加上-D BUILD_opencv_cudacodec=OFF
cmake -D CMAKE_BUILD_TYPE=Release -D BUILD_opencv_cudacodec=OFF -D CMAKE_INSTALL_PREFIX=/usr/local ..

cmake编译:
ubuntu18.04安装opencv3.4.0_第1张图片

安装

    # 4个线程编译,查看cpu核数量
    make -j4
    # 安装
    sudo make install

两步安装:
ubuntu18.04安装opencv3.4.0_第2张图片ubuntu18.04安装opencv3.4.0_第3张图片

配置opencv编译环境,将其添加到系统路径中

# 打开配置文件,可能是空文件
sudo gedit /etc/ld.so.conf.d/opencv.conf
# 在出现的编辑器中加上如下,保存
/usr/local/lib
# 使配置生效
sudo ldconfig  
# 配置bash.bash.rc
sudo gedit /etc/bash.bashrc
# 在最后面添加下面两条路径
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig  
export PKG_CONFIG_PATH
# 保存,使配置生效
source /etc/bash.bashrc
sudo updatedb

测试

进入opencv/samples/cpp目录下,有一个opencv自带的例程,我们对此例程进行编译

cd ../samples/cpp/example_cmake
cmake .
make
./opencv_example

摄像头打开,opencv3.4.0已经能够正常使用了。

参考(感谢)

1、https://blog.csdn.net/qq_36059536/article/details/107356389
2、https://blog.csdn.net/qq_38236355/article/details/88864023?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.control
3、https://blog.csdn.net/kevineeo/article/details/83242516?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control

你可能感兴趣的:(OpenCV,opencv,ubuntu)