Background removal from reference image

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
volkan
Posts: 5
Joined: 2017-07-06T03:55:26-07:00
Authentication code: 1151

Background removal from reference image

Post by volkan » 2017-07-13T00:46:46-07:00

Hello when i try to remove background my code is

Code: Select all

convert master.JPG back.JPG \( -clone 0 -clone 1 -compose difference -composite -threshold 0 \) \
-delete 1 -alpha off  -compose copy_opacity -composite -fuzz 10% -transparent white -trim \
output.png
Master image
Image

Background image

Image

out image transparent

Image

some of images close to white color so white areas maybe transparent some times, my code is good or any idea ?

snibgo
Posts: 9388
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Background removal from reference image

Post by snibgo » 2017-07-13T04:25:13-07:00

That seems a very complex command. Where did back.JPG come from? If you made it, why is it jpeg? Never use jpeg, unless you really have to.

Why not simply:

Code: Select all

convert yellowJacket.jpg -fuzz 10% -transparent White out.png
10% is a wide margin. I usually find 5% is enough for finding a flat colour in a jpg. For clarity, I try things like this:

Code: Select all

convert yellowJacket.jpg -fuzz 5% -fill Red -opaque White x.png
Then I can easily see if the correct pixels have changed.
snibgo's IM pages: im.snibgo.com

volkan
Posts: 5
Joined: 2017-07-06T03:55:26-07:00
Authentication code: 1151

Re: Background removal from reference image

Post by volkan » 2017-07-25T09:01:45-07:00

Hi

Thanks a lot , we actually use raw photo so my command looks like

this

Code: Select all

convert _MG_0001.CR2 ........
Some images has a white areas in the object fuzz method not working very well

User avatar
fmw42
Posts: 22091
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Background removal from reference image

Post by fmw42 » 2017-07-25T09:32:17-07:00

you can do a flood fill to only make the outside white areas transparent

Code: Select all

convert master.JPG -fuzz 5% -fill none -draw "matte 0,0 floodfill" result.png
Please always provide your IM version and platform, since syntax may vary.

volkan
Posts: 5
Joined: 2017-07-06T03:55:26-07:00
Authentication code: 1151

Re: Background removal from reference image

Post by volkan » 2017-07-25T09:56:21-07:00

Hi ,

Thanks for your help , My IM details

Code: Select all

Version: ImageMagick 7.0.6-3 Q16 x86_64 2017-07-24 http://www.imagemagick.org
Copyright: © 1999-2017 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules
Delegates (built-in): bzlib freetype jng jpeg ltdl lzma png tiff xml zlib


Error happen

Code: Select all

convert: non-conforming drawing primitive definition `matte' @ error/draw.c/DrawImage/3269.

User avatar
fmw42
Posts: 22091
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Background removal from reference image

Post by fmw42 » 2017-07-25T10:14:48-07:00

For IM 7, the syntax is slightly different.

Code: Select all

magick master.JPG -fuzz 5% -bordercolor white -border 1 -fill none -draw "alpha 0,0 floodfill" -shave 1x1 result.png

I have added a white border at the beginning so that the floodfill will work all around and between his legs. The shave off the border at the end.

But, note, that floodfill will skip processing the gap between his arms and his body and between his fingers. So you might have to floodfill those regions separately, if you want them transparent.

User avatar
fmw42
Posts: 22091
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Background removal from reference image

Post by fmw42 » 2017-07-25T10:18:42-07:00

I have made an edit, so be sure to review above

User avatar
fmw42
Posts: 22091
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Background removal from reference image

Post by fmw42 » 2017-07-25T10:43:10-07:00

Here is another way. You threshold the image to make white as black and all the rest as white to create a mask. Then remove small regions (less than 30 pixel area) using -connected-components. Then put the resulting processed mask into the alpha channel of the image.

See http://magick.imagemagick.org/script/co ... onents.php

Code: Select all

convert master.jpeg \
\( -clone 0 -negate -threshold 5% -type bilevel +write master_mask_before.gif \
-define connected-components:area-threshold=30 \
-define connected-components:mean-color=true \
-connected-components 4 +write master_mask_after.gif \) \
-alpha off -compose copy_opacity -composite \
master_transparent.png
I have save the mask before and after processing and the result.

mask before:
Image

mask after:
Image

result:
Image

volkan
Posts: 5
Joined: 2017-07-06T03:55:26-07:00
Authentication code: 1151

Re: Background removal from reference image

Post by volkan » 2017-07-26T06:14:29-07:00

Hi

very good method it's amazing when i search the algorithms i found this

https://youtu.be/6uPj8qeMVJk?t=1m3s

I think use very different light methods to help background removal

User avatar
fmw42
Posts: 22091
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Background removal from reference image

Post by fmw42 » 2017-07-26T09:21:35-07:00


volkan
Posts: 5
Joined: 2017-07-06T03:55:26-07:00
Authentication code: 1151

Re: Background removal from reference image

Post by volkan » 2017-08-08T16:16:29-07:00

Hello Again :))

Black Background image
Image

White Background image
Image

when i try to remove background result is :

if you look more carefully
Image

can you see ? :))
Please focus on the product. product is %80 Transparent :)

User avatar
fmw42
Posts: 22091
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Background removal from reference image

Post by fmw42 » 2017-08-08T16:45:09-07:00

Which of your two input images (black background or white background) were you using the make your result above?

What was the command you used?

I have moved this topic to the Users forum, since you are using Imagemagick commands.

Try this:

Code: Select all

convert w.jpg -fuzz 25% -transparent white w_t.png

Post Reply