OpenVideo Documentation

   Main Page       Modules       Class Hierarchy       Alphabetical List       Compound List       File List       Compound Members       Related Pages   

OpenCVSrc.cxx

Go to the documentation of this file.
00001 /* ========================================================================
00002  * Copyright (C) 2005  Graz University of Technology
00003  *
00004  * This framework is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This framework is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this framework; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  * For further information please contact Denis Kalkofen under
00019  * <kalkofen@icg.tu-graz.ac.at> or write to Denis Kalkofen,
00020  * Graz University of Technology, Inffeldgasse 16a, A8010 Graz,
00021  * Austria.
00022  * ========================================================================
00023  * PROJECT: OpenVideo
00024  * ======================================================================== */
00033 // OpenVideo includes for this file
00034 #include <openvideo/nodes/OpenCVSrc.h>
00035 #include <openvideo/openVideo.h>
00036 
00037 // If OpenCV is available
00038 #ifdef ENABLE_OPENCV
00039 
00040 // Standard includes
00041 #include <string>
00042 #include <iostream>
00043 #include <sstream>
00044 #include <vector>
00045 
00046 // OpenCV includes
00047 #include <cv.h>
00048 #include <cvaux.h>
00049 #include <highgui.h>
00050 
00051 // OpenVideo includes
00052 #include <openvideo/nodes/OpenCVSrc.h>
00053 #include <openvideo/Manager.h>
00054 #include <openvideo/State.h>
00055 
00056 // We are using namespaces
00057 using namespace std;
00058 using namespace openvideo;
00059 
00060 // ********************************************************
00061 // PRIVATE CLASS DEFINITION
00062 // ********************************************************
00063 
00064 class OpenCVSrcP
00065 {
00066 public:
00067   // Constructor
00068   OpenCVSrcP(OpenCVSrc * owner) {
00069     this->owner = owner;
00070 
00071     this->captureFrom = CAPTURE_FROM_CAMERA;
00072     this->capture = NULL;
00073     
00074     this->filename = "capture.avi";
00075     this->cameraId = 0;
00076     this->width    = 640;
00077     this->height   = 480;
00078     this->fps      = 15;
00079   }
00080   
00082   ~OpenCVSrcP(void) {
00083     if (capture != NULL) {
00084       cvReleaseCapture(&capture);
00085     }
00086   }
00087 
00089   int captureFrom;
00091   string filename;
00093   int cameraId;
00095   int width;
00097   int height;
00099   int fps;
00100 
00102   CvCapture * capture;
00103 
00104   // Capture type variables
00105   const static int CAPTURE_FROM_CAMERA = 1;
00106   const static int CAPTURE_FROM_FILE   = 2;
00107   
00108 private:
00110   OpenCVSrc * owner;
00111 };
00112 
00113 #undef PUBLIC
00114 #undef PRIVATE
00115 
00116 #define PUBLIC(obj) (obj->owner)
00117 #define PRIVATE(obj) (obj->pimpl)
00118 
00119 // ********************************************************
00120 
00121 
00122 // constructor
00123 OpenCVSrc::OpenCVSrc()
00124 {
00125 #if OPENVIDEO_DEBUG && 0
00126   cout << "OpenCVSrc::OpenCVSrc()" << endl;
00127 #endif
00128 
00129   // Initialize private class
00130   PRIVATE(this) = new OpenCVSrcP(this);
00131 }
00132 
00133 // destructor
00134 OpenCVSrc::~OpenCVSrc()
00135 {
00136 #if OPENVIDEO_DEBUG && 0
00137   cout << "OpenCVSrc::~OpenCVSrc()" << endl;
00138 #endif
00139 }
00140 
00144 void 
00145 OpenCVSrc::initPixelFormats()
00146 {
00147 #if OPENVIDEO_DEBUG && 0
00148   cout << "OpenCVSrc::initPixelFormats()" << endl;
00149 #endif
00150 
00151   inherited::pixelFormats.push_back(PIXEL_FORMAT(FORMAT_B8G8R8));
00152 }
00153 
00157 bool 
00158 OpenCVSrc::setParameter(string key, string value)
00159 {
00160 #if OPENVIDEO_DEBUG && 0
00161   cout << "OpenCVSrc::setParameter(): " << key << " " << value << endl;
00162 #endif
00163 
00164   // Setup a string stream to read variables from the value string
00165   istringstream iss(value);
00166 
00167   // Is the key handled by the parent?
00168   if (inherited::setParameter(key, value)) {
00169     return true;
00170   }
00171   
00172   // Need to handle the key for our selves.
00173   if (key == "capturefrom") {
00174     if (value == "camera") {
00175       PRIVATE(this)->captureFrom = OpenCVSrcP::CAPTURE_FROM_CAMERA;
00176     }
00177     else if (value == "file") {
00178       PRIVATE(this)->captureFrom = OpenCVSrcP::CAPTURE_FROM_FILE;
00179     }
00180     else {
00181       cerr << "Unknown capture from value: " << value << endl;
00182       assert(0);
00183     }
00184     return true;
00185   }
00186   else if (key == "width") {
00187     iss >> PRIVATE(this)->width;
00188     return true;
00189   }
00190   else if (key == "height") {
00191     iss >> PRIVATE(this)->height;
00192     return true;
00193   }
00194   else if (key == "fps") {
00195     iss >> PRIVATE(this)->fps;
00196     return true;
00197   }
00198   else if (key == "cameraId") {
00199     iss >> PRIVATE(this)->cameraId;
00200     return true;
00201   }
00202   else if (key == "filename") {
00203     iss >> PRIVATE(this)->filename;
00204     return true;
00205   }
00206   else { // Key not recognized by any system
00207     cout << "OpenCVSrc::setParameter(): Unable to parse key: '" << key << "' with value: '" << value << "'" << endl;
00208   }
00209 
00210   // Return false if no system was able to recognize the key
00211   return false;
00212 }
00213 
00217 void 
00218 OpenCVSrc::init()
00219 {
00220 #if OPENVIDEO_DEBUG && 0
00221   cout << "OpenCVSrc::init()" << endl;
00222 #endif
00223 
00224   // Make sure the stream-tree is correctly defined
00225   assert(inherited::getInDegree() == 0);
00226 
00227   // Initialize state variable
00228   inherited::state = new State();
00229   inherited::state->clear();
00230   inherited::state->width = PRIVATE(this)->width;
00231   inherited::state->height = PRIVATE(this)->height;
00232 }
00233 
00237 void 
00238 OpenCVSrc::start() 
00239 {
00240 #if OPENVIDEO_DEBUG && 0
00241   cout << "OpenCVSrc::start()" << endl;
00242 #endif 
00243 
00244   // Set up capture process
00245   if (PRIVATE(this)->captureFrom == OpenCVSrcP::CAPTURE_FROM_FILE) {
00246     // FIXME: Use cvCaptureFromAVI instead?
00247     PRIVATE(this)->capture = cvCaptureFromFile(PRIVATE(this)->filename.c_str());
00248   }
00249   else if (PRIVATE(this)->captureFrom == OpenCVSrcP::CAPTURE_FROM_CAMERA) { // Capture from video camera(s)
00250     PRIVATE(this)->capture = cvCaptureFromCAM(PRIVATE(this)->cameraId);
00251   }
00252 
00253   // Check if the capture object is invalid...
00254   if (PRIVATE(this)->capture == NULL) {
00255     cerr << "Unable to open input video stream" << endl;
00256   }
00257   else {
00258     // Set user-defined height and width
00259     cvSetCaptureProperty(PRIVATE(this)->capture, CV_CAP_PROP_FRAME_WIDTH, PRIVATE(this)->width);
00260     cvSetCaptureProperty(PRIVATE(this)->capture, CV_CAP_PROP_FRAME_HEIGHT, PRIVATE(this)->height);
00261     cvSetCaptureProperty(PRIVATE(this)->capture, CV_CAP_PROP_FPS, PRIVATE(this)->fps);
00262 
00263     // Retrieve actual values used
00264     PRIVATE(this)->width  = (int) cvGetCaptureProperty(PRIVATE(this)->capture, CV_CAP_PROP_FRAME_WIDTH);
00265     PRIVATE(this)->height = (int) cvGetCaptureProperty(PRIVATE(this)->capture, CV_CAP_PROP_FRAME_HEIGHT);
00266     PRIVATE(this)->fps    = (int) cvGetCaptureProperty(PRIVATE(this)->capture, CV_CAP_PROP_FPS);
00267 
00268     // FIXME: Have to fake width here because wrong capture property is
00269     // sometimes returned.
00270     if (PRIVATE(this)->width == 0) {
00271       PRIVATE(this)->width = 640;
00272     }
00273     if (PRIVATE(this)->height == 0) {
00274       PRIVATE(this)->height = 480;
00275     }
00276   }
00277 }
00278 
00282 void
00283 OpenCVSrc::stop() 
00284 {
00285 #if OPENVIDEO_DEBUG && 0
00286   cout << "OpenCVSrc::stop()" << endl;
00287 #endif 
00288 
00289   // Close down capture process
00290   cvReleaseCapture(&PRIVATE(this)->capture);
00291   PRIVATE(this)->capture = NULL;
00292 }
00293 
00297 void 
00298 OpenCVSrc::process()
00299 {
00300 #if OPENVIDEO_DEBUG && 0
00301   cout << "OpenCVSrc::process()" << endl;
00302 #endif
00303 
00304   IplImage * image = cvQueryFrame(PRIVATE(this)->capture);
00305   if (image) {
00306     inherited::state->width  = image->width;
00307     inherited::state->height = image->height;
00308     inherited::state->format = FORMAT_R8G8B8;
00309     inherited::state->frame  = (unsigned char *) image->imageData;
00310   }
00311   else {
00312     cerr << "OpenCVSrc::process(): ERROR: Unable to capture image..." << endl;
00313   }
00314 }
00315 
00319 void
00320 OpenCVSrc::postProcess()
00321 {
00322 #if OPENVIDEO_DEBUG && 0
00323   cout << "OpenCVSrc::postProcess()" << endl;
00324 #endif 
00325 
00326   this->state->frame = NULL;
00327 }
00328 
00329 #endif // ENABLE_OPENCV
 This page was generated at Wed May 31 13:04:16 2006 for OpenVideo by Doxygen.
 If you have any comments, please send a message to schmalstieg@icg.tu-graz.ac.at.
www.studierstube.org