Remove background

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
Daedolon
Posts: 7
Joined: 2016-03-10T07:16:05-07:00
Authentication code: 1151

Remove background

Post by Daedolon »

Hi everybody,

I'm new to ImageMagick and I've read a few tutorial but it's a bit too complicated for to figure out which command to run to obtain what I want.
I'm using a 3D software to generate pictures and those pictures have a plain black background by default.
Currently, I'm using a duplicate picture of the original but with a green background and use GIMP to make a difference between the two image and use the result as a mask on the black picture to remove the background.

I'd like to make this process automatic with ImageMagick but so far, I didn't even managed to get a good image difference.

On GIMP, when I apply the image difference with one layer green and one layer black, I merge the result and obtain one layer with only 2 colors : green and black. Then I select this as a mask.

However I tried to use the difference command of ImageMagick but I only obtain some result in red or white or black.

Here are the pictures reference :

http://www.gaweb-studio.com/test_green.png
http://www.gaweb-studio.com/test_black.png

and the result I want :
http://www.gaweb-studio.com/result.png

Could you tell me which command I'd need to run to obtain a picture with a transparent background ? Or if you know of a better way to remove the black background, let me know. With my current solution, some detail which should be half transparent (like hair edges) are darkened, but I couldn't find a better solution so far.

Thanks
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Remove background

Post by snibgo »

This is very close to your desired result. (I think it is better.)

Code: Select all

convert test_black.png ( +clone test_green.png -compose Difference
 -composite -grayscale Brightness -negate ) -compose CopyOpacity -composite c.png
In general, it would be better to generate the original with a transparent background. Can't your 3D software do that?
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Remove background

Post by anthony »

But if your software can't generate transparency (alpha mask), all is not lost in getting a perfect transparency.

Generate the same image on 2 complementary colored backgrounds. For example black-white, green-purple, blue-yellow.
Black-white is the easiest as the mathematics simplifies, thus avoiding a complex FX expression.

From that you can then merge them to restore the alpha transparency.
See IM Examples, Masking, Background Removal using Two Backgrounds
http://www.imagemagick.org/Usage/maskin ... background

A technique developed in the discussion Undo a Composite -dissolve
viewtopic.php?t=18235

All other techniques (generally from one image on green screen) is not perfect and can not recover all edge aliasing pixels for all colors.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Daedolon
Posts: 7
Joined: 2016-03-10T07:16:05-07:00
Authentication code: 1151

Re: Remove background

Post by Daedolon »

The software allows me to select a background so I tried putting a simple transparent PNG background but the result with this is a black background unfortunately.

I tried both your solutions and the results were indeed what I wanted.
The only difference was with snibgo solution, the edges were a little blurrier with a lighter color on some edges too. So I went for anthony solution. I didn't see this tutorial but it is indeed what I was searching for. I'll be using black&white backgrounds from now on. I thought green was the way to go since they always use it in movie making.

Thanks a lot for your help and your time ! That'll save me a lot of time.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Remove background

Post by anthony »

The reason green screen is used in move making is they can only take one image for sequences involving live actors. The perfect solution requires 2 images with different backgrounds, which is impossible in real time.

Some 'space opera' movies (starwars some to mind) use two separate shots with different backgrounds for its ship models, so as to avoid color spill from green screen. Some movies actually they used three shots for each frame, with a extra shot in complete darkness to get the 'running lights' from the ship, for better effects.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
skinnytron
Posts: 2
Joined: 2016-03-28T15:57:36-07:00
Authentication code: 1151

Re: Remove background

Post by skinnytron »

I'm trying something really similar but I can't seem to get the desired result. I'm trying to remove a background from an image that contains only text. Luckily I can change the color of the background so I was able to generate two versions (one with a white background and one with a black background).

I tried a few options from: http://www.imagemagick.org/Usage/maskin ... background . Most notably:

Code: Select all

convert 10908-black.png 10908-white.png -alpha off \
         \( -clone 0,1 -compose difference -composite -negate \) \
         \( -clone 0,2 +swap -compose divide -composite \) \
         -delete 0,1 +swap -compose Copy_Opacity -composite \
         10908-alpha.png
which produced an image with a transparent background. However, the edges of the text that were transparent in the original image came out solid so it resulted in really hideous aliasing effect. Not sure if there are any options I can change here to remedy this.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Remove background

Post by snibgo »

Upload a sample to somewhere like dropbox.com and paste the URL here.

But if you are trying to break captcha, I won't help.
snibgo's IM pages: im.snibgo.com
skinnytron
Posts: 2
Joined: 2016-03-28T15:57:36-07:00
Authentication code: 1151

Re: Remove background

Post by skinnytron »

snibgo thanks for the response but I just found the problem (which turned out to be pebkac ...)

I was checking the quality of the newly created image in photoshop but for some reason it was opening the file in mode "Indexed Color". I guess this mode is very lossy because the text looked awful. Once I opened it in a web browser it looked perfect (which was generated with the command I previously posted)

Haha, no it's nothing greasy like trying to break captcha. I'm just trying to generate text to go on t-shirts with a certain font, style color ect but variable text.
Bowen73
Posts: 2
Joined: 2016-07-24T14:24:33-07:00
Authentication code: 1151

Re: Remove background

Post by Bowen73 »

Hi, Im new to imagemagick too. my issue is similar to the original post. I have a subject on a green back ground (wearing an item of green clothing) and an original background thats just green. im trying to use the convert option that takes 2 images and compares and removes whats the same (i.e. the background) and leaves everything else i.e the subjeck in front of the screen including the green clothing

I found this about the masking but it doesnt do anything apart from a few transparent specs at the edges

Code: Select all

 convert overlay_figure.gif   overlay_bgnd.gif  \  -compose ChangeMask  -composite  overlay_removed.png
im trying this using a windows .bat file. any help to get this to work would be amazing :-)

thanks
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Remove background

Post by snibgo »

What is the backslash doing?

A link to your images might help. Please also say what version of IM you are using.
snibgo's IM pages: im.snibgo.com
Bowen73
Posts: 2
Joined: 2016-07-24T14:24:33-07:00
Authentication code: 1151

Re: Remove background

Post by Bowen73 »

im not sure what the slash is for. that was taken from the example on the webiste http://www.imagemagick.org/Usage/masking/

the version im using is: ImageMagick-7.0.2-Q16

what im after acheiving is similar to the original post. a subject in front of green :http://www.gaweb-studio.com/test_green.png
an image with just the green on its own. and the resulting image would be http://www.gaweb-studio.com/result.png with just the subject and a transparent background
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Remove background

Post by snibgo »

The usage pages have examples for bash. When a command is continued on the next line, the last character on the line is a back-slash.

For Windows, the equivalent character is the caret ^.

But you haven't broken the command into lines, and are not using bash, so IM will see the backslash and won't know what to do with it.

See Converting Scripts: UNIX Shell to Window DOS

After removing the backslash, your command works fine.
snibgo's IM pages: im.snibgo.com
snoop911
Posts: 1
Joined: 2016-08-11T13:29:27-07:00
Authentication code: 1151

Re: Remove background

Post by snoop911 »

Total noobie here, and I'm not just saying that, I'm not very tech savy in general, but I can follow directions!

Any pointers to a tutorial on working with IM to remove the background? I found one from 2007 but I imagine things might be done differently these days..
http://tech.natemurray.com/2007/12/conv ... arent.html

I've captured thousands of still images in front of a green screen... and my goal is to ultimately have all these images in gray scale only, with the background all in black (formerly green), maybe even crop the image so that it fits just around the object.

I imagine I'd first have to replace all green with black, then convert to grayscale, but not sure how to get started or the steps to look?

I've seen some tutorials using other software that requires outlining the object (moving ants), however that's what I'm trying to avoid, since there's too many images to process by hand!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove background

Post by fmw42 »

snoop911: It would be best if you provided a sample of your images. You can post to some place such as dropbox.com and put the URL here. If you have shadows on the green screen from your main object, then that makes it even harder to clean up, so long as the green screen is a constant color green and has no other texture.

Also always provide your IM version and platform, since syntax may differ.

Also in the future please start a new topic rather than posting onto an old one.

Please read the top-most post in this forum at viewtopic.php?f=1&t=9620
Post Reply