软硬件环境
宿主机: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
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 qt-qt-gui
$cmake-gui --version
cmake 和 cmake-gui都能正常显示版本信息后,说明安装成功。
OpenCV 的交叉编译
解压OpenCV压缩文件,得到文件夹。
$cmake-gui 打开cmake的gui界面,开始进行配置,配置过程如下图所示:
1、选定解压后的源文件和将要编译的文件目录,这个自己之前先建好。
2、点击Configure 选择工程类型,选择交叉编译,点击下一步。
3、在下图的界面中选择交叉编译工具链以及库文件相关信息,按照图中的进行填写,其中红框部分为交叉编译工具链的实际路径,根据宿主机实际安装路径填写。
4、点击Finsh完成初步配置。
我的实际路径如下:
5、此时主界面如下图所示,这时Cmake会读取程序的一些配置信息,可以再下图红框中对每一个编译选项进行修改,这里去掉WITH_TIFF支持并修改安装路径CMAKE_INSTALL_PREFIX,其他配置可以根据需求选择。
通过终端进入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
按照此方法更改对应文件,如下图所示:
此处在CMakeCache.txt 的173行,添加-lpthread -lrt,然后继续编译。编译过程十来分钟左右,编译成功。
运行make install进行安装,在我的安装目录中就会多出以下几个文件夹:
lib文件夹即生成的库文件,拷贝到开发板“对应目录”即可。
这里对应目录是指跟当前环境下一样的绝对路径,当然也需要在/lib下也拷贝一份,这样就不怕程序找不到运行库了。
Qt+opencv小程序测试
新建带界面的工程
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执行即可。