The subtraction operation

Similar to adding two Mat objects to each other, you can also subtract all elements of one image from another using the subtract function or the - operator. Here's an example:

Mat image = imread("Test.png"); 
Mat overlay = imread("Overlay.png"); 
Mat result; 
subtract(image, overlay, result); 

The last line in the preceding code can also be replaced with this:

result = image - overlay; 

Here's the result of the subtraction operation if we used the same two images from the previous examples:

Notice how the subtraction of higher pixel values (brighter pixels) from the source image results in the dark color of the overlay text. Also note that the subtraction operation depends on the order of its operands, unlike addition. Try swapping the operands and see what happens for yourself.

Just like addition, it's also possible to multiply a constant number with all of the pixels of an image. You can guess that subtraction of a constant value from all pixels will result in a darker image (depending on the subtracted value) which is the opposite of the addition operation. Here's an example of making an image darker with a simple subtraction operation:

result = image - 80;

In case the source image is a three-channel RGB image, you need to use a vector as the second operand:

result = image - Vec3b(80, 80, 80); 
..................Content has been hidden....................

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