Background removal

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
Will
Posts: 4
Joined: 2013-08-11T20:52:19-07:00
Authentication code: 6789

Background removal

Post by Will »

Hello, I need to remove a black background while making the edge, which is black/red mixed, semi-red transparent. I don't have other images to work with for masks, etc.. Can this be done with command line ImageMagick?

The following is an example image:

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

Re: Background removal

Post by fmw42 »

Use combinations of the red and blue channels for the mask. The red channel is thicker than the blue channel. Get the blue channel. Then get the difference between the red and blue channels, which corresponds to the red outline region. This will be ramped as the red fades to black. Add the two results and put that into the alpha channel of your image.

Try this (unix syntax). if on windows remove all the \ and put ^ at the end of each line


convert Nine.png \( -clone 0 -channel b -separate +channel -auto-level \) \
\( -clone 0 -channel rb -separate +channel -auto-level +swap -compose minus -composite \) \
\( -clone 1 -clone 2 -compose plus -composite \) \
-delete 1,2 -alpha off -compose copy_opacity -composite result.png
Will
Posts: 4
Joined: 2013-08-11T20:52:19-07:00
Authentication code: 6789

Re: Background removal

Post by Will »

Thanks for the reply.

What you provided is about as close as I could get from the examples. Unfortunately, the edge has been removed a little, has some black still in it, and is not faded in a transparency way. There should be some way to remove all black, ie the solid background and the influence of it on the edges.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Background removal

Post by GreenKoopa »

If you know that the background is black, any result should get you back to the original by placing it on a black background. The math for transparency is simple, but it doesn't yield a single solution. For example, your source image composed over a black background will yield the same image since it has no transparency. Here I assume the opposite, that you want the maximum transparency. Anything along this continuum may be chosen, and none may be "correct" because this may be chosen separately for each pixel. Should a 50% red pixel be 100% red and 50% transparent, or 50% red and 0% transparent? Without a mask, or the image also composed over a white background, there is no "correct". I hope this helps.

Code: Select all

convert Nine.png ^
-colorspace HSV -channel B ^
( -clone 0 -evaluate Set 100% -colorspace sRGB ) ^
( -clone 0 -separate ) ^
+channel -delete 0 ^
-alpha off -compose Copy_Opacity -composite ^
PNG32:result.png

convert result.png -background black -flatten check.png

compare -metric RMSE Nine.png check.png null:
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Background removal

Post by snibgo »

If using GreenKoopa's solution in a Windows command file, the % needs to be doubled: "100%%".
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Background removal

Post by fmw42 »

Since the red fades to black some part of it will be nearly black in the fade and your eyes cannot distinguish it from black. The fade is done via the difference between red and blue, which covers the same area as the red to black fade. You can modify the command to force the fade to be faster in the mask, but then your outline will be thinner.

Try this with some anti-aliasing of the edge at the end


convert Nine.png \( -clone 0 -channel b -separate +channel -auto-level -write show: \) \
\( -clone 0 -channel rb -separate +channel -auto-level +swap -compose minus -composite -write show: \) \
\( -clone 1 -clone 2 -compose plus -composite -write show: -blur 0x1 -level 50x100% -write show: \) \
-delete 1,2 -alpha off -compose copy_opacity -composite -write show: result_1.png
Will
Posts: 4
Joined: 2013-08-11T20:52:19-07:00
Authentication code: 6789

Re: Background removal

Post by Will »

Veerry sweeet, thanks much -all- for your replies and examples. GreenKoopa - it is very close to the original minus the black. It's hard to tell switching between transparent and black background if the Red luminosity/brightness has changed - maybe my eyes aren't that good. Now, a little bash script to convert a multitude.

Thanks again, all the replies and examples help in the learning process.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Background removal

Post by GreenKoopa »

As my test showed, it is exactly the original minus black. While I preserve hue (this process will work for any color), of course brightness has increased. That is what removing black does. The question was how much black you wanted removed. I removed it all, which I thought was too much but this is the subjective part. It also depends on what you plan on doing with the image next. If you compose the result over black, your original brightness level is restored. If you compose it over white, brightness will be increased further. This is usually the goal of having transparency.
Will
Posts: 4
Joined: 2013-08-11T20:52:19-07:00
Authentication code: 6789

Re: Background removal

Post by Will »

Thanks GreenKoopa, just what I was looking for - "exactly the original minus black". Good to here you confirm. Ya, brightness, hue, or any other potential alterations, I was concerned with for the core extracted image.

Can't give you much, but will definitely give UPs to the ImageMagick and your support when possible. Now, I'm ghosted...
Post Reply