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

Background removal from reference image

Post by volkan »

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: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Background removal from reference image

Post by snibgo »

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

Re: Background removal from reference image

Post by volkan »

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: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Background removal from reference image

Post by fmw42 »

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

Re: Background removal from reference image

Post by volkan »

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: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Background removal from reference image

Post by fmw42 »

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: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Background removal from reference image

Post by fmw42 »

I have made an edit, so be sure to review above
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Background removal from reference image

Post by fmw42 »

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

Re: Background removal from reference image

Post by volkan »

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: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Background removal from reference image

Post by fmw42 »

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

Re: Background removal from reference image

Post by volkan »

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: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Background removal from reference image

Post by fmw42 »

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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Background removal from reference image

Post by fmw42 »

Your problem with your two images is that the black background one is not fully black near the label, whereas your white background one is more solidly white. In your black one, you can see dark gray close to the label.
classic12
Posts: 3
Joined: 2018-12-11T07:51:59-07:00
Authentication code: 1152

Re: Background removal from reference image

Post by classic12 »

Hi guys,

I am trying to process all the .jpg files in a folder apply your process and place into a new folder.

I have this code that processes the files then puts the new version into folder newImages. ( works but the result is not as good as your version )

find . -type f -name "*.jpg" -print0 | while IFS= read -r -d $'\0' file; do convert -verbose "$file" - fuzz 20% transparent white "newImages/$file.png"; done

Your code that gives me the best result on one file is :

convert DCF887D2.jpg \
\( -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

So I have tried combining the 2 to loop through the folder and create the new versions in the newImages folder.

I have this working :

find . -type f -name "*.jpg" -print0 | while IFS= read -r -d $'\0' file; do
convert $file \
\( -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 \
newImages/$file.png; done


The only problem is if the filename contains a space it fails.

Any ideas ?

Cheers

Steve Warby
classic12
Posts: 3
Joined: 2018-12-11T07:51:59-07:00
Authentication code: 1152

Re: Background removal from reference image

Post by classic12 »

I found this as a work around

for oldname in *
do
newname=`echo $oldname | sed -e 's/ /_/g'`
mv "$oldname" "$newname"
done

This removes any spaces in the filename first.
Post Reply