Bitwise logical operations

Just like basic operations, you can also perform bitwise logical operations on all of the elements of two matrices or a matrix and a scalar. For this reason, you can use the following functions:

  • bitwise_not
  • bitwise_and
  • bitwise_or
  • bitwise_xor

It's immediately recognizable from their names that these functions can perform Not, And, Or, and Exclusive OR operations, but let's see how they're used in detail with some hands-on examples:

First things first, the bitwise_not function is used to invert all the bits of all pixels in an image. This function has the same effect as the inversion operation that can be found in most photo editing applications. Here's how it's used:

bitwise_not(image, result); 

The preceding code can be replaced with the following too, which uses an overloaded bitwise not operator (~) in C++:

result = ~image; 

If the image is a monochrome black and white image, the result will contain an image with all white pixels replaced with black and vice versa. In case the image is an RGB color image, the result will be inverted (in the sense of its binary pixel values), which is depicted in the following example image:

The bitwise_and function, or the & operator, is used to perform a bitwise And operation on pixels from two images or pixels from an image and a scalar. Here is an example:

bitwise_and(image, mask, result); 

You can simply use the & operator and write the following instead:

result = image & mask; 

The bitwise_and function can be easily used to mask and extract certain areas in images. For instance, the following image is a demonstration of how bitwise_and results in an image that passes the white pixels and removes the black pixels:

Besides masking certain areas of an image, the bitwise And operation can be used to filter out a channel altogether. To be able to do this, you need to use the second form of the & operator, which takes a matrix and a scalar and performs the And operation between all pixels and that value. Here is an example code that can be used to mask (zero out) the green color channel in an RGB color image:

result = image & Vec3b(0xFF, 0x00, 0xFF); 

Now, let's move on to the next bitwise operation, the Or operation. The bitwise_or and | operators can both be used to perform a bitwise Or operation on two images, or an image and a scalar. Here is an example:

bitwise_or(image, mask, result);

Similar to the bitwise And operation, you can use the | operator in the Or operation and simply write the following instead of the preceding code:

result = image | mask; 

If the And operation was used to pass through the non-zero pixels (or non-black pixels), then it can be said that the Or operation is used to pass through the pixel with the higher value (or brighter) in any of its input images. Here's the result of performing the bitwise Or operation on the previous example images:

Similar to bitwise And operation, you can also use bitwise Or operation to update an individual channel or all the pixels of an image. Here is an example code that shows how you can update only the green channel in an RGB image to have the maximum possible value (which is 255, or hexadecimal FF) in all of its pixels and leave the other channels as they are:

result = image | Vec3b(0x00, 0xFF, 0x00); 

Finally, you can use bitwise_xor, or the ^ operator to perform an Exclusive Or between the pixels of two images, or an image and a scalar. Here is an example:

bitwise_xor(image, mask, result); 

Or simply use the ^ operator and write the following instead:

result = image ^ mask;

Here is the resulting image, if the Exclusive Or operation is performed on the example image from the preceding section:

Notice how this operation leads to the inversion of the pixels in the masked area? Think about the reason behind this by writing down the pixel values on a paper and trying to calculate the result by yourself. Exclusive Or, and all bitwise operations, can be used for many other computer vision tasks if their behavior is clearly understood.

..................Content has been hidden....................

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