欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

opencv移植(二)

程序员文章站 2022-05-03 17:53:05
接下来就是下载opencv了: 下载地址:http://blog.csdn.net/yanzi1225627/article/details/47668021 下载好之后解压:unzip opencv-2.4.11.zip cd opencv-2.4.11.zip 建两个文件夹来存放配置和安装路径: ......
原文:https://blog.csdn.net/guet_kite/article/details/78667175?utm_source=copy 
版权声明:本文为博主原创文章,转载请附上博文链接!

接下来就是下载opencv了: 
下载地址: 

下载好之后解压: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(0))) {
                 cout<<"can not open camera.\n"<<endl;
                 return -1;
         }
         cvnamedwindow("video", 1);
         while(frame = cvqueryframe( capture )){
                  cvshowimage("video",frame);
                  cout<<"have capture\n"<<endl;
         }

         cout<<"no have capture\n"<<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移植(二)

所以提示自然找不到,所以要改成我们现在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......\n" << 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 $@ $^
11 
12 clean:
13     rm $(out)

 

 这是girl.jpg图: 

opencv移植(二)

这是fly.jpg图:

opencv移植(二)

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

opencv移植(二)