How to change the image background to white?

2.8k views Asked by At

I have set of images. Images have a simple background. I want to change that background to white using Marvin Framework and Java.

As I am new to Marvin, it is making me trouble to change the background. I also tried opencv for java but its giving unsatisfied link error.

Image Example:

enter image description here

1

There are 1 answers

3
Gabriel Archanjo On

To get a perfect result you'll need to find a way to remove shadows. But I think it is a good start point for you.

Algorithm:

  1. Convert the image to binary color model (pixels are true or false) given a gray scale threshold.
  2. Perform a morphological dilation for closing openings in the shoes boundary.
  3. Fill the background with color rgb(255,0,255)
  4. After filling the background with a new color in the binary image, set the same pixels to white in the original image.

output:

enter image description here

source code:

import static marvin.MarvinPluginCollection.*;

public class RemoveBackground {

    public RemoveBackground(){
        MarvinImage image = MarvinImageIO.loadImage("./res/shoes.jpg");
        MarvinImage bin = MarvinColorModelConverter.rgbToBinary(image, 116);
        morphologicalDilation(bin.clone(), bin, MarvinMath.getTrueMatrix(5, 5));
        MarvinImage mask = MarvinColorModelConverter.binaryToRgb(bin);
        boundaryFill(mask.clone(), mask, 5, 5, new Color(255,0,255));


        for(int y=0; y<mask.getHeight(); y++){
            for(int x=0; x<mask.getWidth(); x++){
                if(mask.getIntColor(x, y) == 0xFFFF00FF){
                    image.setIntColor(x, y, 255,255,255);
                }
            }
        }
        MarvinImageIO.saveImage(image, "./res/shoes_out.jpg");
    }

    public static void main(String[] args) {
        new RemoveBackground();
        System.exit(0);
    }
}