Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cut extra background in image?

Say I have an image that looks like this:

enter image description here

I fail to see what approach I could take in order to modify the picture so that all the background color surrounding the image is gone. So, a potential result would be this:

enter image description here

As you can see the white background has been cut out and now is about 2 pixels from the actual shoes.

I don't have just shoes, but I am looking for an algorithm that would let me do that. I am using Ruby and Minimagick, but I guess that first step would be to figure it out the algorithm that I could use.

EDIT: The background is not necessary white.

like image 918
Hommer Smith Avatar asked Nov 13 '13 17:11

Hommer Smith


1 Answers

If I understand you right, this sounds like a simple task that doesn't need any fancy algorithms.

  1. Find the background color of the image. One simple way to do that would be to just take the color of the pixel in, say, the top left corner. There are fancier ways you could use, but this will work for your example image.

  2. Find the leftmost and rightmost columns containing a pixel of some color other than the background. Those columns will be the leftmost and rightmost columns of your cropped image.

  3. Find the topmost and bottommost rows containing a pixel of some color other than the background. Those rows will be the topmost and bottommost rows of your cropped image.

  4. Crop the image to the dimensions found above. If you want, you can adjust the dimensions to leave a border of any size you want around the image.

like image 150
Ilmari Karonen Avatar answered Oct 18 '22 18:10

Ilmari Karonen