How to cut off additional background in the image?

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

enter image description here

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:

enter image description here

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.

+6
source share
2 answers

If I understand correctly, this sounds like a simple task that does not need any fantastic algorithms.

  • Find the background color of the image. One easy way to do this is to just take the color of the pixel, for example, in the upper left corner. There are more attractive ways you could use, but this will work for your sample image.

  • Find the left and right columns containing a pixel of a color other than the background. These columns will be the left and rightmost columns of your cropped image.

  • Find the top and bottom lines containing a pixel of a color other than the background. These lines will be the top and bottom lines of your cropped image.

  • Crop the image to the size indicated above. If you want, you can adjust the size to leave a frame of any size around the image.

+3
source

Use the algorithm for reducing the minimum algorithm st from the standard image processing library

+1
source

Source: https://habr.com/ru/post/958017/


All Articles