Common pitfalls and suggested solutions

OpenCV is very feature rich and provides multiple solutions and paths to resolve a visual-understanding problem. With this great power also comes hard work, choosing and crafting the best processing pipeline for the project requirements. Having multiple options means that probably finding the exact best performing solution is next to impossible, as many pieces are interchangeable and testing all the possible options is out of our reach. This problem's exponential complexity is compounded by the input data; more unknown variance in the incoming data will make our algorithm choices even more unstable. In other words, working with OpenCV, or any other computer vision library, is still a matter of experience and art. A priori intuition as to the success of one or another route to a solution is something computer vision engineers develop with years of experience, and for the most part there are no shortcuts.

There is, however, the option of learning from someone else's experience. If you've purchased this book, it likely means you are looking to do just so. In this section, we have prepared a partial list of problems that we encountered in our years of work as computer vision engineers. We also look to propose solutions for these problems, like we used in our own work. The list focuses on problems arising from computer vision engineering; however, any engineer should also be aware of common problems in general purpose software and system engineering, which we do not enumerate here. In practice, no system implementation is without some problems, bugs, or under-optimizations, and even after following our list, one will probably find there is much more left to do.

The primary common pitfall in any engineering field is making assumptions instead of assertions. For any engineer, if there's an option to measure something, it should be measured, even by approximation, establishment of lower and upper bounds, or measuring a different highly correlated phenomenon. For some examples on which metrics can be used for measurement in OpenCV, refer to Chapter 20Finding the Best OpenCV Algorithm for the Job. The best made decisions are the informed ones, based on hard data and visibility; however, that is often not the privilege of an engineer. Some projects require a fast and cold start that forces an engineer to rapidly build up a solution from scratch, without much data or intuition. In such cases, the following advice can save a lot of grief:

#define BOOST_TEST_MODULE binarization test
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE( binarization_test )
{
// On empty input should return empty output
BOOST_TEST(binarization_function(cv::Mat()).empty())
// On 3-channel color input should return 1-channel output
cv::Mat input = cv::imread("test_image.png");
BOOST_TEST(binarization_function(input).channels() == 1)
}

After compiling this file, it creates an executable that will perform the tests and exit with a status of 0 if all tests passed or 1 if any of them failed. It is common to mix this approach with CMake's CTest (https://cmake.org/cmake/help/latest/manual/ctest.1.html) feature (via ADD_TEST in the CMakeLists.txt files), which facilitates building tests for many parts of the code and running them all upon command.