OpenCVSrc.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00033
00034 #include <openvideo/nodes/OpenCVSrc.h>
00035 #include <openvideo/openVideo.h>
00036
00037
00038 #ifdef ENABLE_OPENCV
00039
00040
00041 #include <string>
00042 #include <iostream>
00043 #include <sstream>
00044 #include <vector>
00045
00046
00047 #include <cv.h>
00048 #include <cvaux.h>
00049 #include <highgui.h>
00050
00051
00052 #include <openvideo/nodes/OpenCVSrc.h>
00053 #include <openvideo/Manager.h>
00054 #include <openvideo/State.h>
00055
00056
00057 using namespace std;
00058 using namespace openvideo;
00059
00060
00061
00062
00063
00064 class OpenCVSrcP
00065 {
00066 public:
00067
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
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
00123 OpenCVSrc::OpenCVSrc()
00124 {
00125 #if OPENVIDEO_DEBUG && 0
00126 cout << "OpenCVSrc::OpenCVSrc()" << endl;
00127 #endif
00128
00129
00130 PRIVATE(this) = new OpenCVSrcP(this);
00131 }
00132
00133
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
00165 istringstream iss(value);
00166
00167
00168 if (inherited::setParameter(key, value)) {
00169 return true;
00170 }
00171
00172
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 {
00207 cout << "OpenCVSrc::setParameter(): Unable to parse key: '" << key << "' with value: '" << value << "'" << endl;
00208 }
00209
00210
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
00225 assert(inherited::getInDegree() == 0);
00226
00227
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
00245 if (PRIVATE(this)->captureFrom == OpenCVSrcP::CAPTURE_FROM_FILE) {
00246
00247 PRIVATE(this)->capture = cvCaptureFromFile(PRIVATE(this)->filename.c_str());
00248 }
00249 else if (PRIVATE(this)->captureFrom == OpenCVSrcP::CAPTURE_FROM_CAMERA) {
00250 PRIVATE(this)->capture = cvCaptureFromCAM(PRIVATE(this)->cameraId);
00251 }
00252
00253
00254 if (PRIVATE(this)->capture == NULL) {
00255 cerr << "Unable to open input video stream" << endl;
00256 }
00257 else {
00258
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
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
00269
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
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