Remove background from image
original image from PAKUTASO
- Create mask image using Image Segmentation technique.
- Create trimap image using Morphology(
cv2.erode()
,cv2.dilate()
). - Apply Image Matting using FBA_Matting
Supported foreground object type depends on DEEPLABV3-RESNET101 output.
This model trained on PASCAL VOC 2012 semantic segmentation benchmark.
Class index of the dataset is shown as below.
[Class List]
1. aeroplane
2. bicycle
3. bird
4. boat
5. bottle
6. bus
7. car
8. cat
9. chair
10. cow
11. diningtable
12. dog
13. horse
14. motorbike
15. person
16. potted plant
17. sheep
18. sofa
19. train
20. tv/monitor
.png
.jpg
$ git clone https://github.com/takecx/remove_bg.git
$ cd remove_bg
$ git submodule update -i
Download FBA_Matting
model from here and place it to ./FBA_Matting/FBA.pth
You should create python environment using requirements.txt
.
$ conda create -n remove_bg python=3.7
$ conda activate remove_bg
$ pip install -r requirements.txt
Run BackgroundRemover.py
with some arguments.
$ python BackgroundRemover.py (image_file) --kernel_size (morphology kernel size) --iteration (morphology iteration num)
- input image file path (required)
--kernel_size
: kernel size of morphology conversion (optional)--iteration
: iteration num of morphology conversion (optional)
For example,
- input image file path :
./examples/images/sample.png
--kernel_size
: 5--iteration
: 3
then, you run
$ python BackgroundRemover.py ./examples/images/sample.png --kernel_size 5 --iteration 3