Pasting object on background using mask

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
AlbertW
Posts: 2
Joined: 2013-04-10T07:04:11-07:00
Authentication code: 6789

Pasting object on background using mask

Post 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!
pipe
Posts: 28
Joined: 2013-04-09T08:32:37-07:00
Authentication code: 6789

Re: Pasting object on background using mask

Post 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!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Pasting object on background using mask

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
AlbertW
Posts: 2
Joined: 2013-04-10T07:04:11-07:00
Authentication code: 6789

Re: Pasting object on background using mask

Post 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!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Pasting object on background using mask

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply