Intelligent Background Removal

Right, here’s how I’m going to get your ugly mugs into the game.

I will extend the functionality of the existing video capture/edit code, allowing the player to film him/herself against a completely static background, and then I will run some really clever code which removes the background, leaving only the moving player pixels in the final sequence.

 

Identifying Genuine Background Pixels

I will run all of the images through a sequence which extracts the entire sequence into a HashMap<Point, Vector<Integer>>. This seems to make more sense, to divide things into “per pixel” rather than “per image”, since we’ll be dealing with each individual pixel as a collective of that coordinate’s value throughout the sequence.

So then we histogram each pixel’s vector.

 

Removing Background Pixels

Now here’s the clever bit: Once we’ve picked out pixels that have at some point been a background pixel (based on a suitable spike in its histogram), we record the colour value of the spike in a HashMap<Point, Integer> and then we use this value (+/- 10%) to figure out which pixels need removing.

With our background pixel colour HashMap in place, we iterate through every pixel of every image in the sequence (will probably have to limit this to n number of frames, to keep memory usage respectable – for a 64 image sequence at 352 x 288, that’s 6488064 pixels!), apply a zeroAlpha colour to any pixel that matches the “known background”.

For every image:

for (int y = 0; y < bitmap.getHeight(); y++){
    for (int x = 0; x < bitmap.getWidth(); x++){
        int pixelInt = bitmap.getPixel(x, y);
        if (closeEnoughMatch(pixelInt, backgroundPixelsHashMap.get(new Point(x, y))){
            bitmap.setPixel(x, y, zeroAlpha);
        }
    }
}

 

Saving As PNG

Outputstream stream = new FileOutputStream(“/sdcard/filename.png”);
bitmap.setHasAlpha(true);
bitmap.compress(CompressFormat.PNG, 100, stream);
stream close();

And that just about covers it really. I will let you know as and when I get round to implementing this. It’ll be a huge fucking boost if I can.

 

TRP

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s