1. 程式人生 > >海思移植opencv+車輛檢測

海思移植opencv+車輛檢測

1.確保ubuntu能上網
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:
程式碼: 全選
[email protected]:~/zhouhua/opencv/opencv-2.4.9$ ls
3rdparty        LICENSE    apps   data  include    modules    samples

CMakeLists.txt  README.md  cmake  doc   index.rst  platforms
[email protected]:~/zhouhua/opencv/opencv-2.4.9$ cd ..    
[email protected]:~/zhouhua/opencv$ ls
build  opencv-2.4.9  opencv-2.4.9.zip  output
[email protected]:~/zhouhua/opencv$ mkdir build
[email protected]:~/zhouhua/opencv$ mkdir output


6.執行cmake-gui
程式碼: 全選
[email protected]:~/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
程式碼: 全選
[email protected]:~/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
程式碼: 全選
[email protected]:~/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");
}


為了方便,直接將庫和標頭檔案拷貝到編譯器的目錄下去
程式碼: 全選
[email protected]:~/zhouhua/opencv/mytest$ sudo cp ../output/lib/* /opt/hisi-linux-nptl/arm-hisiv100-linux/arm-hisiv100-linux-uclibcgnueabi/lib/
[email protected]:~/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!