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;
}

2012年12月26日 星期三

OpenCV 2 Computer Vision Application Programming Cookbook


Source Codes
Amazon

Learning OpenCV: Computer Vision with the OpenCV Library


電子書(eBook)
Amazon


  • Paperback: 555 pages
  • Publisher: O'Reilly Media; 1st edition (October 1, 2008)
  • Language: English
  • ISBN-10: 0596516134
  • 另一本完整的OpenCV教學

Mastering OpenCV with Practical Computer Vision


書中範例程式碼

BOOK

在Codeblocks下導入OpenCV 2.4.x


教學
A.安裝Code::Blocks附帶MinGW版本
http://www.codeblocks.org/downloads/26
B.下載openCV將之解壓縮在C:\ (不能解壓縮在使用者資料夾)

C.修改環境變數,增加PATH=C:\opencv\build\x86\mingw\bin

D.下載MinGW(gcc 4.6),並且安裝(安裝時選擇預設,不要選擇下載最新程式碼)

D.1.將安裝後的C:\MinGW\bin\libstdc++-6.dll複製到CodeBlocks安裝資料夾內的MinGW\bin\

E.設定CodeBlocks編譯器參數
1. Add the OpenCV header files directory
Open Settings → Compiler and debugger... → Search directories tab → Compiler tab
CodeBlock settings: search directory
Click add button for adding a new entry. In the popup dialog, type c:\opencv\build\include,
Add OpenCV include directory to CodeBlocks
and click Ok.

1-2. Add the OpenCV library files directory
Open Settings → Compiler and debugger... → Search directories tab → Linker tab

Click add button for adding a new entry. In the popup dialog, type C:\opencv\build\x86\mingw\lib,

2. Add the OpenCV libraries needed for linking
Open Settings → Compiler and debugger... → Linker settings tab.
CodeBlocks linker settings
Click add for adding new entries and open a popup dialog.
CodeBlock settings: Add libraries to link
Click the "..." button to open the File Open Dialog. Go to c:\opencv\build\x86\mingw\libdirectory and select all files by pressing Ctrl-A.
Select OpenCV libraries
Click Open to add the files,
OpenCV libraries added to CodeBlocks
Click Ok to save all settings.