Using OpenCV functions

In this recipe, we used a loop with iterators in order to perform our computation. Alternatively, we could have achieved the same result by calling a sequence of OpenCV functions. The color detection method would then be written as follows:

  cv::Mat ColorDetector::process(const cv::Mat &image) { 
   
         cv::Mat output; 
         // compute absolute difference with target color 
         cv::absdiff(image,cv::Scalar(target),output); 
         // split the channels into 3 images 
         std::vector<cv::Mat> images; 
         cv::split(output,images); 
         // add the 3 channels (saturation might occurs here) 
         output= images[0]+images[1]+images[2]; 
         // apply threshold 
         cv::threshold(output,  // input image 
                       output,  // output image 
                       maxDist, // threshold (must be < 256) 
                       255,     // max value 
         cv::THRESH_BINARY_INV); // thresholding mode 
   
         return output; 
   } 

This method uses the absdiff function that computes the absolute difference between the pixels of an image and, in this case, a scalar value. Instead of a scalar value, another image can be provided as the second argument to this function. In the latter case, a pixel-by-pixel difference will be applied; consequently, the two images must be of the same size. The individual channels of the different image are then extracted using the split function (which is discussed in the There's more... section of the Performing simple image arithmetic recipe of Chapter 2, Manipulating the Pixels), in order to be able to add them together. It is important to note that the result of this sum may sometimes be greater than 255, but because saturation is always applied, the result will be stopped at 255. The consequence is that with this version, the maxDist parameter must also be less than 256; this should be corrected if you consider this behavior unacceptable. The last step is to create a binary image by using the threshold function. This function is commonly used to compare all the pixels with a threshold value (the third parameter), and in the regular thresholding mode (cv::THRESH_BINARY), it assigns the defined maximum value (the fourth parameter) to all the pixels greater than the threshold and 0. Here, we used the inverse mode (cv::THRESH_BINARY_INV), in which the defined maximum value is assigned to the pixels that have a value lower than, or equal to, the threshold. The cv::THRESH_TOZERO_INV and cv::THRESH_TOZERO_INV modes, which leave the pixels greater than or lower than the threshold unchanged, are also of interest.

Using OpenCV functions is always a good idea. You can quickly build complex applications and potentially reduce the number of bugs. The result is often more efficient (thanks to the optimization efforts invested by the OpenCV contributors). However, when many intermediate steps are performed, you may find that the resulting method consumes more memory.