1. 程式人生 > >Ubuntu16.04交叉編譯OpenCV並移植

Ubuntu16.04交叉編譯OpenCV並移植

軟硬體環境

宿主機:Ubuntu 16.04 64bit

開發板:tq2440

OpenCV: 2.4.3

其他:

arm-linux-g++ 4.4.3 / arm-linux-gcc 4.4.3

CMake 2.8.10.2

CMake-gui 3.5.1

所需包下載:http://pan.baidu.com/s/1jHMSRlG

Cmake的安裝

OpenCV 2.2以後版本需要使用Cmake生成makefile檔案,因此需要先安裝cmake。

解壓cmake-2.8.10.2.tar.gz

切換到解壓後目錄執行

$./configure

$make -j4

$sudo make install

$cmake --version 

$sudo apt-get install cmake-qt-gui

$cmake-gui --version

cmake 和 cmake-gui都能正常顯示版本資訊後,說明安裝成功。

OpenCV 的交叉編譯

解壓OpenCV壓縮檔案,得到資料夾。

$cmake-gui 開啟cmake的gui介面,開始進行配置,配置過程如下圖所示:

1、選定解壓後的原始檔和將要編譯的檔案目錄,這個自己之前先建好。

2、點選Configure 選擇工程型別,選擇交叉編譯,點選下一步。

image

3、在下圖的介面中選擇交叉編譯工具鏈以及庫檔案相關資訊,按照圖中的進行填寫,其中紅框部分為交叉編譯工具鏈的實際路徑,根據宿主機實際安裝路徑填寫。

image

4、點選Finsh完成初步配置。

我的實際路徑如下:


5、此時主介面如下圖所示,這時Cmake會讀取程式的一些配置資訊,可以再下圖紅框中對每一個編譯選項進行修改,這裡去掉WITH_TIFF支援並修改安裝路徑CMAKE_INSTALL_PREFIX,其他配置可以根據需求選擇。

image

通過終端進入OpenCV-ARM目錄,make編譯,編譯過程中出錯如下:

複製程式碼
Linking CXX executable ../../bin/opencv_perf_core
../../lib/libopencv_highgui.so: undefined reference to `_TIFFmemcpy'
../../lib/libopencv_core.so: undefined reference to `pthread_spin_init'
../../lib/libopencv_core.so: undefined reference to `pthread_spin_unlock'
../../lib/libopencv_highgui.so
: undefined reference to `_TIFFmemset' ../../lib/libopencv_highgui.so: undefined reference to `_TIFFwarningHandler' ../../lib/libopencv_core.so: undefined reference to `pthread_key_create' ../../lib/libopencv_highgui.so: undefined reference to `_TIFFmemcmp' ../../lib/libopencv_core.so: undefined reference to `pthread_getspecific' ../../lib/libopencv_highgui.so: undefined reference to `_TIFFfree' ../../lib/libopencv_ts.so: undefined reference to `pthread_key_delete' ../../lib/libopencv_core.so: undefined reference to `pthread_spin_lock' ../../lib/libopencv_highgui.so: undefined reference to `_TIFFmalloc' ../../lib/libopencv_highgui.so: undefined reference to `TIFFOpen' ../../lib/libopencv_core.so: undefined reference to `pthread_spin_destroy' ../../lib/libopencv_core.so: undefined reference to `pthread_once' ../../lib/libopencv_highgui.so: undefined reference to `_TIFFrealloc' ../../lib/libopencv_core.so: undefined reference to `clock_gettime' ../../lib/libopencv_core.so: undefined reference to `pthread_spin_trylock' ../../lib/libopencv_highgui.so: undefined reference to `_TIFFerrorHandler' ../../lib/libopencv_core.so: undefined reference to `pthread_setspecific' collect2: ld returned 1 exit status make[2]: *** [bin/opencv_perf_core] 錯誤 1 make[1]: *** [modules/core/CMakeFiles/opencv_perf_core.dir/all] 錯誤 2 make: *** [all] 錯誤 2
複製程式碼

關於TIFF的報錯,是因為一開始我勾選了TIFF的支援,而這個支援是需要宿主機中安裝相應的程式的,所以報錯,可以開啟cmake 去掉勾選。 其他報錯經過查詢找到以下解釋:

編譯時發現如下錯誤: 
Linking CXX executable ../../bin/opencv_createsamples 
../../lib/libcxcore.so: undefined reference to `clock_gettime' 
../../lib/libcxcore.so: undefined reference to `pthread_key_create' 
../../lib/libcxcore.so: undefined reference to `pthread_getspecific' 
../../lib/libcxcore.so: undefined reference to `pthread_setspecific'

原因是cmake不認識我定義的arm-linux系統標記,沒有加上庫pthread和rt的連結選項

此時需要修改CMakeCache.txt,CMAKE_EXE_LINKER_FLAGS原來為空,加上-lpthread -lrt

按照此方法更改對應檔案,如下圖所示:

image

此處在CMakeCache.txt 的173行,新增-lpthread -lrt,然後繼續編譯。編譯過程十來分鐘左右,編譯成功

執行make install進行安裝,在我的安裝目錄中就會多出以下幾個資料夾:

 

lib資料夾即生成的庫檔案,拷貝到開發板“對應目錄”即可。

這裡對應目錄是指跟當前環境下一樣的絕對路徑,當然也需要在/lib下也拷貝一份,這樣就不怕程式找不到執行庫了。

-----------------------------------------------------------------

Qt+opencv小程式測試

新建帶介面的工程,並在窗體上新增一個lable控制元件,名為label

mainwindow.cpp程式碼如下:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv/cv.h>
#include <opencv/highgui.h>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    IplImage *frame = 0;
    frame = cvLoadImage("pic.jpg",-1);    //pic.jpg在所建工程的目錄下
    QImage image((const uchar*)frame->imageData,
                 frame->width,
                 frame->height,
                 QImage::Format_RGB888);
    image = image.rgbSwapped();
    image = image.scaled(320,240);
    ui->label->setPixmap(QPixmap::fromImage(image));
}

MainWindow::~MainWindow()
{
    delete ui;
}

.pro檔案修改如下:
#-------------------------------------------------
#
# Project created by QtCreator 2016-09-17T17:11:24
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = opencvtest2
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

#INCLUDEPATH += /usr/local/include/opencv
#LIBS += -lopencv_core \
#-lopencv_imgproc \
#-lopencv_highgui \
#-lopencv_ml \
#-lopencv_video \
#-lopencv_features2d \
#-lopencv_calib3d \
#-lopencv_objdetect \
#-lopencv_contrib \
#-lopencv_legacy \
#-lopencv_flann

INCLUDEPATH +=  /home/lhc/OpenCV/output/opencv-arm/include \
                /home/lhc/OpenCV/output/opencv-arm/include/opencv \
                /home/lhc/OpenCV/output/opencv-arm/include/opencv2

LIBS += /home/lhc/OpenCV/output/opencv-arm/lib/libopencv_highgui.so \
        /home/lhc/OpenCV/output/opencv-arm/lib/libopencv_core.so    \
        /home/lhc/OpenCV/output/opencv-arm/lib/libopencv_imgproc.so

LIBS += -lpthread -lrt

其他檔案預設不必修改。

切換到工程目錄下開啟終端,執行

$qmake-arm

qmake-arm是自己搭建的qt交叉編譯環境下的命令,根據自己環境修改

$make

根目錄下要有一張pic.jpg的圖片,否則無法正常執行

按要求移動到開發板上執行。

如果要在x86裝置上執行,須將opencv安裝到預設路徑(/usr/local)下,將例程.pro檔案中註釋部分取消註釋,註釋下邊部分註釋起來,用x86的qmake生成makefile,再make執行即可。

文章參考: