opencv移植
Posted xlh-xhy-hzc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
在where is the source code:里写上opencv的源文件路径
在where to build the binaries:里面写上作为cmake编译arm版本的工作目录
然后点击configure
这里generator保持为Unix Makefiles,然后选择第四个选项:Specify options for cross-compiling
最后点击Next
这里Targeting System选的是Linux(之前我选的是arm-linux,有点坑,选这个没有V4L支持),
接着就是选择工具链了,我用的是友善的4.4.3的工具链,Compliers写的是交叉编译工具 arm-linux-gcc、arm-linux-g++
Target Root写的是交叉编译工具 arm-linux-g++/gcc 的所在包含文件夹
最后点击 Finish即可
然后中间红色的配置里:
取消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等…….
最后点击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(0))) { cout<<"Can not open camera. "<<endl; return -1; } cvNamedWindow("video", 1); while(frame = cvQueryFrame( capture )){ cvShowImage("video",frame); cout<<"have capture "<<endl; } cout<<"no have capture "<<endl; cvDestroyWindow("video"); cvReleaseCapture(&capture); return 0; }
编译:
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的库:
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.3/arm-none-linux-gnueabi/lib
然后把编译出来的test文件放到开发板上执行。
测试二:
1 #include<opencv2/core/core.hpp> 2 #include<opencv2/highgui/highgui.hpp> 3 #include<iostream> 4 5 using namespace cv; 6 using namespace std; 7 8 int main( ) 9 { 10 //载入图片 11 Mat image= imread("girl.jpg"); 12 Mat logo= imread("fly.jpg"); 13 //定义一个Mat类型,用于存放,图像的ROI 14 Mat imageROI; 15 16 imageROI= image(Rect(10,10,logo.cols,logo.rows));//定义一个左上角点坐标为(_x, _y)的cols*rows矩形窗口 17 18 //将logo加到原图 19 //参数:图、权重、图、权重、添加的常数项、输出图 20 addWeighted(imageROI,0.5,logo,0.3,0.,imageROI); 21 22 cout << "start add picture...... " << endl; 23 //输出一张jpg图片到工程目录下 24 imwrite("fly_girl.jpg",image); 25 26 waitKey(); 27 28 return 0; 29 }
Makefile:
1 CC = arm-linux-g++ 2 LFLAGS = -Wno-psabi 3 LIBS = -L/usr/local/opencv/install_opencv/lib/ -L/work/qt/my_qt/lib 4 CPPFLAGS = -lQt5Widgets -lQt5Gui -lQt5Core -lpthread -lopencv_core -lopencv_highgui -lpthread -lrt 5 LINC += -I/usr/local/opencv/install_opencv/include/opencv/ 6 objs := open 7 out := open 8 9 $(out):$(objs).cpp 10 ${CC} ${LFLAGS} ${LIBS} ${LINC} ${CPPFLAGS} -o [email protected] $^ 11 12 clean: 13 rm $(out)
这是girl.jpg图:
这是fly.jpg图:
这是使用opencv合成的效果图:
以上是关于opencv移植的主要内容,如果未能解决你的问题,请参考以下文章
opencv4图像分割那些你不知道的API 代码可直接移植使用