Page 1 of 1

Background removal

Posted: 2013-08-11T21:23:23-07:00
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

Re: Background removal

Posted: 2013-08-11T22:13:53-07:00
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

Re: Background removal

Posted: 2013-08-12T00:17:41-07:00
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.

Re: Background removal

Posted: 2013-08-12T01:43:22-07:00
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:

Re: Background removal

Posted: 2013-08-12T06:42:04-07:00
by snibgo
If using GreenKoopa's solution in a Windows command file, the % needs to be doubled: "100%%".

Re: Background removal

Posted: 2013-08-12T09:58:57-07:00
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

Re: Background removal

Posted: 2013-08-12T16:49:44-07:00
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.

Re: Background removal

Posted: 2013-08-12T17:43:23-07:00
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.

Re: Background removal

Posted: 2013-08-12T18:23:13-07:00
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...