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
1. 下載opencv/opencv_contrib源代碼
這里直接從opencv的github倉庫clone最新的opencv源代碼到本地:
cd /home/yan/
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
在本地目錄home/yan/下面將會得到兩個文件夾:
yan@yanubuntu:~$ ll | grep opencv
drwxrwxr-x 15 yan yan 4096 9月 14 14:09 opencv/
drwxrwxr-x 7 yan yan 4096 9月 14 09:38 opencv_contrib/
2. cmake生成MakeFile文件
依次執(zhí)行以下命令:
cd /home/yan/opencv/
mkdir build
cd build
接下來是cmake命令:
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/home/yan/opencv_contrib/modules -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3 -D BUILD_opencv_python3=ON -D BUILD_opencv_python2=OFF -D PYTHON3_EXCUTABLE=/usr/bin/python3 -D PYTHON3_INCLUDE_DIR=/usr/include/python3.5m -D WITH_CUDA=OFF -D OPENCV_GENERATE_PKGCONFIG=ON ..
對cmake的某些參數(shù)作解釋:
cmake
-D CMAKE_BUILD_TYPE=Release
-D CMAKE_INSTALL_PREFIX=/usr/local
# 如果要安裝opencv_contrib模塊宵统,需要指定對應(yīng)的modules文件夾位置
-D OPENCV_EXTRA_MODULES_PATH=/home/yan/opencv_contrib/modules
# 基本用不到python2趣钱,因此這里只編譯python3的opencv版本
-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3
-D BUILD_opencv_python3=ON
-D BUILD_opencv_python2=OFF
-D PYTHON3_EXCUTABLE=/usr/bin/python3
-D PYTHON3_INCLUDE_DIR=/usr/include/python3.5m
# 不需要使用opencv的GPU加速,將CUDA選項關(guān)掉
-D WITH_CUDA=OFF
# 這個選項非常重要星岗,會生成一個被pkg-config模塊使用的opencv4.pc文件,編譯opencv的c++項目時可能會用到
-D OPENCV_GENERATE_PKGCONFIG=ON
cmake過程中會下載某些文件戒洼,比較慢俏橘,請耐心等待。
3. make & make install
cmake完成后圈浇,依次執(zhí)行:
make -j8
sudo make install
make編譯過程也比較慢寥掐,通過 -j8 實現(xiàn)多線程加速。
4. 后處理
打開home目錄下的.bashrc文件:
sudo vim ~/.bashrc
在最后添加如下環(huán)境變量:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
該目錄下面有一個opencv4.pc文件磷蜀,查看一下里面有哪些內(nèi)容:
yan@yanubuntu:~$ ls /usr/local/lib/pkgconfig/
libspatialindex.pc opencv4.pc
yan@yanubuntu:~$ cat /usr/local/lib/pkgconfig/opencv4.pc
# Package Information for pkg-config
prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv4/opencv
includedir_new=${prefix}/include/opencv4
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.1.2
Libs: -L${exec_prefix}/lib -lopencv_stitching -lopencv_gapi -lopencv_bgsegm -lopencv_img_hash -lopencv_xfeatures2d -lopencv_shape -lopencv_superres -lopencv_freetype -lopencv_aruco -lopencv_dnn_objdetect -lopencv_ccalib -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_stereo -lopencv_tracking -lopencv_datasets -lopencv_plot -lopencv_rgbd -lopencv_fuzzy -lopencv_line_descriptor -lopencv_reg -lopencv_hfs -lopencv_bioinspired -lopencv_surface_matching -lopencv_optflow -lopencv_ximgproc -lopencv_quality -lopencv_dpm -lopencv_highgui -lopencv_xobjdetect -lopencv_saliency -lopencv_hdf -lopencv_text -lopencv_dnn -lopencv_ml -lopencv_face -lopencv_objdetect -lopencv_videostab -lopencv_video -lopencv_calib3d -lopencv_videoio -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir_old} -I${includedir_new}
該opencv4.pc文件(貌似opencv3.x之前是生成名為opencv.pc的文件召耘,這里編譯的是opencv4.1.2,因此變成了opencv4.pc)是在前面cmake時通過添加-D OPENCV_GENERATE_PKGCONFIG=ON
選項自動生成的褐隆。
該文件實際上也就是指定了安裝opencv生成的 Include/包含目錄 和 Libs/庫目錄 的路徑污它,以便在使用g++編譯包含opencv函數(shù)的c++代碼時,就可以使用 pkg-config 選項指定opencv庫的搜索路徑。
有了 opencv4.pc 文件衫贬,以及將該文件路徑添加到 PKG_CONFIG_PATH 環(huán)境變量之后德澈。就可以通過pkg-config命令來查看安裝的opencv的一些信息了,比如:
# 查看opencv的版本號
yan@yanubuntu:~$ pkg-config --modversion opencv4
4.1.2
# 查看opencv的Include目錄/頭文件路徑
yan@yanubuntu:~$ pkg-config --cflags opencv4
-I/usr/local/include/opencv4/opencv -I/usr/local/include/opencv4
# 查看opencv的Libs目錄/庫目錄
yan@yanubuntu:~$ pkg-config --libs opencv4
-L/usr/local/lib -lopencv_stitching -lopencv_gapi -lopencv_bgsegm -lopencv_img_hash -lopencv_xfeatures2d -lopencv_shape -lopencv_superres -lopencv_freetype -lopencv_aruco -lopencv_dnn_objdetect -lopencv_ccalib -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_stereo -lopencv_tracking -lopencv_datasets -lopencv_plot -lopencv_rgbd -lopencv_fuzzy -lopencv_line_descriptor -lopencv_reg -lopencv_hfs -lopencv_bioinspired -lopencv_surface_matching -lopencv_optflow -lopencv_ximgproc -lopencv_quality -lopencv_dpm -lopencv_highgui -lopencv_xobjdetect -lopencv_saliency -lopencv_hdf -lopencv_text -lopencv_dnn -lopencv_ml -lopencv_face -lopencv_objdetect -lopencv_videostab -lopencv_video -lopencv_calib3d -lopencv_videoio -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
5. 測試: 使用opencv顯示圖像
下面測試opencv的c++接口固惯,新建如下的DisplayImage.cpp文件:
// DisplayImage.cpp -- my first c++/opencv code in linux system
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
string img_path = "img.png";
Mat image;
image = imread(img_path);
if(image.data == NULL)
{
cout<<"No image data \n";
return -1;
}
namedWindow("DisplayImage", WINDOW_AUTOSIZE);
imshow("DisplayImage", image);
waitKey(0);
return 0;
}
假設(shè)cpp文件所在的文件夾下包含一個名為 img.png 的圖片文件梆造。
下面介紹如何編譯cpp文件到可執(zhí)行文件。有幾種方式:
5.1 g++帶pkg-config參數(shù)
命令如下:
yan@yanubuntu:~/Desktop/c_plus_opencv_sample$ ls
DisplayImage.cpp img.png
yan@yanubuntu:~/Desktop/c_plus_opencv_sample$ g++ DisplayImage.cpp -o DisplayImage.out $(pkg-config --cflags --libs opencv4) --std=c++11
yan@yanubuntu:~/Desktop/c_plus_opencv_sample$ ls
DisplayImage.cpp DisplayImage.out img.png
可以看到葬毫,編譯成功后镇辉,生成了可執(zhí)行文件DisplayImage.out。
運行可執(zhí)行文件DisplayImage.out:
yan@yanubuntu:~/Desktop/c_plus_opencv_sample$ ./DisplayImage.out
不出意外的話贴捡,圖片就顯示成功了:
這里是直接使用g++編譯cpp文件忽肛,解釋一下用到的編譯選項:
g++ DisplayImage.cpp
# 通過 -o 選項,指定生成的可執(zhí)行文件的文件名栈暇,如果不指定的話麻裁,默認(rèn)會生成一個 *a.out*的可執(zhí)行文件
-o DisplayImage.out
# 使用pkg-config選項指定cpp代碼中用到的所有opencv的包含文件以及庫文件,前面提到源祈,opencv4.pc中提供了這些路徑信息煎源。
$(pkg-config --cflags --libs opencv4)
# 使g++編譯器支持c++11特性
# 如果沒有這個選項,會出現(xiàn)類似error信息: error: #error "OpenCV 4.x+ requires enabled C++11 support"
--std=c++11
關(guān)于pkg-config工具的介紹請參考官網(wǎng)香缺。
以上面的cpp代碼為例手销,由于代碼中用到了opencv的兩個頭文件中定義的函數(shù):
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
因此,在g++編譯鏈接過程中图张,我們需要給編譯器提供opencv相關(guān)的包含文件路徑以及庫文件路徑锋拖,這樣g++才能生成完整的可執(zhí)行文件。
而pkg-config工具就是專門管理gcc/g++編譯過程中需要用到的依賴模塊路徑的:
- 前面我們編譯opencv時生成了一個opencv4.pc文件祸轮,該文件包含了所有opencv的包含文件/庫文件路徑兽埃。
- 通過將opencv4.pc文件的路徑添加到PKG_CONFIG_PATH 環(huán)境變量,pkg-config工具就掌握了opencv相關(guān)文件的所有路徑信息适袜。
- g++ 編譯時柄错,通過提供
$(pkg-config --cflags --libs opencv4)
選項,pkg-config工具會提供g++所需的opencv相關(guān)依賴文件的路徑苦酱。
不僅是opencv售貌,其他所有的庫的路徑都可以通過pkg-config來統(tǒng)一管理。
2. 通過cmake + make生成可執(zhí)行文件
典型的c++項目的編譯方式應(yīng)該是使用cmake + make的方式疫萤。需要編寫一個 CMakeLists.txt 文件:
cmake_minimum_required(VERSION 2.8)
project(DisplayImage)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(DisplayImage DisplayImage.cpp)
target_link_libraries(DisplayImage ${OpenCV_LIBS})
SET( CMAKE_CXX_FLAGS "-std=c++11")
保存后執(zhí)行cmake生成MakeFIle文件,然后執(zhí)行make得到可執(zhí)行文件:
yan@yanubuntu:~/Desktop/c_plus_opencv_sample$ cmake .
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: /usr/local (found version "4.1.2")
-- Configuring done
-- Generating done
-- Build files have been written to: /home/yan/Desktop/c_plus_opencv_sample
yan@yanubuntu:~/Desktop/c_plus_opencv_sample$ make
Scanning dependencies of target DisplayImage
[ 50%] Building CXX object CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o
[100%] Linking CXX executable DisplayImage
[100%] Built target DisplayImage
yan@yanubuntu:~/Desktop/c_plus_opencv_sample$
5.3 使用IDE
之前在Windows系統(tǒng)中用過VS颂跨,在ubuntu系統(tǒng)中還沒有使用過集成開發(fā)環(huán)境(Integrated Development Environment,IDE)扯饶,不過應(yīng)該和VS差不多恒削,這應(yīng)該是最簡單的編譯方式了池颈,點擊 run
按鈕,所有的編譯鏈接執(zhí)行過程自動完成蔓同。
目前對在linux系統(tǒng)的c++開發(fā)基本不懂饶辙,以上內(nèi)容純屬搬磚。僅供參考斑粱。