Ubuntu 18.04编译OpenCV4.0 + CMake构建首个工程

参考链接

Install OpenCV 4 on Ubuntu 18.04 (C++ and Python)

安装依赖

按照参考链接中的内容,安装依赖项,可能需要根据个人情况进行修改,以下是我安装的内容:

// Step 1: Update Packages
sudo apt -y update
sudo apt -y upgrade

// Step 2: Install OS Libraries
## Install dependencies
sudo apt -y install build-essential checkinstall cmake pkg-config yasm
sudo apt -y install git gfortran
sudo apt -y install libjpeg8-dev libpng-dev
 
sudo apt -y install software-properties-common
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt -y update
 
sudo apt -y install libjasper1
sudo apt -y install libtiff-dev
 
sudo apt -y install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
sudo apt -y install libxine2-dev libv4l-dev

sudo apt -y install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt -y install libgtk2.0-dev libtbb-dev qt5-default
sudo apt -y install libatlas-base-dev
sudo apt -y install libfaac-dev libmp3lame-dev libtheora-dev
sudo apt -y install libvorbis-dev libxvidcore-dev
sudo apt -y install libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt -y install libavresample-dev
sudo apt -y install x264 v4l-utils
 
# Optional dependencies
sudo apt -y install libprotobuf-dev protobuf-compiler
sudo apt -y install libgoogle-glog-dev libgflags-dev
sudo apt -y install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

编译

  • 下载OpenCV源码:opencv + opencv_contrib
// opencv
git clone [email protected]:opencv/opencv.git
// opencv_contrib
git clone [email protected]:opencv/opencv_contrib.git

此时的目录结构为:
~/Downloads/opencv
~/Downloads/opencv_contrib
在opencv目录下建立build目录:

cd opencv
mkdir build
cd build

执行CMake:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/opencv -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D BUILD_TIFF=ON -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules -D BUILD_EXAMPLES=ON ..

这里的cmake选项可能会影响后面make能否顺利通过。
这里我指定了opencv的install path为/usr/local/opencv,opencv_contrib的源码路径在../../opencv_contrib/modules。
第一次我没有加入-D BUILD_TIFF=ON选项,导致make过程中找不到tiff的库文件,解决方法就是增加这个选项。
然后make,make install即可。

CMake构建首个工程

工程目录结构如下:
project_dir/bin
project_dir/build
project_dir/src
project_dir/CMakeLists.txt
其中,src目录存放源码,编译结果存放在build目录下,bin目录存放可执行文件。
project_dir/CMakeLists.txt内容如下:

# project name
PROJECT(HELLO_WORLD)
# requirement of cmake version
CMAKE_MINIMUM_REQUIRED(VERSION 3.5)
# set the directory of executable files (指定可执行文件的路径为bin目录)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
# add source code files to SRC_LIST (添加源码文件)
SET(SRC_LIST ${PROJECT_SOURCE_DIR}/src/main.cpp)
# find required opencv (cmake可以自动寻找OpenCV相关路径,
# 当找到OpenCV程序库之后,就会定义几个变量:OpenCV_FOUND、
# OpenCV_INCLUDE_DIRS、OpenCV_LIBRARY_DIRS、OpenCV_LIBS,
# 它们分别指是否找到OpenCV、OpenCV的头文件目录、OpenCV的库文件目录、
# OpenCV的所有库文件列表)
FIND_PACKAGE(OpenCV REQUIRED)
# directory of opencv headers (OpenCV_INCLUDE_DIRS是cmake自动找到的目录)
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
# name of executable file and path of source file (指定可执行文件的名称为helloopencv以及源码文件)
ADD_EXECUTABLE(helloopencv ${SRC_LIST})
# directory of opencv library (OpenCV_LIBRARY_DIRS是cmake自动找到的目录)
LINK_DIRECTORIES(${OpenCV_LIBRARY_DIRS})
# opencv libraries (指定链接的对象)
TARGET_LINK_LIBRARIES(helloopencv ${OpenCV_LIBS})

在build目录下执行cmake ..即可。
最后,在bin目录下执行helloopencv即可。
main.cpp的内容如下:

#include 
#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
	Mat src_image = imread("../test.png");

	imshow("", src_image);
	waitKey(0);

	return 0;
}

问题记录

  1. 软连接问题
  • 问题描述
    在Mac上源码编译安装OpenCV4.0.1后,Xcode工程调用OpenCV时出现该问题。Ubuntu系统暂未发现。
    报错信息类似:
    dyld: Library not loaded: libopencv_dnn.4.0.dylib

    Reason: image not found
  • 可行的解决方案:依次修改每个软连接的路径。
  • 我的解决方案
    问题来自于cmake时指定了CMAKE_INSTALL_PREFIX选项:
-D CMAKE_INSTALL_PREFIX=/usr/local/opt/opencv4

该选项指定了make install的目标目录。
在cmake时不指定该选项,使用默认设置,make install会将头文件安装在/usr/local/include/opencv4目录,库文件安装在/usr/local/lib目录,Xcode工程目录要指定这两个目录,可以正常使用。

  • 无效的解决方案
    指定-D BUILD_SHARED_LIBS=OFF(不编译动态库,只生成.a文件)
    修改所有的CMakeLists.txt和OpenCVModule.cmake中的INSTALL_NAME_DIR lib为INSTALL_NAME_DIR ${CMAKE_INSTALL_PREFIX}/lib

你可能感兴趣的:(环境配置)