Gradient Domain Fushion
In this part, I try to seamlessly blend an object or texture from a source image into a target image. The simplest method would be to just copy and paste the pixels from one image directly into the other. Unfortunately, this will create very noticeable seams, even if the backgrounds are well-matched. Here we use the insight, that people often care much more about the gradient of an image than the overall intensity. So we can set up an optimization problem as finding values for the target pixels that maximally preserve the gradient of the source region without changing any of the background pixels. Note that we are making a deliberate decision here to ignore the overall intensity! So something green might become red, but it still looks okay. We formulate the following optimization problem:
$v = arg \min_{v} \sum_{i \in S, j \in N_i \cap S} ((v_i-v_j) - (s_i - s_j))^2 + \sum_{i \in S, j \in N_i \cap \neg S} ((v_i-t_j) - (s_i - s_j))^2$
Here, each "i" is a pixel in the source region "S", and each "j" is a 4-neighbor of "i". Each summation guides the gradient values to match those of the source region. In the first summation, the gradient is over two variable pixels; in the second, one pixel is variable and one is in the fixed target region. This method is called "Poisson Blending".
Step 1. Given a source image and a target image, we manually select the boundaries of a region in the source image and specify a location in the target image where it should be blended. Then, transform (e.g., translate) the source image so that indices of pixels in the source and target regions correspond. Ideally, the background of the object in the source region and the surrounding area of the target region will be of similar color.
Step 2. Solve the above-mentioned optimization problem.
Step 3. Copy the solved values v_i into your target image. For RGB images, process each channel separately.
|
|
Jon Snow |
Geralt of Rivia |
|
Jon Snow in Iris's dream |
Here I try to blend Jon Snow into a scene in the video game, Witcher 3. The scenes' colors are not similar, but as we see above, the algorithm still does its best to blend the two images to a semi-believable state. However the result is still not perfect. For example, Jon Snow's face is turned to a weird color so that its gradients become similar to the background. In the following example, I demonstrate a better result, when blending images with similar backgrounds and tones.
|
A mothership blended into a medieval painting |