05-11 20:49
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[OpenCV] WebCam Frame Capture 본문

Programming/OpenCV

[OpenCV] WebCam Frame Capture

cinema4dr12 2015. 7. 23. 00:37

이번 글에서는 OpenCV를 통해 WebCam의 Frame을 Capture하는 방법에 대하여 알아보도록 하겠습니다.

C 라이브러리를 기반으로 하는 버전 2.0 이전의 방식과 C++ 라이브러리를 기반으로 하는 버전 2.0 이상의 방식을 비교해 보도록 하겠습니다.

C 기반

우선 C 라이브러리를 기반으로 하는 예전 방식은 다음과 같습니다.

Example Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <opencv2/opencv.hpp>
 
#include <stdlib.h>
#include <stdio.h>
 
using namespace std;
using namespace cv;
 
/* @ function main */
int main( int argc, char** argv )
{
  IplImage* image = 0;
  
  // find a webcam
  CvCapture* capture = cvCaptureFromCAM( 0 );
 
  // create window
  cvNamedWindow( "WebCam Frame Capture"0 );
 
  // resize windows to 640 * 480
  cvResizeWindow( "WebCam Frame Capture"640480 );
 
  whiletrue ) {
    cvGrabFrame( capture ); 
 
    // capture a frame from webcam
    image = cvRetrieveFrame( capture );
 
    // draw the captured frame onto the window created
    cvShowImage( "WebCam Frame Capture", image );
   
    if( cvWaitKey( 10 ) >= 0 )
      break;
  }
 
  // release capture
  cvReleaseCapture( &capture );
 
  // destory window created
  cvDestroyWindow( "WebCam Frame Capture" );
 
  return 0;
}
cs

C++ 기반

이제 C++ 라이브러리 방식으로 WebCam Frame Capture를 해보겠습니다. OpenCV 버전 2.0부터 C++ 라이브러리 방식으로 바뀌었습니다. VideoCapture 클래스에 관련 기능이 구현되어 있으며, 이 클래스에 대한 Reference는 다음 링크를 참고합니다: http://docs.opencv.org/master/d8/dfe/classcv_1_1VideoCapture.html


Example Code는 다음과 같습니다.

Example Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <opencv2/opencv.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
  
using namespace std;
using namespace cv;
 
 
/* @ function main */
int main( int argc, char** argv )
{
    // open the default camera
    cv::VideoCapture cap( 0 );
  
    // check if we succeeded
    if!cap.isOpened() )
        return -1;
  
    cv::namedWindow( "WebCam Frame Capture"1 );
  
    for( ; ; ) {
        cv::Mat frame;
         
        // get a new frame from camera
        cap >> frame;
  
        cv::imshow( "WebCam Frame Capture", frame );
  
        if( waitKey( 10 ) >= 0break;
    }
  
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}
cs

Python 기반

Python 기반으로 웹캠 프레임 캡춰 코드는 매우 간단합니다. 다음 코드를 참고하시기 바랍니다. (코드는 매우 Self-explanatory하므로 별도의 설명은 하지 않겠습니다^^)


Example Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import cv2
 
def show_webcam(mirror=False):
    cam = cv2.VideoCapture(0)
    while True:
        ret_val, img = cam.read()
        if mirror: 
            img = cv2.flip(img, 1)
        cv2.imshow('my webcam', img)
        
        if cv2.waitKey(1== 27
            break  # esc to quit
    cv2.destroyAllWindows()
 
def main():
    show_webcam(mirror=True)
 
if __name__ == '__main__':
    main()
cs



Python 환경에 OpenCV를 설치하려면 여기를 참고하시기 바랍니다.

'Programming > OpenCV' 카테고리의 다른 글

[OpenCV] Harris Corner Detector  (0) 2015.07.25
[OpenCV] Laplace Operator  (0) 2015.07.25
[OpenCV] Sobel Edge Detector  (0) 2015.07.21
[OpenCV] RGB Color를 Grayscale로 변환하기  (3) 2015.07.21
[OpenCV] Gaussian Blur  (4) 2015.07.18
Comments