Ubuntu 18.04 配置 OpenCV 3.4.5 並編寫測試代碼

1.什麼是OpenCV?

OpenCV是一個基於BSD許可(開源)發行的跨平臺計算機視覺庫,可以運行在Linux、Windows、Android和Mac OS操作系統上。它輕量級而且高效——由一系列 C 函數和少量 C++ 類構成,同時提供了Python、Ruby、MATLAB等語言的接口,實現了圖像處理和計算機視覺方面的很多通用算法。
OpenCV用C++語言編寫,它的主要接口也是C++語言,但是依然保留了大量的C語言接口。該庫也有大量的Python、Java and MATLAB/OCTAVE(版本2.5)的接口。這些語言的API接口函數可以通過在線文檔獲得。如今也提供對於C#、Ch、Ruby,GO的支持。
——摘自百度

2.準備工作

首先,要更新 Ubuntu 的庫

sudo apt-get update
sudo apt-get upgrade

然後安裝一些 OpenCV 會用的庫(這一步一定要認真做,否則後面會出現各種莫名其妙的錯誤!!!

sudo apt-get install build-essential cmake libgtk2.0-dev libtiff4-dev libjasper-dev libavformat-dev libswscale-dev libavcodec-dev libjpeg62-dev pkg-config ffmpeg

在這一步中可能會遇到報錯:

沒有可用的軟件包 libtiff4-dev,但是它被其它的軟件包引用了。
這可能意味着這個缺失的軟件包可能已被廢棄,
或者只能在其他發佈源中找到
然而下列軟件包會取代它:
libtiff5-dev:i386 libtiff5-dev

解決方案是(其實是隨便選了第二個…):

sudo apt-get install libtiff5-dev

尤其是 libgtk2.0-dev 和 pkg-config 這兩個庫,否則在後面運行例程時會報錯:

terminate called after throwing an instance of ‘cv::Exception’
what(): OpenCV(3.4.5) /home/cxy/文檔/opencv-3.4.5/modules/highgui/src/window.cpp:617: error: (-2: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 function ‘cvNamedWindow’

如果忘記了裝上面的某個庫,在補裝了以後要重新執行第4部分中的內容纔可以正常編譯工程。

3.OpenCV 下載

直接從官網提供的鏈接下載就行:OpenCV source download
下載解壓後放在合適的位置,進入解壓目錄,進行如下操作,爲OpenCV的編譯作準備:

mkdir release
cd release

4.編譯 OpenCV

首先,設置 cmake 編譯參數:

sudo cmake …

然後開始編譯 OpenCV:

sudo make
sudo make install

注:編譯第一步可能耗時比較久,建議找點其他事做!

5.配置環境變量

網上可以查到很多種方法,這裏提供一種親測可行的方法,僅供參考:

sudo gedit /etc/ld.so.conf.d/opencv.conf

在文件 opencv.conf 內的末尾添加 “/usr/local/lib” 並保存;
更新系統的共享鏈接庫:

sudo ldconfig

下一步:

sudo gedit /etc/bash.bashrc

在 bash.bashrc 的末尾添加 “export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig”

source /etc/bash.bashrc

至此,OpenCV環境搭建完畢(大功告成.jpg)。

6.編寫測試項目

一開始以爲配好環境,寫一個 cpp 文件就可以運行了,其實不然…
正解:

6.1 創建工程目錄,並進入目錄

mkdir testcv
cd testcv

工程的名字根據個人喜好決定

6.2 創建cpp文件並編寫測試代碼

創建cpp文件:

sudo gedit test.cpp

編寫代碼如下:

#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
    cout << "Hello OpenCV " << CV_VERSION << endl;
    // Load image
    Mat myMat = imread("test.jpg", 1);
    // Create a window
    cvNamedWindow("Opencv Image", WINDOW_AUTOSIZE);
    // Show the image
    imshow("Opencv Image", myMat);
    // Wait 5000 ms
    waitKey(5000);
    return 0;
}

6.3 創建CMakeLists.txt文件,編輯編譯代碼

以下內容,直接複製粘貼的(傳送門
查看 opencv-3.4.5/samples/cpp/example_cmake 路徑下的 CMakeLists.txt:

這個是 cmake 的編譯文件,主要用於生成 Makefile,然後用 make 編譯工程

# cmake needs this line
cmake_minimum_required(VERSION 2.8)
 
# Define project name
project(opencv_example_project)
 
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
 
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
 
if(CMAKE_VERSION VERSION_LESS "2.8.11")
  # Add OpenCV headers location to your include paths
  include_directories(${OpenCV_INCLUDE_DIRS})
endif()
 
# Declare the executable target built from your sources
add_executable(opencv_example example.cpp)
 
# Link your application with OpenCV libraries
target_link_libraries(opencv_example ${OpenCV_LIBS})

將其中的一部分按照自己的情況修改:

# cmake needs this line 要求的最低版本
cmake_minimum_required(VERSION 2.8)
# Define project name 定義工程名 
project(example_project)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI 自動查找庫
find_package(OpenCV REQUIRED)
# Declare the executable target built from your sources 聲明可執行目標文件及源文件
add_executable(example example.cpp)  # 目標文件,源文件0,源文件1,...
# Link your application with OpenCV libraries 將目標文件與庫鏈接
target_link_libraries(example ${OpenCV_LIBS})  # 目標文件,庫路徑

這裏附上自己修改過的 CMakeLists.txt 文件:

# cmake needs this line
cmake_minimum_required(VERSION 2.8)
 
# Define project name
project(testcv)
 
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
 
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
 
if(CMAKE_VERSION VERSION_LESS "2.8.11")
  # Add OpenCV headers location to your include paths
  include_directories(${OpenCV_INCLUDE_DIRS})
endif()
 
# Declare the executable target built from your sources
add_executable(test test.cpp)
 
# Link your application with OpenCV libraries
target_link_libraries(test ${OpenCV_LIBS})

6.4 編譯並運行工程

生成Makefile:

cmake .

編譯工程:

make

運行可執行文件:

./test

在這裏插入圖片描述

參考:

1.OpenCV 環境搭建
2.Ubuntu 創建 OpenCV 工程

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章