Search code examples
c++opencvcomputer-visionflycapture

Using embeddedFrameCounter in the FlyCapture2 C++ API


I want to implement a framesafe capture with Point Grey Research cameras. To do that, I want to check if any frames have been missed by any cameras. The wrapper class that I am extending to do this is using the FlyCapture 2 API, so I would like to keep using that if possible.

The PGR FAQ on this question only refers to the old API and IEEE-1394 cameras (the ones I use use USB 3.0). I am trying to use the frame counting functionality previously provided by uiSeqNum through the ImageMetadata object and its embeddedFrameCounter member. However, it always seems to be 0.

I wonder if I am making a stupid mistake along the way or if it's a bug. In any case this code should run if you link the necessary files for OpenCV and Flycapture.

#include<iostream>
#include"opencv2\opencv.hpp"
#include"FlyCapture2.h"

int main()  {
    BusManager busMgr;
    PGRGuid guid;
    Camera cam;
    Image rawImage, convertedimage;

    busMgr.GetCameraFromIndex(0, &guid);
    cam.Connect(&guid);
    cam.SetVideoModeAndFrameRate(VIDEOMODE_640x480RGB,FRAMERATE_30);
    cam.StartCapture();

    IplImage* src_img;
    while ((waitKey(30) > 0) == false) {
        cam.RetrieveBuffer(&rawImage);
        ImageMetadata metadata = rawImage.GetMetadata();
        cout << "framecount: " << metadata.embeddedFrameCounter << endl;

        rawImage.Convert(PIXEL_FORMAT_BGR, &convertedimage);
        src_img = cvCreateImage(cvSize(rawImage.GetCols(),rawImage.GetRows()),IPL_DEPTH_8U,3);
        memcpy(src_img->imageData, convertedimage.GetData(), convertedimage.GetDataSize());

        cvShowImage("Test", src_img);
        cvReleaseData(src_img)
    }
    waitKey(0);
}

As an aside: tips on improving the memory management in this capture loop are appreciated.


Solution

  • Alright: you have to activate the frame count in advance, like most if not all other embedded image metadata. You can do it with the EmbeddedImageInfo object like this (if you insert these lines after the call to connect the camera):

    EmbeddedImageInfo EmbeddedInfo;
    cam.GetEmbeddedImageInfo(&EmbeddedInfo);
    
    if (EmbeddedInfo.frameCounter.available == true) {
        EmbeddedInfo.frameCounter.onOff = true; 
    }
    else {
        cout << "Framecounter is not available!" << endl;
    }
    
    cam.SetEmbeddedImageInfo(&EmbeddedInfo);
    

    The sample ExtendedShutterEx provides basically this code snippet more generally with error checking and for multiple cameras.