Bitwise Operations on Images in Computer Vision
Overview
A bitwise operator is a character that represents an operation that operates on data at the bit level rather than bytes or bigger data units, as is more frequent. Contrarily, the majority of ordinary operators operate on single or multiple bytes, which typically include eight bits in most systems.
Bitwise operators can speed up and improve the efficiency of some code since they allow for higher precision and consume fewer resources.OpenCV is a library for image processing. Several issues in computer vision and machine learning applications are resolved with OpenCV. In this article, we'll show you how OpenCV may be used to do bitwise operations on images, which is one of its interesting uses.
Introduction
Bitwise operations in computer vision are useful for manipulating binary images, defining non-rectangular regions of interest, and extracting portions of an image. They operate on binary representations of numbers, rather than their values, which can improve efficiency and precision in some applications. Bitwise operations can be used to create new images, apply watermarks, and perform other operations on existing images. These operations work on individual pixels in the image to produce correct results.
Bitwise Operations on Images
There are built-in methods in OpenCV for and, or, and not operations. Bitwise AND, Bitwise OR, Bitwise NOT and Bitwise XOR
OR Operation
The bitwise OR operation performs a logical OR between the corresponding bits of the input images. The result is a new image where each pixel has a value that is the bitwise OR of the corresponding pixels in the input images.
Syntax: cv2.bitwise_or(src1, src2, dst, mask=None)
Parameters
src1: First input image.
src2: Second input image.
dst: Output image that has the same size and depth as the input images.
mask: Optional argument that specifies a mask to be applied to the input images.
AND Operation
The bitwise AND operation performs a logical AND between the corresponding bits of the input images. The result is a new image where each pixel has a value that is the bitwise AND of the corresponding pixels in the input images.
Syntax: cv2.bitwise_and(src1, src2, dst, mask=None)
Parameters
src1: First input image.
src2: Second input image.
dst: Output image that has the same size and depth as the input images.
mask: Optional argument that specifies a mask to be applied to the input images.
NOT Operation
The bitwise NOT operation performs a logical NOT on each pixel of the input image. The result is a new image where each pixel has a value that is the bitwise NOT of the corresponding pixel in the input image.
Syntax: cv2.bitwise_not(src, dst, mask=None)
Parameters
src: Input image.
dst: Output image that has the same size and depth as the input image.
mask: Optional argument that specifies a mask to be applied to the input image.
XOR Operation
The bitwise XOR operation performs a logical XOR between the corresponding bits of the input images. The result is a new image where each pixel has a value that is the bitwise XOR of the corresponding pixels in the input images.
Syntax: cv2.bitwise_xor(src1, src2, dst, mask=None)
Calculates the bit-wise "exclusive or" operation on two arrays or an array and a scalar per element.
Parameters
src1: First input image.
src2: Second input image.
dst: Output image that has the same size and depth as the input images.
mask: Optional argument that specifies a mask to be applied to the input images.
Applications of Bitwise Operations
Image Masking
A binary image of zero- and non-zero values is referred to as a mask. Masks are used to extract specific regions of interest from images using bitwise techniques. By applying thresholding to images, masks can be produced. Any pixels that are zero in the mask are set to zero in the output image when a mask is applied to another binary or a grayscale image of the same size. The rest are unaltered. Both logical AND and pixel multiplication can be used to create masking, with the latter typically being faster.
Here is an example of an image that has been masked to remove anything that is not skin.
Image Blending
This operator creates a blend from two identical-sized input images. The value of each pixel in the output image is a linear combination of the corresponding pixel values in the input images, much like pixel addition (where this operator takes as input two equally sized images and produces as output a third image of the same size as the first two). The user-specified coefficients of the linear combination provide the ratio by which to scale each image before combining it. When these ratios are used, the output pixel values are not allowed to go above the maximum pixel value.
The following equation is used to add images:
−
f0, f1 are the two input images. f0 may also be a constant in some applications, enabling the addition of a constant offset value to a single image. The blending ratio(α), or, regulates how much each input image will influence the output. α can either be a fixed value applied to every pixel in the image or it can be calculated individually for each pixel using a mask. The mask's size must then match the size of the photos exactly. Moreover, blending can be utilized in images to create attractive effects.
Object Detection and Segmentation
Object/Image segmentation is a task in image processing that divides the image into different parts so that pixels in those regions have similar properties.
Image segmentation comes in two different flavours:
Local segmentation: This type of segmentation focuses on a particular region or section of the image.
Global segmentation is concerned with segmenting the entire image. We can only create a bounding box corresponding to each class in the image by using Object Detection models. Nevertheless, because the bounding boxes are either rectangular or square, it won't reveal anything about the object's shape. Image segmentation will produce pixel-wise masks for each object, making it possible to understand the object's finer details. the bitwise AND operation is used to mask the image for segmentation. We conduct a bitwise AND operation on the original image and the mask to display just the segmented portions of the image. To see the image split, return the image to RGB (while being comparable to the original image).
Image Thresholding
Image thresholding is a straightforward but efficient technique for separating an image's foreground from its background. By transforming grayscale images into binary images, this image analysis technique is a sort of image segmentation that isolates objects. Picture thresholding works best in images with a lot of contrast.
Techniques for Performing Bitwise Operations
OpenCV Library
Images can be altered using bitwise techniques. These bitwise approaches can be used to create new images, apply watermarks to existing images, and perform other operations on existing images in computer vision applications. Unlike other morphing approaches in OpenCV, these operations work on the individual pixels in the image to get correct results. There are built-in methods in OpenCV for and, or, not, and xor operations. Bitwise and, Bitwise or, Bitwise not, and Bitwise xor are their acronyms. Implementation of Bitwise Operations by OpenCV library
OUTPUT
bitwise_AND
bitwise_OR
bitwise_NOT
bitwise_XOR
Python Programming Language
Bitwise operators look virtually the same across different programming languages.
Operator | Example | Meaning |
---|---|---|
& | a & b | Bitwise AND |
I | a I b | Bitwise OR |
^ | a ^ b | Bitwise XOR (exclusive OR) |
~ | ~a | Bitwise NOT |
<< | a << m | Bitwise left shift |
>> | a >> n | Bitwise right shift |
MATLAB Environment
MATLAB provides various functions for bitwise operations like 'bitwise and', 'bitwise or' and 'bitwise not' operations, shift operations, etc.
Functions | Meaning |
---|---|
bitand | Bit-wise AND |
bitor | Bit-wise OR |
bitxor | Bit-wise XOR |
bitcmp | Bit-wise complement |
bitget | Get bit at specified position |
bitset | Set bit at specific location |
bitshift | Shift bits specified number of places |
swapbytes | Swap byte ordering |
Examples of Bitwise Operations in Image Processing
1. Creating Binary Masks for Object Detection
OUTPUT:
2.Blending Two Images Together
OUTPUT
BLENDED IMAGE
3.Segmenting an Object From a Background
OUTPUT
Input Image
SEGMENATATION
4. Performing Image Thresholding for Image Enhancement
OUTPUT
The image is first converted to grayscale. Then, adaptive thresholding is applied using OpenCV's cv2.adaptiveThreshold() function. This function applies thresholding to each pixel in the image based on the mean of the surrounding pixels. The result is a binary image with enhanced contrast.
Advantages and Limitations of Bitwise Operations
Advantages
- As bitwise operations work with binary representations of numbers rather than their values, they are quick and effective.
- They can be applied to the definition of non-rectangular regions of interest, the extraction of particular areas from an image, and the creation of new images.
- To carry out more difficult jobs, bitwise operations can be integrated with other image processing methods like thresholding and morphological operations.
Disadvantages
- Only binary images with just two-pixel values are acceptable for bitwise operations (typically black and white). They cannot be applied directly to colour or grayscale photos.
- When used on images that are noisy or have low contrast, bitwise operations might not always yield accurate results.
- Bitwise operations can sometimes result in jagged edges and artefacts in the produced images, making them unsuitable for applications that call for exact object boundaries.
- For beginners, bitwise operations might be difficult since they call for a solid grasp of logical operations and binary representations.
Conclusion
- As a result, bitwise operations are an effective tool in computer vision for modifying binary images, specifying non-rectangular regions of interest, removing particular areas from an image, and producing new images.
- Because they work with binary representations of numbers, they are quick and effective.
- Bitwise operations, however, are restricted to binary images and may not always yield accurate results when used on noisy or low-contrast images, which is one of its drawbacks. Also, they might not be appropriate for jobs that need exact object bounds.
- In computer vision, bitwise operations are a crucial method that should be utilised in conjunction with other methods of image processing to produce more precise and effective results.