Hi,

I am using OpenCV to capture video from a webcam while toggling
between camera settings. The idea is to turn on the camera's infrared
lights and then turn them off and look at the difference between the
frames to track objects.

But my problem is that there seems to be old data in the captures.
Meaning, after I turn the infrared off, the video still shows infrared
effects for the first few frames. I think the reason might be that
there is still old frame data in the video capture buffer. Right now I
am calling QueryFrame a few times in order to empty the buffer, but
this results in a time lag. And since I need to toggle quite quickly,
this lag is having a big effect on my results.

Is there a way to empty the buffer quickly and access only live
images, or does anyone know of any other reason this might be happening?

Thank you!

Here is a code snippet:

int _tmain(int argc, _TCHAR* argv[])
{
// Find the webcam device using Direct Show
HRESULT hr;
IBaseFilter *pSrcFilter=NULL;
hr = FindCaptureDevice(&pSrcFilter);

// allocate memory for frame images
IplImage *capframe=0;
IplImage *frame=0;
IplImage *infraFrame=0;

// Use OpenCV capture to stream live video from video device 0
CvCapture* capture = cvCaptureFromCAM(0);
// Create a window to display the video frames
cvNamedWindow("ToothSleuth");
// Create another window to display the infrared video frames
cvNamedWindow("Infrared ToothSleuth");

// Control webcam settings with the IAmCameraControl Interface
CComPtr<IAMCameraControl> pCameraControl;
pSrcFilter->QueryInterface(IID_IAMCameraControl,(void**)&pCameraControl);

capframe=cvQueryFrame(capture);
frame=cvCloneImage(capframe);
infraFrame=cvCloneImage(capframe);

// Begin video capture
while(1) {
// Set infrared to "on"
pCameraControl->Set(CameraControl_Exposure,
0x0001,CameraControl_Flags_Auto);
cvQueryFrame(capture);
cvQueryFrame(capture);
capframe=cvQueryFrame(capture);
cvCopy(capframe,frame);

// Set infrared to "off"
pCameraControl->Set(CameraControl_Exposure,
0x0002,CameraControl_Flags_Manual);
cvQueryFrame(capture);
capframe=cvQueryFrame(capture);
cvCopy(capframe,infraFrame);

// Display the two videos
cvShowImage("ToothSleuth", frame);
cvShowImage("Infrared ToothSleuth", infraFrame);


// Wait for escape key to terminate
if(c==27) {
break;
}
frameCount++;
}

return 0;
}


Posted by 삼백야드


티스토리 툴바