海思移植opencv+车辆检测
2.安装cmake
代码: 全选
sudo apt-get install cmake-gui
3.下载opencv2.4.9 Linux版源码,不要用最新的3.0.0
http://opencv.org/downloads.html
4.解压opencv源码
代码: 全选
unzip opencv-2.4.9.zip
5.创建一个build目录用于编译和一个output目录用于存放编译完成后的海思平台的opencv:
代码: 全选
xlab@xlab-dev:~/zhouhua/opencv/opencv-2.4.9$ ls
3rdparty LICENSE apps data include modules samples
CMakeLists.txt README.md cmake doc index.rst platforms
xlab@xlab-dev:~/zhouhua/opencv/opencv-2.4.9$ cd ..
xlab@xlab-dev:~/zhouhua/opencv$ ls
build opencv-2.4.9 opencv-2.4.9.zip output
xlab@xlab-dev:~/zhouhua/opencv$ mkdir build
xlab@xlab-dev:~/zhouhua/opencv$ mkdir output
6.执行cmake-gui
代码: 全选
xlab@xlab-dev:~/zhouhua/opencv/opencv-2.4.9$ cmake-gui
点击Browse Source选择~/zhouhua/opencv/opencv-2.4.9
点击Browse Build选择~/zhouhua/opencv/build
然后点击Configure
此时出现的对话框选择最后一项:Specify options for cross-compiling
下一步
Operating System填写arm-hisiv100nptl-linux
C填写arm-hisiv100nptl-linux-gcc
C++填写arm-hisiv100nptl-linux-g++
下一步,然后等待Configuration done
然后在出现的列表中修改CMAKE_INSTALL_PREFIX为~/zhouhua/opencv/output
然后点击Generate
等待Generation done
即可关闭cmake软件。
7.进入build目录执行make
代码: 全选
xlab@xlab-dev:~/zhouhua/opencv/build$ make
提示出错:
代码: 全选
../../lib/libopencv_core.so: undefined reference to `pthread_once'
../../lib/libopencv_core.so: undefined reference to `pthread_spin_lock'
../../lib/libopencv_core.so: undefined reference to `pthread_spin_unlock'
../../lib/libopencv_core.so: undefined reference to `pthread_spin_init'
../../lib/libopencv_core.so: undefined reference to `pthread_spin_trylock'
../../lib/libopencv_core.so: undefined reference to `pthread_spin_destroy'
修改CMakeCache.txt大约200行处
//Flags used by the linker.
CMAKE_EXE_LINKER_FLAGS:STRING= -lpthread -lrt
继续make
可能出现如下错误
代码: 全选
CMake Error at /home/xlab/zhouhua/opencv/opencv-2.4.9/cmake/cl2cpp.cmake:50 (string):
string does not recognize sub-command MD5
make[2]: *** [modules/ocl/opencl_kernels.cpp] Error 1
make[1]: *** [modules/ocl/CMakeFiles/opencv_ocl.dir/all] Error 2
make: *** [all] Error 2
删除/home/xlab/zhouhua/opencv/opencv-2.4.9/cmake/cl2cpp.cmake的第50行的内容即可。
继续make
完成后执行make install
代码: 全选
xlab@xlab-dev:~/zhouhua/opencv/output$ ls
LICENSE bin include lib share
8.得到了include和lib目录就可以编写程序了,来试试最常用的车辆检测吧
编写如下代码
代码: 全选
- #include"cv.h"
- #include"highgui.h"
- #include"stdio.h"
- /******************fortime mesurement*************************/
- #include<sys/time.h>
- structtimeval tpstart,tpend;
- unsigned longtimeuses;
- voidtimeRec()
- {
- gettimeofday(&tpstart,0);
- }
- int timeRep()
- {
- gettimeofday(&tpend,0);
- timeuses=(tpend.tv_sec-tpstart.tv_sec)*1000000+tpend.tv_usec-tpstart.tv_usec;
- printf("use time:%uus\n",timeuses);
- return timeuses;
- }
- /********************end**************************************/
- int main(intargc, char* argv[])
- {
- IplImage* img= NULL;
- CvMemStorage* storage =cvCreateMemStorage(0);
- CvHaarClassifierCascade*cascade = cvLoadHaarClassifierCascade("./model.xml",cvSize(24,24));
- //CvHaarClassifierCascade* cascade =(CvHaarClassifierCascade*)cvLoad("./lbpcascade_frontalface.xml", 0,0, 0);
- CvSeq* faces;
- //加载图像
- img = cvLoadImage(argv[1], 0);
- printf("img w=%d h=%d\n",img->width, img->height);
- //检测并计时
- timeRec();
- faces = cvHaarDetectObjects(img,cascade, storage, 1.1, 3, 0,cvSize(24,24) );
- timeRep();
- if (faces->total == 0){
- printf("no face!\n");
- }
- printf("car= %d\n", faces->total);
- //释放内存
- cvReleaseImage(&img);
- printf("car detected! car.jpg!\n");
- }
为了方便,直接将库和头文件拷贝到编译器的目录下去
代码: 全选
xlab@xlab-dev:~/zhouhua/opencv/mytest$ sudo cp ../output/lib/* /opt/hisi-linux-nptl/arm-hisiv100-linux/arm-hisiv100-linux-uclibcgnueabi/lib/
xlab@xlab-dev:~/zhouhua/opencv/mytest$sudo cp ../output/include/* /opt/hisi-linux-nptl/arm-hisiv100-linux/arm-hisiv100-linux-uclibcgnueabi/include/ -r
然后编译:(由于版本比较高,用了opencv2的头文件,因此需要额外增加一个-I参数指定头文件目录)
代码: 全选
arm-hisiv100nptl-linux-g++ face.cpp -I/home/xlab/zhouhua/opencv/output/include/opencv -lopencv_highgui -lopencv_core -lopencv_imgproc -lpthread -lrt -lopencv_objdetect -o face
会提示一些warning,不用管。
编译成功,然后拷贝车辆分类器文件过来。
再找个图片过来,我这里就用car.jpg了。
将/root/jiang/OpenCV/output/lib下的libopencv_imgproc.so、libopencv_objdetect.so、libopencv_highgui.so和libopencv_core.so复制到u盘,将u盘中这4个动态库做软连接库到/lib目录下。
代码: 全选
ln -s /mnt/udisk/libopencv_imgproc.so /lib/libopencv_imgproc.so
ln -s /mnt/udisk/libopencv_objdetect.so /lib/libopencv_objdetect.so
ln -s /mnt/udisk/libopencv_highgui.so /lib/libopencv_highgui.so
ln -s /mnt/udisk/libopencv_core.so /lib/libopencv_core.so
备注:
删除软链接:
rm -rf /lib/libopencv_core.so 注意不是rm -rf /lib/libopencv_core.so/
然后到car所在的/mnt/udisk目录去执行即可:
# ./car car.jpg
img w=686h=398
use time:18323188us
car = 5
cardetected! in car.jpg!
文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/56016268
- 点赞
- 收藏
- 关注作者
评论(0)