Page 1 of 2

Background removal from reference image

Posted: 2017-07-13T00:46:46-07:00
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 ?

Re: Background removal from reference image

Posted: 2017-07-13T04:25:13-07:00
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.

Re: Background removal from reference image

Posted: 2017-07-25T09:01:45-07:00
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

Re: Background removal from reference image

Posted: 2017-07-25T09:32:17-07:00
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.

Re: Background removal from reference image

Posted: 2017-07-25T09:56:21-07:00
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.

Re: Background removal from reference image

Posted: 2017-07-25T10:14:48-07:00
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.

Re: Background removal from reference image

Posted: 2017-07-25T10:18:42-07:00
by fmw42
I have made an edit, so be sure to review above

Re: Background removal from reference image

Posted: 2017-07-25T10:43:10-07:00
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

Re: Background removal from reference image

Posted: 2017-07-26T06:14:29-07:00
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

Re: Background removal from reference image

Posted: 2017-07-26T09:21:35-07:00
by fmw42

Re: Background removal from reference image

Posted: 2017-08-08T16:16:29-07:00
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 :)

Re: Background removal from reference image

Posted: 2017-08-08T16:45:09-07:00
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

Re: Background removal from reference image

Posted: 2018-01-06T13:41:44-07:00
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.

Re: Background removal from reference image

Posted: 2018-12-11T08:09:45-07:00
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

Re: Background removal from reference image

Posted: 2018-12-11T08:29:28-07:00
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.