2012年12月27日 星期四

Visual C++ 2010使用OpenCV的環境設定

在Windows下面使用OpenCV開發程式, 起初的環境設定有一些麻煩, 一開始是使用Code::Blocks.雖然上網搜尋設定後可以成功編譯,但是執行的時候dll檔總是缺東缺西,況且目前的教學幾乎都是使用Visual C++當作Windows開發環境.所以最後還是選擇了使用Visual Studio 2010,以下筆記唯一些環境的設定.

Step1. 下載OpenCV官方網站的安裝檔(目前版本2.4.3)後解壓至目錄(我使用預設目錄C:\opencv)

Step2. 設定環境變數:進入控制台設定環境變數(指定動態連結檔的位置C:\opencv\build\x86\vc10\bin)然後重新開機使生效
PS:記得多路徑要加入分號區隔";"


在專案的屬性管理員中設定OpenCV的include和Lib
Step3. 先在VC中新增一個命令列的空專案,屬性為C++

Step4. 進入該專案的屬性管理員針對Debug和Release做環境設定(以下兩種設定方式幾乎一樣)


4.1 先設定include的位置
左邊欄位選擇C/C++,並且在右邊欄位的"其他Include目錄"選項增加以下目錄:
C:\opencv\build\include
C:\opencv\build\include\opencv
C:\opencv\build\include\opencv2

4.2 隨即進入左欄的"連結器"的"一般"選項,並且設定該右欄的"其他程式庫目錄",然後指定目錄為:C:\opencv\build\x86\vc10\lib

4.3 進入連結器子選項的輸入"選項",並且在"其他相依性"選項中增加:

opencv_calib3d243d.lib
opencv_contrib243d.lib
opencv_core243d.lib
opencv_features2d243d.lib
opencv_flann243d.lib
opencv_gpu243d.lib
opencv_haartraining_engined.lib
opencv_highgui243d.lib
opencv_imgproc243d.lib
opencv_legacy243d.lib
opencv_ml243d.lib
opencv_nonfree243d.lib
opencv_objdetect243d.lib
opencv_photo243d.lib
opencv_stitching243d.lib
opencv_ts243d.lib
opencv_video243d.lib
opencv_videostab243d.lib

PS: 這些連結庫檔案都在C:\opencv\build\x86\vc10\lib的資料夾裡面,Release屬性在這部分的設定就是沒有檔名尾巴的"d"

4.4 接下來換設定Release的屬性工作表

PS2:因為這兩個屬性設定檔在每個OpenCV相關的專案都需要使用到,因此我提供已經寫好的屬性工作表,不用新增,只需要導入即可

For Debug
For Release
PS3:工作表是所有OpenCV相關專案共用一份實體檔案,如果想分開的話請將檔案複製到個別專案資料夾,並且重新尋找導入


Step5. 以上環境設定完成即可開始寫程式

在此提供簡單的讀取圖片程式碼:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file

    if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );                   // Show our image inside it.

    waitKey(0); // Wait for a keystroke in the window
    return 0;
}

沒有留言:

張貼留言