00001
00002 #include "flycapturevs.h"
00003 #include "iniparser.h"
00004 #include <iostream>
00005
00006 #define DRAGONFLY 0
00007 #define FLEA 1
00008
00009 void FlyCaptureFactory::registerParameters(ParamSection *sec) {
00010 sec->addBoolParam("useFlyCapture", &use, true);
00011 };
00012
00013 VideoSource *FlyCaptureFactory::construct() {
00014 if (use) {
00015 FlyCaptureVideoSource *vs = new FlyCaptureVideoSource();
00016 if (vs->initialize()) return vs;
00017 delete vs;
00018 }
00019 return 0;
00020 }
00021
00022 FlyCaptureVideoSource::FlyCaptureVideoSource()
00023 {
00024
00025 flycaptureCreateContext( &context );
00026 playing = true;
00027 imagesExist = false;
00028 }
00029
00030
00031
00032 bool FlyCaptureVideoSource::initialize()
00033 {
00034
00035 FlyCaptureError error = flycaptureInitialize( context, 0 );
00036 if(error == FLYCAPTURE_FAILED)
00037 {
00038 return false;
00039 } else {
00040
00041
00042 error = flycaptureStart(context, FLYCAPTURE_VIDEOMODE_ANY, FLYCAPTURE_FRAMERATE_30);
00043
00044 if(error != FLYCAPTURE_OK){
00045 return false;
00046 }
00048 flycaptureGrabImage2(context,&pImage);
00049 int cameraWidth = pImage.iCols;
00050 int cameraHeight= pImage.iRows;
00051 tempImageColor = cvCreateImage(cvSize(cameraWidth, cameraHeight), IPL_DEPTH_8U, 3);
00052 tempImageGray = cvCreateImage(cvSize(cameraWidth, cameraHeight), IPL_DEPTH_8U, 1);
00053
00054 imagesExist=true;
00055 return true;
00056 }
00057 }
00058
00059 static void bayerDownsampleColor(const FlyCaptureImage *src, IplImage *dst)
00060 {
00061 const unsigned char *srcpix = (const unsigned char *) src->pData;
00062 unsigned char *dstpix = (unsigned char *) dst->imageData;
00063
00064 dst->channelSeq[0] = 'B';
00065 dst->channelSeq[1] = 'G';
00066 dst->channelSeq[2] = 'R';
00067
00068 for (int y=0; y< dst->height; y++) {
00069 for (int x=0; x< dst->width; x++) {
00070 *dstpix++ = srcpix[0];
00071 *dstpix++ = (srcpix[1] + srcpix[src->iRowInc]) >> 1;
00072 *dstpix++ = srcpix[src->iRowInc+1];
00073
00074
00075 srcpix += 2;
00076 }
00077 srcpix += src->iRowInc;
00078 }
00079 }
00080
00081 bool FlyCaptureVideoSource::getFrame(IplImage *dst)
00082 {
00083 if (!playing) return true;
00084
00085 FlyCaptureImage fim;
00086
00087 fim.iCols = dst->width;
00088 fim.iRows = dst->height;
00089 fim.iRowInc = dst->widthStep;
00090 fim.pData = (unsigned char*)dst->imageData;
00091 bool ok=true;
00092
00093 if (dst->depth==IPL_DEPTH_8U) {
00094 switch(dst->nChannels) {
00095 case 1: fim.pixelFormat = FLYCAPTURE_MONO8; break;
00096 case 3: fim.pixelFormat = FLYCAPTURE_BGR; break;
00097 default:
00098 ok = false;
00099 }
00100 } else
00101 ok =false;
00102
00103 flycaptureGrabImage2(context,&pImage);
00104 FlyCaptureError error = flycaptureConvertImage(context, &pImage, &fim);
00105 if(error != FLYCAPTURE_OK){
00106 std::cerr << "FlyCapture: Can't convert frame !\n";
00107 return false;
00108 }
00109
00110 return true;
00111 }
00112
00113
00114
00115 FlyCaptureVideoSource::~FlyCaptureVideoSource()
00116 {
00117 flycaptureStop(context);
00118 flycaptureDestroyContext( context );
00119 if(imagesExist)
00120 cvReleaseImage(&tempImageColor);
00121 if(imagesExist)
00122 cvReleaseImage(&tempImageGray);
00123
00124 }
00125
00126 void FlyCaptureVideoSource::start() {
00127 playing = true;
00128 }
00129
00130 void FlyCaptureVideoSource::stop() {
00131 playing = false;
00132 }
00133
00134 void FlyCaptureVideoSource::getSize(int &width, int &height)
00135 {
00136 flycaptureGrabImage2(context,&pImage);
00137 width = pImage.iCols;
00138 height= pImage.iRows;
00139 }