Next is to create (1 – mask), technically this is 255 – mask, because the mask is created in a grayscale channel, which has values ranging from 0 to 255. However, in this case we deal it with the binary channel, the values are either 0 or 255, meaning either 100% opacity or 100% transparency, no other values in between. Although in other cases the values in between could indicate the percentage of transparency.
Figure 3 below shows the result of (1 – mask).
Then apply (1 – mask) to the second image as bitwise AND operation to produce
the background image, the result is shown in Figure 4 below.
The pixels of the bird are removed, and all others remain on the result image. This implements the second part of the formula:
g( i , j ) = mask & f0 ( i , j ) + (1 − mask) & f1 ( i , j )
Finally, the two images are merged by bitwise OR operation, or just simply add them up, the result is shown as Figure 5 below.
This implements the whole formula:
g( i , j ) = mask & f0 ( i , j ) + (1 − mask) & f1 ( i , j )
By comparing it with the one in the last section, the difference is obvious, this one does not have any faded-out effect.
In this case the foreground and background are merged with 100% transparency. In real-world projects, depending on what effects you want to achieve, different methods can be selected.
Below is the code snippet in the ImageProcessing class-
1 def blend_with_mask(self, blend, mask, image=None):
2 if image is None:
3 image = self.image
4 blend = cv2.resize(blend, image.shape[1::-1])
5 mask = cv2.resize(mask, image.shape[1::-1])
6 result = cv2.bitwise_and(blend, mask) +
cv2.bitwise_and(image,(255-mask))
7 return result
Line 1 Define blend_with_mask() function, the blend image and mask image are
passed as parameters.
Line 4 - 5 The mask and blend images must be in the same size as the original image, use cv2.resize() to make them the same dimension, in case they are not.
Line 6 Use bitwise AND on blend image and mask image to produce the foreground
image.
Use bitwise AND again on the original image with (255-mask) to produce the
background image.
Then add both images using plus operation to make the resulting image.
cv2.bitwise_or() can also be used, they are the same in this case.
The full source codes for this example are in BlendImageBitwise.py file.
1 import cv2
2 import common.ImageProcessing as ip
3 4 def blendTwoImagesWithMask(imageFile1, imageFile2):
5 title = "Blend Two Images"
6 iproc = ip.ImageProcessing(title, imageFile1)
7 iproc.show(title="Original Image1",
image=iproc.image)
8 toBlend = cv2.imread(imageFile2)
9 iproc.show(title="Original Image2", image=toBlend)
10_, mask = iproc.remove_background_by_color(
hsv_lower = (90, 0, 100),
11 hsv_upper = (179,255,255),
12 Image = toBlend )
13 iproc.show(title="Mask from Original Image2",
image=mask )
14 iproc.show(title="(1-Mask)", image= (255-mask))
15 blend = iproc.blend_with_mask(toBlend, mask)
16 iproc.show(title=title, image=blend)
17 cv2.waitKey(0)
18 cv2.destroyAllWindows()
19
20 if __name__ == "__main__":
21 image1 = "../res/sky001.jpg"
22 image2 = "../res/bird002.jpg"
23 blendTwoImagesWithMask(image1, image2)
Line 10 – 12 is to call remove_background_by_color() function in ImageProcessing class to get the mask of the bird image. The value of parameters of hsv_lower and hsv_upper are specific only to this image, different images should have different values to get a mask.
0 comments:
Post a Comment