Page 1 of 1

Remove background of products pictures for e-commerce

Posted: 2018-04-07T06:19:50-07:00
by ksom
Hi all!

The version of ImageMagick that I use is : ImageMagick 6.9.7-4 Q16 on Linux Ubuntu.

My goal is to remove background on clothes pictures.

The best result I had is when I use this command but depending on the complexity of background I had bad results:

Code: Select all

convert x.jpeg -fill none -fuzz 4% -draw 'matte 0,0 floodfill' -flop -draw 'matte 0,0 floodfill' -flip -draw 'matte 0,0 floodfill' -flop -draw 'matte 0,0 floodfill' -flip x.png
I tried to adjust fuzz value but if I decrease it's worst and if I increase this result on removing part of clothes.

Example:
before:
ImageImageImage

after:
ImageImageImage

So I had the following idea: multiplicate the reference points that I used.

When I implemented that I had a problem because the first matte removed the most parts of background and when the command did the second matte, the picked color is transparent so doesn't do more background remove.

After that I though if before I do the matte I collect all the colors of reference points I want to use. I can give it to convert command.

So I tried to do this but I got an error. For example :

Code: Select all

convert x.jpeg -fill none -fuzz 4% -draw 'matte #ffffffff floodfill' x.png
This tell me that syntax isn't good. How can I give the hexa or rgba to matte?

I want do this that way because when I tried:

Code: Select all

convert x.jpeg -fuzz 4% -transparent ‘#ffffffff’
I got really bad result comparing to matte syntax (that remove very much parts of clothes).

Ideally I want to be able to do something like:

Code: Select all

convert x.jpeg -fill none -fuzz 4% -draw 'matte 0,0 floodfill' -draw 'matte 0,100 floodfill' -draw 'matte 0,200 floodfill' x.png
but in a way that the matte doesn't apply right now to keep all the colors of references points and apply after all -draw matte instructions is done. (Like doing each -draw instruction separately and merge removed results to the original image)

If it is not possible I want to find a way to give directly the colors instead of 0,0 or 0,200 .

I really think if I can to this in this way I will get very well result. Because when I do it manually one by one I see all the parts being removed but when I do it all together It doesn't work for the reason explained before.

Thanks a lot!

Ksom

Re: Remove background of products pictures for e-commerce

Posted: 2018-04-07T06:37:29-07:00
by snibgo
It's hard to tell from these thumbnails. In #2, the socks seem to merge into the background. So I can't see how the socks can be automatically separated from the background.

Re: Remove background of products pictures for e-commerce

Posted: 2018-04-07T09:55:24-07:00
by fmw42
That is a very hard problem, especially with shadows and parts of the image nearly the same color as the background. If I had a good solution, I would be rich by now.

There are some interactive tools that could help. For example PS and GIMP has tools that allow you to trace near the boundary and they snap to the edges. Also see Clipping Magick at https://clippingmagic.com and OpenCV has grabcut for background removal.

Re: Remove background of products pictures for e-commerce

Posted: 2018-10-24T12:33:08-07:00
by NEGUZO
Hi all

I have a similar problem...

I need to remove all the colors of an image, except the ones I indicate.

Can anybody help me please ?

Thank you

Re: Remove background of products pictures for e-commerce

Posted: 2018-10-24T13:28:31-07:00
by fmw42
You should probably do that interactively in Photoshop (or GIMP), were you can successively click on different regions to remove various intensity shadows. ImageMagick can remove nearly constant color background with -fuzz XX% -draw ".... flood fill" that are not similar in color to your main object.

Re: Remove background of products pictures for e-commerce

Posted: 2018-10-27T14:49:44-07:00
by fmw42
Here is an idea that I have been exploring. Using canny edge detection and connected components and morphology to remove slowly changing background color that is different from the object. Here is an example using your first image. There are a lot of arguments that may need tuning for each the image. But if anyone else has some improvements or ideas how to make it more universal or intelligent, please contribute. I am by no means and expert on morphology other than some basics. I have left in tmp images so that one can see the steps more clearly.

Image

Code: Select all

line1 - read input
line2 - clone and perform canny edge detection 
line3 - thicken to ConvexHull
line4 - do 5 iterations of thinning to prune short line sections
line5 - do closing with octagon size 5 and add 1 pixel black border
line6 - flood fill the outside with transparency
line7-9 - do connected components to remove 100 or smaller areas and shave 1 pixel border
line10 - fill any remaining black with white
line11 - put processed clone into alpha channel of original
line12 - do fuzzy replace of remaining gray with transparency
line13- write output

Code: Select all

convert thumb_618706_piece_picture_small.jpeg \
\( -clone 0 -canny 0x1+3%+3% +write thumb_tmp1.png \
-morphology Thicken:-1 ConvexHull +write thumb_tmp2.png \
-morphology Thinning:5 LineEnds +write thumb_tmp3.png  \
-morphology close octagon:5 -bordercolor black -border 1 +write thumb_tmp4.png \
-fill none -draw "matte 0,0, floodfill" +write thumb_tmp5.png \
-define connected-components:area-threshold=200 \
-define connected-components:mean-color=true \
-connected-components 4 -shave 1x1 +write thumb_tmp6.png \
-channel rgba -fill white -opaque black +write thumb_tmp7.png \) \
-alpha off -compose copy_opacity -composite -write thumb_tmp8.png \
-fuzz 10% -fill none -opaque "#d2d1cf" \
thumb_result.png
Image

Image

Image

Image

Image

Image

Image

Image

Image

Re: Remove background of products pictures for e-commerce

Posted: 2018-10-27T18:18:00-07:00
by Dekaner
Perhaps using a green screen vs. a white background would help?

Re: Remove background of products pictures for e-commerce

Posted: 2018-10-27T19:42:59-07:00
by fmw42
Dekaner wrote: 2018-10-27T18:18:00-07:00 Perhaps using a green screen vs. a white background would help?
Obviously, but not always possible. Also what if the clothing is green?

Re: Remove background of products pictures for e-commerce

Posted: 2018-10-27T23:44:59-07:00
by fmw42
Here is another much simpler concept. I sample each edges of the image in 5 equally spaced locations and do a fuzzy -fill ... -opaque ...

Image

Code: Select all

inc=25
fuzzval=15
WxH=`convert thumb_618706_piece_picture_small.jpeg -format "%wx%h" info:`
ww=`echo "%WxH" | cut -dx -f1`
hh=`echo "%WxH" | cut -dx -f2`
xlast=$((ww-1))
ylast=$((hh-1))
convert thumb_618706_piece_picture_small.jpeg thumb_618706_piece_picture_small_result.png
for ((i=0; i<=100; i=i+25)); do
ii=$((i*xlast))
jj=$((j*ylast))
iicolor1=`convert thumb_618706_piece_picture_small.jpeg -format "%[pixel:u.p{$ii,0}]" info:`
iicolor2=`convert thumb_618706_piece_picture_small.jpeg -format "%[pixel:u.p{$ii,$xlast}]" info:`
jjcolor1=`convert thumb_618706_piece_picture_small.jpeg -format "%[pixel:u.p{$jj,0}]" info:`
jjcolor2=`convert thumb_618706_piece_picture_small.jpeg -format "%[pixel:u.p{$jj,$ylast}]" info:`
convert tmp.png -alpha on -channel rgba -fuzz $fuzzval% \
-fill none -opaque "$iicolor1" \
-fill none -opaque "$iicolor2" \
-fill none -opaque "$jjcolor1" \
-fill none -opaque "$jjcolor2" \
thumb_618706_piece_picture_small_result.png
done
Image

But this approach fails for your other two images, because the clothing contains white colors that are too close to those of the background.