Skip to content

Background removal of an image using OpenCV and Deep Learning.

Notifications You must be signed in to change notification settings

aadityavikram/Background-Removal

Repository files navigation

Background-Removal

Background removal of an image using OpenCV and Deep Learning.

Setup :-

Python -- > Python 3.6.5

OS --> Windows 10 (OS build-->17763.253) (Version-->1809)

GPU --> Nvidia Geforce GTX 1060 (6gb)

CPU --> Intel Core i7-8750 @ 2.20GHz

RAM --> 16gb

Background of images containing a person can be removed by running person.py

python person.py

specify path of file when it outputs "Enter path of file: "

runs on Keras 2.0.9

uses 100 layer Tiramisu neural network which was based on the DensNet.

the pre-trained model is not so accurate and works best when a single person is present in the picture.

and as it is 100-layer Tiramisu model, re-training it would take days on my system.

pre-trained models can be downloaded at:-

https://drive.google.com/open?id=1zk4j3bT1F6TZQiDsMKmEi7xB2Q6fSuHA

https://drive.google.com/open?id=1B-3dTJzlzBJ9Pz0q5QY7AP3T5zGbhuQZ

*both models gave different results depending on the image*

Background of images not containing a person can be removed by running non-person.py

python non-person.py

specify path of file when it outputs "Enter path of file: "

uses OpenCV

*3-input.jpg gave better result when deep learning was used with 2nd model than when 1st model or OpenCV were used*

Process for person.py :-

1) Loaded the pre-trained model.

2) Resized the image to 224x224 as the model was taking input in the same resolution as seen from model.json file.

3) Removed the transparency channel before getting predictions and resize the prediction to its original height and width.

4) Pixel values above the threshold factor were converted back to 255 and below threshold were converted back to 0 to exclude out of range pixels.

5) Added back the transparency channel and converted the array back to image.

6) Saved the image in png format.

Process for non-person.py :-

1) Grayed the image and applied canny edge detection, erosion and dilation.

2) Found the contours and filled all the contours.

3) Blurred the mask after smoothing it to make the contours smooth.

4) Converted the mask into 3-channel and blended it with foreground.

5) Added transparency channel to the image to make background transparent.

6) Saved the image in png format.

Contrast and brightness can be increased for better background removal using sunglasses_removal.py file

python sunglasses_removal.py

Put the dataset folder containing the training images in the same directory as this python file.

Final images will be saved in the same format as the dataset folder in a new trained directory

Links referenced :-

https://towardsdatascience.com/background-removal-with-deep-learning-c4f2104b3157

https://www.youtube.com/watch?v=8-3vl71TjDs

https://docs.opencv.org/3.4/db/d5c/tutorial_py_bg_subtraction.html

http://benjamintan.io/blog/2018/05/24/making-transparent-backgrounds-with-numpy-and-opencv-in-python/

https://docs.opencv.org/3.4/d3/dc1/tutorial_basic_linear_transform.html

About

Background removal of an image using OpenCV and Deep Learning.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages