Friday, May 20, 2011

Memory Management for OpenCV applications

Memory management in OpenCV is an important task because:
  1. Most of OpenCV coding is done in C,C++ where programmers have to themselves deal with pointers, references (not have dealt with python programming in OpenCV). 
  2. No garbage collection in these languages (destructors in C++ claim to do it, but not sure how efficient it is).
I'm talking about managing memory, where one would have to deal with 1000's of images (video stream, stream of images over the network, etc) in real time.

Update: I would suggest moving to C++ API or Python API of OpenCV instead of C. They have better Memory Management internally.

Btw, My other programs in OpenCV will be posted here.

Things NEVER to do in OpenCV:
  • DECLARE structures, variables in a loop

    instead it should be (assuming function will return the right image):

  • use "create" functions in a loop. (cvCreateImage, cvCreateMat, etc).

    instead, it should be something as:

Things to do:

  • recycling: reuse already allocation memory ( img_src = find_a_face(img_src) )
  • pass by reference instead of pass by value.
  • release images, mats, storage, etc after they are done with ( cvReleaseImage(&img_src) ).
  • try to identify variable that remain constant through out the loop, declare it separately (may be from the constructor).
  • make use of static variable wherever necessary.

Also cool website has some stuff on OpenCV Memory management:

No comments:

Post a Comment