1. 程式人生 > >Ubuntu 18.04編譯OpenCV + CMake構建首個工程

Ubuntu 18.04編譯OpenCV + 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_BUILE_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_EXTRA_MODULES_PATH=../../opencv_contrib/modules -D BUILE_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相關路徑)
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})

執行cmake ..即可。
最後,在bin目錄下執行helloopencv即可。
main.cpp的內容如下:

#include <iostream>
#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;
}