Change the Background of Any Image with 5 Lines of Code
By: Ayoola Olafenwa
Tags: Machine Learning, Artificial Intelligence, Image Segmentation, Computer Vision, Object Detection

Change the Background of Any Image with 5 Lines of Code

Photo Collage by Author

Image segmentation has a lot of amazing applications that solve different computer vision problems. PixelLib is a library created to ensure easy integration of image segmentation in real life applications. PixelLib now supports a feature known as image tuning.

Image Tuning: It is the change in the background of an image through image segmentation. The key role of image segmentation is to remove the objects segmented from the image and place them in the new background created. This is done by producing a mask for the image and combining it with the modified background. We make use of deeplabv3+ model trained on pascalvoc dataset. The model supports 20 common object categories, which means you can change the background of these objects in images.

The model supports the following objects listed below;

person,bus,car,aeroplane, bicycle, ,motorbike,bird, boat, bottle, cat, chair, cow, dinningtable, dog, horse pottedplant, sheep, sofa, train, tv

Background effects supported are:

1 Changing the background of an image with a picture

2 Assigning a distinct color to the background of an image.

3 Blurring the background of an image

4 Grayscaling the background of an image

Install PixelLib and its dependencies:

Install Tensorflow with:(PixelLib supports tensorflow 2.0 and above)

pip3 install tensorflow

Install PixelLib with

pip3 install pixellib

If installed, upgrade to the latest version using:

pip3 install pixellib — upgrade

Change the background of an image with a picture

PixelLib makes it possible to change the background of any image with a picture with just 5 lines of code.

sample.jpg

We want to change the background of the image above with the image provided below.

background.jpg

Code to change the background of an image with a picture

import pixellib from pixellib.tune_bg import alter_bg change_bg = alter_bg()

We imported pixellib, and from pixellib, we imported in the class alter_bg. We created an instance of the class.

change_bg.load_pascalvoc_model("deeplabv3_xception_tf_dim_ordering_tf_kernels.h5")

We loaded deeplabv3+ model. Download deeplabv3+ pascalvoc model from here.

change_bg.change_bg_img(f_image_path = "sample.jpg",b_image_path = "background.jpg", output_image_name="new_img.jpg")

We called the function change_bg_img that handled changing the background of the image with a picture.

The function takes the following parameters:

f_image_path: This is the foreground image, the image which background would be changed.

b_image_path: This is the image that will be used to change the background of the foreground image.

output_image_name: The new image with a changed background.

output Image

WOW! This is beautiful, we have successfully replaced the background of our image.

We are able to use PixelLib to perform excellent foreground and background subtraction through image segmentation.

Code to Obtain output array of the changed image array

For specialized uses, you can easily obtain the array of the changed image with the modified code below.

Assign a distinct color to the background of an image

You can assign a distinct color to the background of your image, just the way you are able to change the background of an image with a picture. This is also possible with five lines of code.

Code to assign a distinct color to the background of an image

It is very similar to the code used above for changing the background of an image with a picture. The only difference is that we replaced the function change_bg_img to color_bg, the function that handled color change.

change_bg.color_bg("sample.jpg", colors = (0, 128, 0), output_image_name="colored_bg.jpg")

The function color_bg takes the parameter colors and we provide the RGB value of the color we want to use. We want the image to have a green background and the color’s RGB value is set to green which is (0, 128, 0).

green background

Note:You can assign any color to the background of your image by providing the RGB value of the color.

change_bg.color_bg("sample.jpg", colors = (255, 255, 255), output_image_name="colored_bg.jpg")

We want to change the background of the image to white and set color’s RGB value to white which is (255,255,255).

white background

The same image with a white background.

Code to Obtain output array of the colored image

For specialized uses, you can easily obtain the array of the changed image with the modified code below.

Grayscale the background of an image

Grayscale the background of any image using the same lines of code with PixelLib.

Code to grayscale the background of an image

change_bg.gray_bg(“sample.jpg”,output_image_name=”gray_img.jpg”)

It is still the same code except we called the function gray_bg to grayscale the background of the image.

output image

Note:The background of the image would be altered and the objects present would maintain their original quality.

Code to Obtain output array of the grayed image

Blur Image Background

You can apply the effect of blurring the background of an image, and it is possible to control how blur the background will be.

sample2.jpg

change_bg.blur_bg("sample2.jpg", low = True, output_image_name="blur_img.jpg")

We called the function blur_bg to blur the background of the image, and set the blurred effect to be low. There are three parameters that determine the degree to which the background is blurred.

low: When it is set to true, the background is blurred slightly.

moderate: When it is set to true, the background is moderately blurred.

extreme: When it is set to true, the background is deeply blurred.

The image is blurred with a low effect.

change_bg.blur_bg("sample2.jpg", moderate = True, output_image_name="blur_img.jpg")

We want to moderately blur the background of the image, and we set moderate to true.

The image’s background is blurred with a moderate effect.

change_bg.blur_bg("sample2.jpg", extreme = True, output_image_name="blur_img.jpg")

We want to deeply blurred the background of the image, and we set extreme to true.

The image’s background is extremely blurred.

Full code to blur the background of an image

Code to Obtain output array of the blurred image

Note: Learn how to apply these background effects to videos and camera’s feeds on PixelLib’s github’s repository and PixelLib’s documentation. I will publish an explanatory article on how to apply these background effects to videos and camera’s feeds soon.

Reach to me via:

Email: olafenwaayoola@gmail.com

Linkedin: Ayoola Olafenwa

Twitter: @AyoolaOlafenwa

Facebook: Ayoola Olafenwa

Check out these articles written on how to make use of PixelLib for semantic and instance segmentation of objects in images and videos.