Video_Capture.cpp
Go to the documentation of this file.
00001 // Copyright (c) 2013 by Wayne C. Gramlich.  All rights reserved.
00002 
00003 #include <assert.h>
00004 #include <ctype.h>
00005 
00006 #include <opencv2/highgui/highgui_c.h>
00007 
00008 #include "CV.hpp"
00009 #include "File.hpp"
00010 #include "String.hpp"
00011 
00019 
00020 int main(int arguments_size, char * arguments[]) {
00021     CvCapture * capture = NULL;
00022     String_Const capture_base_name = "video_capture";
00023 
00024     if (arguments_size <= 1) {
00025         // No arguments; let the user know the usage:
00026         File__format(stderr,
00027           "Usage: Video_Capture camera_number [capture_base_name]\n");
00028         return 1;
00029     } else {
00030         // Grab the arguments:
00031         String argument1 = arguments[1];
00032         if (arguments_size > 2) {
00033             capture_base_name = arguments[2];
00034         }
00035 
00036         // Figure whether to open a video file or a camera;
00037         if (isdigit(argument1[0])) {
00038             // Open the camera:
00039             unsigned int camera_number = String__to_unsigned(argument1);
00040             int camera_flags = CV_CAP_ANY + (int)camera_number;
00041             capture = cvCreateCameraCapture(camera_flags);
00042             if (capture == NULL) {
00043                 File__format(stderr,
00044                   "Could not open camara %d\n", camera_number);
00045                 return 1;
00046             }
00047 
00048             // Set the frame size:
00049             cvSetCaptureProperty(capture,
00050               CV_CAP_PROP_FRAME_WIDTH, (double)640);
00051             cvSetCaptureProperty(capture,
00052               CV_CAP_PROP_FRAME_HEIGHT, (double)480);
00053         } else {
00054             // Open a video file format:
00055             capture = cvCreateFileCapture(argument1);
00056             if (capture == NULL) {
00057                 File__format(stderr,
00058                   "Could not open video file '%s'\n", argument1);
00059                 return 1;
00060             }
00061         }
00062     }
00063     // We should not be able to here without a open *capture*:
00064     assert(capture != NULL);
00065 
00066     // Create the window to display the video into:
00067     String_Const window_name = "Video_Capture";
00068     cvNamedWindow(window_name, CV__window_auto_size);
00069 
00070     // Do a video loop:
00071     unsigned int capture_number = 0;
00072     while (1) {
00073         // Grab a frame from the video source:
00074         CV_Image frame = cvQueryFrame(capture);
00075         if (frame == (CV_Image)0) {
00076             // When *frame* is null, the video source is at end-of-file
00077             // or disconnected:
00078             break;
00079         }
00080         
00081         // Show the image:
00082         cvShowImage(window_name, frame);
00083 
00084         // Deal with key character:
00085         char character = cvWaitKey(33);
00086         if (character == '\033') {
00087             // [Esc] key causes program to escape:
00088             break;
00089         } else if (character == ' ') {
00090             // Write out image out to file system as a .tga file:
00091             String file_name =
00092               String__format("%s-%02d.pnm", capture_base_name, capture_number);
00093             CV_Image__pnm_write(frame, file_name);
00094             File__format(stderr, "Wrote frame out to file '%s'\n", file_name);
00095             capture_number += 1;
00096             String__free(file_name);
00097         }
00098     }
00099 
00100     // Clean up and leave:
00101     cvReleaseCapture(&capture);
00102     cvDestroyWindow(window_name);
00103 
00104     return 0;
00105 }


fiducial_lib
Author(s): Wayne Gramlich
autogenerated on Thu Jun 6 2019 18:08:04