Sunday, October 29, 2023

OpenCV and Image Processing - Bitwise Operation

The last section introduced image blending by implementing the algorithm introduced by the OpenCV document. As you can see from the result in Figure from previous post, the image looks a little bit faded out, and both original images become transparent to some extent. It depends on what effects you want to achieve, sometimes this kind of faded-out effect is not ideal, you might want the images to be opaquely added together without fading out.

This post will introduce another way to blend two images, a blending mask will be used in the image blending. The blending mask is a grayscale image that specifies the contribution of each pixel in the input images to the output image. It is also referred to as an alpha mask or alpha matte. The blending mask is used as a weighting function to compute the weighted average of the pixel values from the input images. The values of the blending mask typically range from 0 to 255, where 0 represents complete opacity and 255 represents complete transparency. The blending mask is usually a grayscale image, but it can also be a color image in some cases. However, in this section we use a binary mask, the values are either 0 or 255, with no values in between.

In this post we will create a mask from the image of the bird, and apply it to both images as a bitwise operation to achieve a different blending effect.

OpenCV provides bitwise operations – AND, OR, NOT and XOR. It is very useful when we want to extract something partially from an image and put it in another image. These bitwise operations can be used here to achieve the effects we want.


The algorithm is as following:

g( i , j ) = mask & f0 ( i , j ) + (1 − mask) & f1 ( i , j )

f0 ( i , j ) and f1 ( i , j ) are the original images to blend, and g( i , j ) is the result image; & is the bitwise AND operation.

The bitwise blending process is shown in Figure 1, a mask is created based on Original Image 2 which is bird image. And (1 - mask) is created based on the mask.

Figure 1 Blending Images with Bitwise

The mask is applied to Original Image 2 by bitwise AND, and produces the foreground of the output image, only the bird appears transparently in the foreground image, and other pixels are opaque and appears as black in the foreground, as shown in Figure 2.

Then (1 -mask) is applied to the Original Image 1 by bitwise AND, and produces the background image. The bird appears black here, and all other pixels appear transparently.

Finally, the foreground and background are joined together by bitwise OR to produce the result, as shown in Figure 2.

There are different ways to create a mask based on an image. An effective way to create a mask is introduced in Section 6.8.1 later, the image is converted to HSV color space, and find out the lower and upper range of HSV value to pick up the interested part of the image, in our case the bird. Then use cv2.inRange() function to get the mask. And lastly convert the mask image from grayscale to BGR color space.

Below are the code snippets to create our mask.

1 def remove_background_by_color(self,hsv_lower,hsv_upper,image=None):

2 if image is None:

3 image = self.image

4 imgHSV = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

5 mask = cv2.inRange(imgHSV, hsv_lower, hsv_upper)

6 mask = 255 - mask

7 mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)

8 bg_removed = cv2.bitwise_and(image, mask)

9 return bg_removed, mask

Figure 2 below shows the foreground image by applying the mask on the bird image with bitwise AND operation.

Foreground Image After mask Applied
As shown in the above result, all the background from the original image is removed, only the bird is left, which is the interesting part of this image. This implements the first part of the above formula:

g( i , j ) = mask & f0 ( i , j ) + (1 − mask) & f1 ( i , j )

We'll continue in next post.




Share:

0 comments:

Post a Comment