Fork me on GitHub

Android Best Practices

General Notes

Debugging

  • Use Mat.toString() and Mat.dump() methods for debugging.
  • If you develop in C++, debug your code on a host. But be aware of the performance difference. We recommend to develop/debug/optimize C++ code on a host, but please check the real speed of application on a device.

Performance

Memory

Memory efficiency should be your first concern. As we all know, memory reallocations are deadly for the device accumulator, so try to avoid them.

  1. Avoid excessive memory allocations and copyings.
  2. Initialize all the matrices at the application start and reuse them later. Please note that if the matrix doesn’t have required size and depth, OpenCV will reallocate it before usage.  Simply create all the matrices with appropriate size and depth for every purpose beforehand and use them everywhere.

JNI calls

  1. Don’t use too many calls to OpenCV Java API, because JNI-calls are really expensive. For example if you want to fill your image with zeros, don’t use put method, call the Mat.setTo() instead.
  2. Be careful with OpenCV calls within loops. Better to prepare everything outside the loop and to avoid repetitive JNI-calls.