Removing background from Uploaded Images

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
pknerd
Posts: 4
Joined: 2012-12-18T23:46:46-07:00
Authentication code: 6789

Removing background from Uploaded Images

Post by pknerd »

Hi

I want to develop an interface where user will take the snap of their independent clothe (jeans, t-shirt) and upload on server. On server end I want to remove background of the uploaded image so that only the real object remain on site(e,g: Shirt, Jeans etc).

What will be the easiest way to carry on this task? What kind of background will be easy to remove out from an object and can use IM commands efficiently. What Instructions I can give to my users so that they can follow it and take snap(e.g: having a white background) so that user can avoid uploading complex image like this.


Assume I ask users to take snap with only WhiteBackground only.. Is it possible to detect White in background and inform user incase of non-White Backgrond?

I will be using PHP to use ImageMagick Library.
Please do guide.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Removing background from Uploaded Images

Post by fmw42 »

Unless there is a unique and constant color for the background (without shadows or flash glare or wrinkles) that is not included in your object (or at least not around the edges), then removing the background can be very hard.

The best way is to pad the image with a few pixels of background color all around the image and then use a fuzzly floodfill technique starting in a corner to make the background transparent.

see
http://www.imagemagick.org/Usage/draw/#matte
http://www.imagemagick.org/Usage/crop/#border


Alternately, if you have a small object or objects in a large area of background that might not be perfectly uniform, you can average the image down to one row or one column, then scale it back up to full size and do a background subtraction or background divide.

see
http://www.imagemagick.org/Usage/masking/#bg_remove
pknerd
Posts: 4
Joined: 2012-12-18T23:46:46-07:00
Authentication code: 6789

Re: Removing background from Uploaded Images

Post by pknerd »

Thanks for the detailed answer. Issue is that the images will be uploaded by user. What all I can do to write a text to have pic with specific background.

I am checking this flodfill and matte stuff.Is it possible to make a solid color background of my own choice of every uploaded pic so that I can then use Bg removal commands easily?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Removing background from Uploaded Images

Post by fmw42 »

The images have to photographed against whatever background you put behind them. But if the background color is not uniform or the edges of the object are a similar color, then removing the background will not work effectively.
pknerd
Posts: 4
Joined: 2012-12-18T23:46:46-07:00
Authentication code: 6789

Re: Removing background from Uploaded Images

Post by pknerd »

By Uniform color you mean a wall with single color or an Cupboard?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Removing background from Uploaded Images

Post by fmw42 »

pknerd wrote:By Uniform color you mean a wall with single color or an Cupboard?

It should work if the wall is a single color and the brightness does not vary much over it and there are no shadows, no glare and it is evenly illuminated. Furthermore the border of the object must not contain a similar color so that there is a clean difference between the border of the object and the background. A cupboard may not work if its door is smaller than the object, since it will have cracks between doors and so the texture will vary. Note that any background with texture is not going to work well.
pknerd
Posts: 4
Joined: 2012-12-18T23:46:46-07:00
Authentication code: 6789

Re: Removing background from Uploaded Images

Post by pknerd »

Sounds not a good idea to implement at user end. My wife rather gave another better yet doable idea. She asked to show an interface where user can crop image in rectangular shape. I think I will go for it. It could be done in JS too.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Removing background from Uploaded Images

Post by anthony »

Another way is to take two images from a camera on a tripod. one with the clothes, the other without. That can be used to generate a reasonable mask of the clothes form the background.

But I doubt you will get users to do this properly as it is requiring specific conditions rather than a quick snap.

For unconditional background removal. Only us humans can do it well.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply