Update to 2.0.0 tree from current Fremantle build
[opencv] / samples / octave / capture-cam.m
1 #! /usr/bin/env octave
2
3 ## import the necessary things for OpenCV
4 addpath("/home/x/opencv2/interfaces/swig/octave");
5 source("/home/x/opencv2/interfaces/swig/octave/PKG_ADD_template");
6 debug_on_error(true);
7 debug_on_warning(true);
8 crash_dumps_octave_core (0)
9 cv;
10 highgui;
11
12 ## the codec existing in cvcapp.cpp,
13 ## need to have a better way to specify them in the future
14 ## WARNING: I have see only MPEG1VIDEO working on my computer
15 H263 = 0x33363255;
16 H263I = 0x33363249;
17 MSMPEG4V3 = 0x33564944;
18 MPEG4 = 0x58564944;
19 MSMPEG4V2 = 0x3234504D;
20 MJPEG = 0x47504A4D;
21 MPEG1VIDEO = 0x314D4950;
22 AC3 = 0x2000;
23 MP2 = 0x50;
24 FLV1 = 0x31564C46;
25
26 #############################################################################
27 ## so, here is the main part of the program
28
29 ## a small welcome
30 printf("OpenCV Octave capture video\n");
31
32 ## first, create the necessary window
33 highgui.cvNamedWindow ('Camera', highgui.CV_WINDOW_AUTOSIZE);
34
35 ## move the new window to a better place
36 #highgui.cvMoveWindow ('Camera', 10, 10);
37
38 try
39   ## try to get the device number from the command line
40   device = int32 (argv(){1});
41 catch
42   ## no device number on the command line, assume we want the 1st device
43   device = -1;
44 end_try_catch
45
46 ## no argument on the command line, try to use the camera
47 capture = highgui.cvCreateCameraCapture (device);
48
49 ## check that capture device is OK
50 if (!swig_this(capture))
51   printf("Error opening capture device\n");
52   exit (1);
53 endif
54
55 ## capture the 1st frame to get some propertie on it
56 frame = highgui.cvQueryFrame (capture);
57
58 ## get size of the frame
59 frame_size = cv.cvGetSize (frame);
60
61 ## get the frame rate of the capture device
62 fps = highgui.cvGetCaptureProperty (capture, highgui.CV_CAP_PROP_FPS);
63 if (fps == 0)
64   ## no fps getted, so set it to 30 by default
65   fps = 30;
66 endif
67
68 ## create the writer
69 writer = highgui.cvCreateVideoWriter ("captured.mpg", MPEG1VIDEO,
70                                       fps, frame_size, true);
71
72 ## check the writer is OK
73 if (!swig_this(writer))
74   printf("Error opening writer\n");
75   exit(1);
76 endif
77
78 while (1)
79   ## do forever
80
81   ## 1. capture the current image
82   frame = highgui.cvQueryFrame (capture);
83   if (swig_this(frame) == 0)
84     ## no image captured... end the processing
85     break
86   endif
87
88   ## write the frame to the output file
89   highgui.cvWriteFrame (writer, frame);
90
91   ## display the frames to have a visual output
92   highgui.cvShowImage ('Camera', frame);
93
94   ## handle events
95   k = highgui.cvWaitKey (5);
96
97   if (k & 0x100 == 27)
98     ## user has press the ESC key, so exit
99     break
100   endif
101 endwhile
102
103
104 ## end working with the writer
105 ## not working at this time... Need to implement some typemaps...
106 ## but exiting without calling it is OK in this simple application
107 ##highgui.cvReleaseVideoWriter (writer)