Remove blue irregular background using chroma key

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
nandelbosc
Posts: 4
Joined: 2016-01-26T16:53:58-07:00
Authentication code: 1151

Remove blue irregular background using chroma key

Post by nandelbosc »

Hi, i'm trying to remove the blue background of this image... https://drive.google.com/file/d/0B9hWe ... p=sharing
I tried different options...

Code: Select all

convert foto.jpg -modulate 100,150 foto2.jpg
convert foto2.jpg foto.png
convert foto.png -channel rgba -alpha set -fuzz 10% -fill none -opaque "#054db3" final.png
convert final.png -channel rgba -alpha set -fuzz 10% -fill none -opaque "#02317f" final1.png
convert final1.png -channel rgba -alpha set -fuzz 5% -fill none -opaque "#03308d" final.png

Code: Select all

convert foto2.jpg foto.png
convert foto.png -fuzz 10% -transparent "#054db3" prova.png
convert prova.png -fuzz 10% -transparent "#132c65" prova.png
convert prova.png -fuzz 10% -transparent "#03308d" prova.png
convert prova.png -fuzz 10% -transparent "#03308d" prova.png

Code: Select all

convert foto2.jpg foto.png
convert foto.png -channel rgba -alpha set -fuzz 15% -fill none -opaque "#054db3" final.png
convert final.png -channel rgba -alpha set -fuzz 8% -fill none -opaque "#132c65" final1.png
convert final1.png -channel rgba -alpha set -fuzz 10% -fill none -opaque "#03308d" final.png
convert final.png -channel rgba -alpha set -fuzz 10% -fill none -opaque "#1c3e7d" final1.png
convert final1.png -channel rgba -alpha set -fuzz 5% -fill none -opaque "#49618d" final.png
With acceptable, but no perfect results.

I think I can increase the success using choma_key... http://www.imagemagick.org/Usage/photos/#chroma_key but i can't make it work correctly.

Someone can help me?

Thank's in advance.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove blue irregular background using chroma key

Post by fmw42 »

Please always provide your IM version and platform. Syntax differs.

On unix try:

Code: Select all

convert foto.jpg \
\( -clone 0 -channel r -separate +channel -threshold 16% \
-morphology close octagon:3 -blur 0x3 -level 50x100% \) \
-alpha off -compose copy_opacity -composite foto.png
vary the different arguments as desired
nandelbosc
Posts: 4
Joined: 2016-01-26T16:53:58-07:00
Authentication code: 1151

Re: Remove blue irregular background using chroma key

Post by nandelbosc »

Please always provide your IM version and platform. Syntax differs.
True. Sorry... Version: ImageMagick 6.7.7-10 2014-03-06 on Ubuntu 14.04 64bits

Thank you very much fmw42! With your options works pretty well. But not perfect...

Changing threshold to 2% https://drive.google.com/file/d/0B9hWen ... sp=sharing

Changing threshold to 3% https://drive.google.com/file/d/0B9hWen ... sp=sharing

any suggention?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove blue irregular background using chroma key

Post by fmw42 »

The fact that your background is not a constant color makes it very hard to perfectly remove the background.

try this (make mask on both the hue and red channels and multiply and use that for the alpha channel)

Code: Select all

convert foto.jpg \
\( -clone 0 -colorspace HSL -channel R -separate +channel \
-fuzz 2% -fill black -opaque "gray(156)" \
-fuzz 0% -fill white +opaque black \
-morphology close octagon:3 -blur 0x3 -level 50x100% \) \
\( -clone 0 -channel r -separate +channel -threshold 16% \
-morphology close octagon:3 -blur 0x3 -level 50x100% \) \
\( -clone 1 -clone 2 -compose multiply -composite \) \
-delete 1,2 -alpha off -compose over -compose copy_opacity -composite result.png
nandelbosc
Posts: 4
Joined: 2016-01-26T16:53:58-07:00
Authentication code: 1151

Re: Remove blue irregular background using chroma key

Post by nandelbosc »

Thank's again fmw42, but your script its so much 'agressive'... https://drive.google.com/file/d/0B9hWen ... sp=sharing

I try to make a background more consistent, with a constant color, and with your first suggestions I think will work.

I really appreciate your collaboration. Thank you.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove blue irregular background using chroma key

Post by fmw42 »

Sorry, I had not noticed the holes. Either the HSL or the Red fuzz values are too large or I need a larger morphology. But better if you have a more constant background. You might also look at OpenCV, grabcut. That may work better for less even backgrounds.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Remove blue irregular background using chroma key

Post by snibgo »

Making the perfect mask for this image is difficult because the image is horrible. It is small, with many artifacts from heavy JPEG compression. Garbage in, garbage out.

For chroma-key, the background has an almost constant hue and high saturation, so selection on ranges of hue and saturation normally work well. But this image has patches of low saturation in the background, caused by JPEG compression.
snibgo's IM pages: im.snibgo.com
nandelbosc
Posts: 4
Joined: 2016-01-26T16:53:58-07:00
Authentication code: 1151

Re: Remove blue irregular background using chroma key

Post by nandelbosc »

Thank's for all the replies.

I'm trying to have a better image (more resolution and PNG instead JPG).

I comment the results here, soon.

Thank's again.
Post Reply