ubuntu 16.06 安裝 OpenCV3.4.1 contrib 編譯 C++
範例中使用的系統是 ubuntu ,使用其他系統在自己置換一下 apt-get install 即可
懶人包 script 在這裡:opencv341_installer for Ubuntu 16.04
事前準備
# 更新系統
sudo apt-get update
sudo apt-get dist-upgrade -y
# 安裝必要軟件
sudo apt-get install build-essentia
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
安裝 OpenCV
檔案從 OpenCV 的官方git下載,下載指令如下
mkdir opencv341
cd opencv341
git clone -b 3.4.1 https://github.com/opencv/opencv
git clone -b 3.4.1 https://github.com/opencv/opencv_contrib
再來就會自己下載好了然後我們建立一個資料夾準備編譯
mkdir build
cd build
cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ../opencv
# 開始編譯
make -j8
這邊會跑一小段時間跑完之後就編譯好了,還需要安裝到系統上
cmake 的時候要從網路下載東西,那個會卡很久正常的,能跑好的就等一下吧
# 複製編譯後的檔案套系統目錄內
sudo make install
# 新增執行檔所需要的 lib 庫路徑到環境變數上
sudo ldconfig
測試
編譯的時候使用 pkg-config 幫我們設置路徑,我們先測試一下是否正確安裝OpenCV了
# 察看OpenCV版本
pkg-config -- modversion
# 察看路徑與函式庫
pkg-config opencv --libs --cflags
這邊都ok有看到東西就安裝好了,你應該會看到
3.4.1
-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_cudabgsegm -lopencv_superres -lopencv_stitching -lopencv_cudafeatures2d -lopencv_cudastereo -lopencv_videostab -lopencv_cudaoptflow -lopencv_cudacodec -lopencv_cudaobjdetect -lopencv_cudalegacy -lopencv_cudawarping -lopencv_saliency -lopencv_fuzzy -lopencv_xobjdetect -lopencv_optflow -lopencv_ximgproc -lopencv_hfs -lopencv_bgsegm -lopencv_xphoto -lopencv_xfeatures2d -lopencv_shape -lopencv_bioinspired -lopencv_surface_matching -lopencv_face -lopencv_photo -lopencv_cudaimgproc -lopencv_cudafilters -lopencv_cudaarithm -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_video -lopencv_ml -lopencv_aruco -lopencv_freetype -lopencv_line_descriptor -lopencv_img_hash -lopencv_dnn_objdetect -lopencv_dnn -lopencv_ccalib -lopencv_reg -lopencv_stereo -lopencv_plot -lopencv_rgbd -lopencv_dpm -lopencv_objdetect -lopencv_structured_light -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_phase_unwrapping -lopencv_imgproc -lopencv_flann -lopencv_core -lopencv_cudev
再來讓我們編譯CPP檔案吧,下面是範例代碼,把他儲存為 opencv.cpp
/**********************************************************
Name :
Date : 2016/05/29
By : CharlotteHonG
Final: 2018/05/23
**********************************************************/
#include <iostream>
using namespace std;
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char const *argv[]) {
/* 畫布 */
Mat img(270, 720, CV_8UC3, Scalar(56, 50, 38));
/* 直線 */
line(img, Point(20, 40), Point(120, 140), Scalar(255, 0, 0), 3);
/* 實心方塊 */
rectangle(img, Point(150, 40), Point(250, 140), Scalar(0, 0, 255), -1);
/* 實心圓 */
circle(img, Point(330, 90), 50, Scalar(0, 255, 0), -1);
/* 空心橢圓 */
ellipse(img, Point(460, 90), Size(60, 40), 45, 0, 360, Scalar(255, 255, 0), 2);
/* 不規則圖形 */
Point points[1][5];
int x = 40, y = 540;
points[0][0] = Point(0 + y, 50 + x);
points[0][1] = Point(40 + y, 0 + x);
points[0][2] = Point(110 + y, 35 + x);
points[0][3] = Point(74 + y, 76 + x);
points[0][4] = Point(28 + y, 96 + x);
const Point* ppt[1] = { points[0] };
int npt[] = { 5 };
polylines(img, ppt, npt, 1, 1, Scalar(0, 255, 255), 3);
/* 繪出文字 */
putText(img, "Test Passed !!", Point(10, 230), 0, 3, Scalar(255, 170, 130), 3);
/* 開啟畫布 */
imshow("OpenCV Test By:Charlotte.HonG", img);
waitKey(0);
return 0;
}
然後編譯
# 長得很奇怪這沒有打錯,是蚯蚓上的那一撇
g++ opencv.cpp -o opencv `pkg-config opencv --cflags --libs`
執行確認結果
./opencv
執行出問題 未定義參考到「cv::String::deallocate()」 opencv 可能是剛剛你的設置函式庫路徑沒執行,再重新執行一次 sudo ldconfig
最後是 makefile 檔不過我也正在學習,還不知道通常正規是怎麼寫的,就是一個能跑的而已~
CXX = g++
CXXFLAG :=
CXXFLAG += -O3
CXXFLAG += -std=c++11
CXXFLAG += -Wall
CXXLIBS :=
CXXLIBS += `pkg-config opencv --libs`
CXXINCS :=
CXXINCS += `pkg-config opencv --cflags`
CPPFILES :=
CPPFILES := opencv.cpp
EXEFILE := main
all: opencv
opencv: opencv.cpp
g++ $(CXXFLAG) $(CPPFILES) -o $(EXEFILE) $(CXXINCS) $(CXXLIBS)
run: opencv
./$(EXEFILE)
沒有留言:
張貼留言