The mathematical operations

Besides the functions we've learned about so far, OpenCV also provides a number of functions to deal with element-wise mathematical operations. In this section, we're going to go through them briefly, but you can, and you should experiment with them by yourself to make sure that you're familiar with them, and you can comfortably use them in your projects.

The element-wise mathematical functions in OpenCV are as follows:

  • The absdiff function can be used to calculate the absolute difference between the pixels of two images of the same size and type or an image and a scalar. Here's an example:
absdiff(image1, image2, result);

In the preceding code, image1, image2, and result are all Mat objects, and each element in the result represents the absolute difference between corresponding pixels in image1 and image2.

  • The exp function can be used to calculate the exponential of all elements in a matrix:
exp(mat, result); 
  • The log function can be used to calculate the natural logarithm of every element in a matrix:
log(mat, result); 
  • pow can be used to raise all of the elements in a matrix to a given power. This function requires a matrix and a double value that will be the power value. Here's an example:
pow(mat, 3.0, result); 
  • The sqrt function is used to calculate the square root of all elements in a matrix, and it's used as follows:
sqrt(mat, result); 
Functions such as log and pow should not be confused with the functions of the same name in standard C++ libraries. For better readability of your code, consider using the cv namespace before the function name in your C++ code. For instance, you can call the pow function as follows:
cv::pow(image1, 3.0, result);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.41.229