2013年12月26日 星期四

OpenCV: Displaying the video feed from camera device

since: 2013/12/26
update: 2013/12/26

reference:
1. Amazon.com: Practical OpenCV eBook
2. Welcome to opencv documentation!
3. I touchs: Using OpenCV on Mac OS X

A. 前置作業:
     1. 先依照 I touchs: Using OpenCV on Mac OS X 的說明, 建置好開發環境.

     2. 需要加入到專案的 OpenCV 函式庫爲: (Add Files to Project...)
          libopencv_core.dylib
          libopencv_highgui.dylib
          libopencv_imgproc.dylib

     3. 將 main.cpp 的 main function 更名.(不作為程式執行的進入點)
          //int main(int argc, const char * argv[])  
          int main_main(int argc, const char * argv[])

     4. 爲專案新增 C++ 檔案:      
         點選專案 > New File... > OS X > C and C++ > C++ Class > Next >
         Save as: displayVideo.cpp > Create

     5. 使用設備:  
          Logitech HD Webcam C310
          參考: 支援 Mac OS 10.4.9 與更新版本的 UVC 網路攝影機

----------------------------------------------------------------------------------------
  
B. 撰寫程式:
    1. 開啓 displayVideo.h 檔案, 修改如下:
#ifndef __HelloOpenCV__displayVideo__
#define __HelloOpenCV__displayVideo__

#include <iostream>

//@add
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>


#endif /* defined(__HelloOpenCV__displayVideo__) */

---------------------------------------------------------------------------------

    2. 開啓 displayVideo.cpp 檔案, 修改如下:
#include "displayVideo.h"

//@add
using namespace cv;
using namespace std;


#define CAMERA_OUTPUT_WINDOW_NAME "camera-output"

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

    // C structure: for video capturing from video files or cameras.
    CvCapture *camCapture;
    int ret = 0;
   
    if (!(camCapture = cvCaptureFromCAM(CV_CAP_ANY))) {
        cout << "Failed to capture from camera" << endl;
       
        ret = 1;
       
        goto exitCameraOpenFailed;
    }
   
    cout << "Camera opened successfully" << endl;

   
    // C function, Creates a window.
    cvNamedWindow(CAMERA_OUTPUT_WINDOW_NAME, CV_WINDOW_AUTOSIZE);
   
    // C/C++ struct: The IplImage is taken from the Intel Image Processing Library, in which the format is native.
    IplImage *cameraFrame;
   
    while (true) {

        // cvQueryFrame(C function): It combine VideoCapture::grab() and VideoCapture::retrieve() in one call.
        if ((cameraFrame = cvQueryFrame(camCapture))) {
            cvShowImage(CAMERA_OUTPUT_WINDOW_NAME, cameraFrame);
        }
       
        if (cvWaitKey(60) != -1) {
            cout << "Input" << endl;
            break;
        }
    }
   
    cout << "Done" << endl;
   
    cvReleaseCapture(&camCapture);

    // C function, destroys the window with the given name.
    cvDestroyWindow(CAMERA_OUTPUT_WINDOW_NAME);
   
    exitCameraOpenFailed:
    return ret;
}


----------------------------------------------------------------------------------------

C. 執行結果:

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。