Page 1 of 1

Pasting object on background using mask

Posted: 2013-04-10T07:12:08-07:00
by AlbertW
Hello,

Suppose I have three images, an object with a background, a mask for the object on the first image (denoting the pixels on the first image where the object is) and a background image. I want to paste the object of the first image in the background. How can I do this with imagemagick? Also, any hints on how to make the pasted object better integrated with the background with image magick? Like edge softening, adjusting lighting, things like that?

Thanks in advance!

Re: Pasting object on background using mask

Posted: 2013-04-10T08:06:15-07:00
by pipe
http://www.imagemagick.org/Usage/masking/ contains a lot of information about this, have you seen that website? It has a lot of great information!

Re: Pasting object on background using mask

Posted: 2013-04-10T17:54:30-07:00
by anthony
You can use the mask to simply mask out the background from the first image and then overlap that onto the new background.

That is what 3-image composition does.
http://www.imagemagick.org/Usage/compose/#mask
http://www.imagemagick.org/Usage/maskin ... ed_compose

Unfortunately that does not actually remove the background completely along edges, unless the mask is either slightly smaller than necessary, or uses a strong fuzzy (feathering), which is actually a very similar thing.

Basically you want to completely remove the background from the first image. That is you not only want to recover how transparent the edge pixels are (which you provide with the mask) but also recover the true color of those pixels.



The problem. To get the true foreground color of a semi-transparent pixel, you need to know either the original true color (the solution) or the true background color of the pixel. That with the transparency from the mask will let us figure out the true foreground color of the edges.

For details of the technique in a closely related problem see...
Background Removal using Two Backgrounds
http://www.imagemagick.org/Usage/maskin ... background

Only in this case we use 'background + mask' instead of two backgrounds.

Re: Pasting object on background using mask

Posted: 2013-04-12T04:49:46-07:00
by AlbertW
Thanks for both replies! I'll look into what you suggested, anthony. It seems like a good way to deal with the border problem!

Re: Pasting object on background using mask

Posted: 2013-04-14T21:21:07-07:00
by anthony
Just remember by 'background + mask' I mean an image of the background without any foreground image on it.

The mask is then used to determine how much of the foreground image color was modified by the background color.

The examples I give a foreground image is provided on two different backgrounds, whcih are then combined to generate a mask. That mask is then combined with one of the original images to recover the foreground color, from the known background color.