Let's say I have an image that looks like this:

I donβt see what approach I could take to change the image so that the entire background color surrounding the image disappears. Thus, the potential outcome would be as follows:

As you can see, the white background has been cut out and now is about 2 pixels from the actual shoe.
I don't only have shoes, but I'm looking for an algorithm that would allow me to do this. I use Ruby and Minimagick, but I guess the first step would be to figure out which algorithm I could use.
EDIT: The background is not necessarily white.
source share