4

If i do:

while(1) { 
   //retrieve image from the camera
   webCamImage=cvQueryFrame(camera) // where 'camera' is cvCreateCameraCapture(0)

   //do some heavy processing on the image that may take around half a second
   funcA()
}

Now when I go to consecutive iterations, it seems that webCamImage lags !

Even if i move the camera, webCamImage takes long time to get updated to the new field of view, and it keeps showing and processing previous field of view camera frames.

I am assuming that cvQuery has some buffer that retrieves the frames.

Can you please advise me on how to get the updated camera view each iteration ?

Many thanks

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
Louis
  • 1,265
  • 1
  • 15
  • 22
  • 2
    Consider that perhaps layers lower than opencv might have buffers beyond your control, so it might not be so easy. Perhaps you could create a thread for processing that calls [`cvRetrieveFrame`](http://opencv.jp/opencv-1.0.0_org/docs/ref/opencvref_highgui.htm#decl_cvRetrieveFrame) only when needed (beware of synchronization issues) and a main thread that calls `cvGrabFrame` regularly. – user786653 Aug 30 '11 at 18:43
  • It is very difficult to answer your question because OpenCV can use up to 15 different APIs for video-capturing (See [this post](http://stackoverflow.com/questions/7247475/opencv-2-3-c-qtgui-problem-initializing-some-specific-usb-devices-and-setups/7264572#7264572) for details). Please post more info about your OS and OpenCV version (which binaries you have downloaded or cmake output if you have complied OpenCV yourself). – Andrey Kamaev Aug 31 '11 at 23:31

1 Answers1

1

cvQueryFrame is just a wrapper that calls 2 other functions: cvGrabFrame, which gets data from the camera very quickly, and cvRetrieveFrame, which uncompresses this data and puts it into an IplImage. If you need frames captured immediately, just grab the frame, and retrieve it for processing later.

See http://opencv.jp/opencv-1.0.0_org/docs/ref/opencvref_highgui.htm FMI

Having said that, though, I use cvQueryFrame with a typical webcam, and I have no trouble getting dozens of frames per second. Any chance that the part that's lagging is actually in your funcA() call? edit: from the comment in your code, I see that funcA() is indeed the slow part. If it takes half a second to execute, you'll only get a new frame from cvQUeryFrame every half second, just as you describe. Try either making funcA faster, or put it in a separate thread.

and as a friendly reminder, the IplImage returned by cvQueryFrame/cvRetrieveFrame should not be modified or deleted by the user; it's part of OpenCV's internal system for storing things, and if you're doing anything interesting with it, you should make a copy. I don't know if you're doing this already, but I certainly did it wrong when I started out.

Alan
  • 192
  • 1
  • 8