1. 程式人生 > >樹莓派入手設定+opencv+qt配置開發環境

樹莓派入手設定+opencv+qt配置開發環境

       最近2個月做了一個基於單目相機跟蹤物體位姿的橫向專案,所用到的硬體主要有Raspberry Pi 3B+,Raspberry Pi Camera V2紅外夜視相機,以及嘉肯光電定製的紅外環形光源。

初次接觸樹莓派,配置上踩過一些坑,現記錄下來:

1、裝系統。由於是工業專案,追求魯棒性,所以安裝的是樹莓派官方推薦的Debain系統,這也是linux系統之一。在自己pc上通過SDFormatter工具先格式化tf卡,然後使用Win32DiskImager工具安裝下載好的Debain映象檔案。

2、開機後配置:在config中拓展系統空間、改位置、時區、鍵盤佈局、enable camera、enable SSH、enable VNC,參考此連結

http://www.cnblogs.com/crosys/p/6220168.html,若顯示器不能全屏,在設定中over scan選項選擇disable,安裝中文字型 、中文字型檔:sudo apt-get install ttf-wqy-microhei ttf-wqy-zenhei xfonts-wqy,安裝ibus中文輸入法(只有這個才能支援在qt  creator中輸入中文)

3、下載並編譯opencv:按照此連結的步驟https://blog.csdn.net/leaves_joe/article/details/67656340,make之前注意:新增contrib選項編譯的時候可能會報錯,如果報錯,去掉此目錄就行(這個是opencv的拓展庫,一般用不到),另外把c加上。注意,編譯時間要6個小時,中間可能會出錯,不要懷疑,重複試,可以先sudo apt-get update、upgrade編譯的時候千萬不要更改下載源。編譯成功後,編譯生成的標頭檔案在/usr/local/include,生成的動態連結庫so檔案在/usr/local/lib,以後移植的時候,可以直接把這些檔案拷貝到對應目錄中,不用再麻煩的編譯了;

4、Qt安裝:sudo apt-get  install  qt5-default、qtcreator

在選單欄Tools->Options->Build&Run,進去之後,單擊Compilers,在選擇Add->GCC->Compiler Path/usr/bin/gcc. 之後單擊KitsManual->Compiler->GCC,Debugger: System GDB at /usr/bin/gdb, Qt Version: Qt 5.5.1(qt5).之後選擇Apply,再選擇OK(若程式碼從win下移植到linux,漢字註釋會亂碼,選擇GBK即可)

對raspicam進行編譯(我第一次編譯的時候失敗的原因是/tmp檔案不能寫入,解決方法:萬能的重啟)

    cd raspicam  #庫下載位置

    mkdir build

    cd buil

    cmake ..

    make

    sudo make install

    sudo ldconfig

6、新建qt 工程測試:在pro檔案中加入如下內容:

INCLUDEPATH += /usr/local/include \

                /usr/local/include/opencv \

                /usr/local/include/opencv2 \

                /usr/local/include/raspicam

LIBS += /usr/local/lib/libopencv_highgui.so \

        /usr/local/lib/libopencv_core.so    \

        /usr/local/lib/libopencv_imgproc.so \

        /usr/local/lib/libopencv_video.so \

        /usr/local/lib/libopencv_videoio.so \

        /usr/local/lib/libopencv_videostab.so \

        /usr/local/lib/libraspicam.so \

        /usr/local/lib/libraspicam_cv.so \

        -L/usr/local/lib \

        -lopencv_core \

        -lopencv_imgcodecs \

        -lopencv_highgui \

        -lopencv_video \

        -lopencv_videoio \

        -lopencv_videostab \

        -lraspicam \

        -lraspicam_cv

7、開始測試

測試程式碼1

/* 這是下載的庫檔案中opencv測試的例子*/

#include <ctime>

#include <iostream>

#include <raspicam/raspicam_cv.h>

using namespace std;

int main ( int argc,char **argv ) {

    time_t timer_begin,timer_end;

    raspicam::RaspiCam_Cv Camera;

    cv::Mat image;

    int nCount=100;

    //set camera params

    Camera.set( CV_CAP_PROP_FORMAT, CV_8UC1 );

    //Open camera

    cout<<"Opening Camera..."<<endl;

    if (!Camera.open()) {cerr<<"Error opening the camera"<<endl;return -1;}

    //Start capture

    cout<<"Capturing "<<nCount<<" frames ...."<<endl;

    time ( &timer_begin );

    for ( int i=0; i<nCount; i++ ) {

        Camera.grab();

        Camera.retrieve ( image);

        if ( i%5==0 )  cout<<"\r captured "<<i<<" images"<<std::flush;

    }

    cout<<"Stop camera..."<<endl;

    Camera.release();

    //show time statistics

    time ( &timer_end ); /* get current time; same as: timer = time(NULL)  */

    double secondsElapsed = difftime ( timer_end,timer_begin );

    cout<< secondsElapsed<<" seconds for "<< nCount<<"  frames : FPS = "<<  ( float ) ( ( float ) ( nCount ) /secondsElapsed ) <<endl;

    //save image

    cv::imwrite("raspicam_cv_image.jpg",image);

    cout<<"Image saved at raspicam_cv_image.jpg"<<endl;

}

測試程式碼2

#include "opencv2/core.hpp"

#include "opencv2/imgproc.hpp"

#include "opencv2/highgui.hpp"

#include "opencv2/videoio.hpp"

#include <iostream>

#include <raspicam/raspicam_cv.h>

using namespace cv;

using namespace std;

int main()

{

    raspicam::RaspiCam_Cv Camera;

    cout << "Built with OpenCV " << CV_VERSION << endl;

    Mat image;

    //set camera params

    Camera.set( CV_CAP_PROP_FORMAT, CV_8UC1 );

    //Open camera

    if (!Camera.open()) {cout<<"Error opening the camera"<<endl;return -1;}

    //Start capture

    else{

        while(1)

        {

            Camera.grab();

            Camera.retrieve ( image);

            if(image.empty())

                break;

            imshow("Sample", image);

            if(waitKey(10) >= 0)

                break;

            waitKey(100);

        }

    }

    return 0;

}