opencv移植(二)

原文:https://blog.csdn.net/Guet_Kite/article/details/78667175?utm_source=copy 
版权声明:本文为博主原创文章,转载请附上博文链接!

接下来就是下载opencv了: 
下载地址:http://blog.csdn.net/yanzi1225627/article/details/47668021

下载好之后解压:unzip opencv-2.4.11.zip
cd opencv-2.4.11.zip
建两个文件夹来存放配置和安装路径:
sudo mkdir /usr/local/opencv/install_opencv -d
sudo mkdir /usr/local/opencv/config_opencv -d

然后用cmake配置:
sudo cmake-gui
opencv移植(二)

在where is the source code:里写上opencv的源文件路径 
在where to build the binaries:里面写上作为cmake编译arm版本的工作目录 
然后点击configure

opencv移植(二)

这里generator保持为Unix Makefiles,然后选择第四个选项:Specify options for cross-compiling 
最后点击Next

opencv移植(二)

这里Targeting System选的是Linux(之前我选的是arm-linux,有点坑,选这个没有V4L支持),
接着就是选择工具链了,我用的是友善的4.4.3的工具链,Compliers写的是交叉编译工具 arm-linux-gcc、arm-linux-g++
Target Root写的是交叉编译工具 arm-linux-g++/gcc 的所在包含文件夹
最后点击 Finish即可
opencv移植(二)

然后中间红色的配置里:
取消WITH_GTK
取消WITH_TIFF
选择WITH_QT
GTK配置比较麻烦,我Ubuntu之前就配置好有QT了,所以我们取消掉,直接用QT最GUI。如果没有配置有QT,那还是不选吧。
最后点击Configure,会发现有错误:Error in configuration process,project files may be invalid
CMake Warning at cmake/OpenCVFindLibsGUI.cmake:18 (find_package):
By not providing “FindQt5Core.cmake” in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by “Qt5Core”, but
CMake did not find one.

Could not find a package configuration file provided by “Qt5Core” with any
of the following names:
Qt5CoreConfig.cmake
qt5core-config.cmake
第一个是一个warning,不管,下一个是error,说的是”Qt5Core”没找到。
我的QT安装路径是:/work/qt/my_qt/
所以直接把/work/qt/my_qt/lib/cmake/Qt5Core 填到Qt5Core_DIR选项即可,
同理,底下几个选项也要填上,Qt5Gui_DIR、Qt5Test_DIR等…….
opencv移植(二)

最后点击Configure无误后,点击Generate按钮就会在/usr/local/opencv/install_opencv目录生成Makefile

cd /usr/local/opencv/config_opencv
修改安装opencv的路径:
sudo vi CMakeCache.txt
找到:CMAKE_INSTALL_PREFIX:PATH=/usr/local
修改为:CMAKE_INSTALL_PREFIX:PATH=/usr/local/opencv/install_opencv
这是我自己的安装路径。

接着sudo make编译
最后把/usr/local/opencv/install_opencv/lib下文件移植到开发板根文件lib下

好了,我们测试下: 
从网上找到一个测试例子,然后修改下,test.cpp:

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <iostream> using namespace std; int main(int argc, char **argv)
{
CvCapture* capture = NULL;
IplImage* frame = NULL;
if(!(capture = cvCaptureFromCAM())) {
cout<<"Can not open camera.\n"<<endl;
return -;
}
cvNamedWindow("video", );
while(frame = cvQueryFrame( capture )){
cvShowImage("video",frame);
cout<<"have capture\n"<<endl;
} cout<<"no have capture\n"<<endl;
cvDestroyWindow("video");
cvReleaseCapture(&capture);
return ;
}

编译:

arm-linux-g++ -I/usr/local/opencv/install_opencv/include/opencv/ -L/usr/local/opencv/install_opencv/lib/ -lcv -lcxcore -lhighgui -lpthread -lrt -o test test.cpp

出现: 
test.cpp:21: note: the mangling of ‘va_list’ has changed in GCC 4.4 
在GCC 4.4里,’va_list’已经被改变了,所以我们编译时,要加上 -Wno-psabi选项:

arm-linux-g++ -Wno-psabi -I/usr/local/opencv/install_opencv/include/opencv/ -L/usr/local/opencv/install_opencv/lib/ -lcv -lcxcore -lhighgui -lpthread -lrt -o test test.cpp

那么,这样可以了吗?
不行!出现:
/usr/local/arm/opt/FriendlyARM/toolschain/4.4.3/bin/../lib/gcc/arm-none-linux-gnueabi/4.4.3/../../../../arm-none-linux-gnueabi/bin/ld: cannot find -lcv
collect2: ld returned 1 exit status
这里提示lcv找不到,为什么呢?
还是我之前说的,网上的文章都是几年前的了,好老了。
打开我们opencv的lib可以看下,根本没有这些个库了,
opencv移植(二)

所以提示自然找不到,所以要改成我们现在opencv的库:

arm-linux-g++ -Wno-psabi -I/usr/local/opencv/install_opencv/include/opencv/ -L/usr/local/opencv/install_opencv/lib/ -lopencv_core -lopencv_highgui -lpthread -lrt -o test test.cpp

出现两个warning:
/usr/local/arm/opt/FriendlyARM/toolschain/4.4.3/bin/../lib/gcc/arm-none-linux-gnueabi/4.4.3/../../../../arm-none-linux-gnueabi/bin/ld: warning: ../../lib/libopencv_imgproc.so, needed by /usr/local/opencv/lib_opencv/lib//libopencv_highgui.so, not found (try using -rpath or -rpath-link)
/usr/local/arm/opt/FriendlyARM/toolschain/4.4.3/bin/../lib/gcc/arm-none-linux-gnueabi/4.4.3/../../../../arm-none-linux-gnueabi/bin/ld: warning: ../../lib/libopencv_core.so, needed by /usr/local/opencv/lib_opencv/lib//libopencv_highgui.so, not found (try using -rpath or -rpath-link)

将需要到的那三个.so文件:libopencv_core.so libopencv_highgui.so libopencv_imgproc.so将这些库复制到编译器的lib中:

sudo cp -d *libopencv_core* *libopencv_highgui* *libopencv_imgproc*  /usr/local/arm/opt/FriendlyARM/toolschain/4.4./arm-none-linux-gnueabi/lib

以上借用风筝大神博客

本人编译使用命令

arm-cortex_a8-linux-gnueabi-g++ -Wno-psabi -I/usr/local/opencv/install_opencv/include/opencv/ -L/usr/local/opencv/install_opencv/lib/ -lopencv_core -lopencv_highgui -lpthread -lrt -o video1 video1.cpp 

然后把编译出来的test文件放到开发板上执行。

测试二:

 #include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<iostream> using namespace cv;
using namespace std; int main( )
{
//载入图片
Mat image= imread("girl.jpg");
Mat logo= imread("fly.jpg");
//定义一个Mat类型,用于存放,图像的ROI
Mat imageROI; imageROI= image(Rect(,,logo.cols,logo.rows));//定义一个左上角点坐标为(_x, _y)的cols*rows矩形窗口 //将logo加到原图
//参数:图、权重、图、权重、添加的常数项、输出图
addWeighted(imageROI,0.5,logo,0.3,.,imageROI); cout << "start add picture......\n" << endl;
//输出一张jpg图片到工程目录下
imwrite("fly_girl.jpg",image); waitKey(); return ;
}

Makefile:

 CC  = arm-linux-g++
LFLAGS = -Wno-psabi
LIBS = -L/usr/local/opencv/install_opencv/lib/ -L/work/qt/my_qt/lib
CPPFLAGS = -lQt5Widgets -lQt5Gui -lQt5Core -lpthread -lopencv_core -lopencv_highgui -lpthread -lrt
LINC += -I/usr/local/opencv/install_opencv/include/opencv/
objs := open
out := open $(out):$(objs).cpp
${CC} ${LFLAGS} ${LIBS} ${LINC} ${CPPFLAGS} -o $@ $^ clean:
rm $(out)

这是girl.jpg图:

opencv移植(二)

这是fly.jpg图:

opencv移植(二)

这是使用opencv合成的效果图:

opencv移植(二)

上一篇:夺命雷公狗ThinkPHP项目之----企业网站17之网站配置页的添加


下一篇:iOS OC和Swift进行互相调用