The goal of this assignment is to seamlessly blend one image to another target image with gradient-domain processing. Specifically, we focus on Poisson blending to achieve the task.
The most naive way to do the task without any seamlessness is simply copying and pasting pixels from one image to another. However, such an approach would lead to obvious seams on the image. We try to remove the seam such that the difference between the boundary id minimal. The approach could be described as below
Preparation: To do the blending, we need a mask image to specify which section in the source to blend in another image. The way to do that is to manually draw a polygon on the source image to use for the blending. The sample masking is similar to below
After preparing the mask image, we are given one source image, one target image, and one mask image to specify which region of the source image to put on the target image. At a high level, we will try to put a source image to the target image so that the boundary maintains the gradient of the source image. This way, we can ensure that the source image won't have different lighting and contrast than the target image.
As a result, we form a least-square problem of constructing the pixels in the target image so that these pixels satisfy the following properties:
For each pixel x,y, inside the reconstructed region, we
have a constraint that
v(x+1, y) - v(x,y) = s(x+1,y) - s(x, y)
v(x, y+1) - v(x,y) = s(x,y +1) - s(x, y)
For each pixel u, v at the boundary, we have a constraint
that
v(u, v) = s(u, v)
With these constraints, we can form a combination of equations of
where A is a sparse matrix with each column correspond to one
pixel in the region
v is the vectorized vector of the image region to
reconstructed
b is the right-hand side of the constraints
Therefore, we would want to minimize
with least-square optimization. Because A is a huge and sparse matrix, we can use scipy's sparse Matrix to construct A to optimize the performance and the memory footprint. As a result, the speed to run the toy problem is instant.
Before solving the original problem, we will solve a toy problem of reconstructing an image using the same gradient constraints to validate the approach.
For each pixel x,y, of the image, we have a constraint that
v(x+1, y) - v(x,y) = s(x+1,y) - s(x, y)
v(x, y+1) - v(x,y) = s(x,y +1) - s(x, y)
v(0, 0) = s(0, 0)
Mixed Gradients
A simple trick to handle the above case is simply choosing the gradient with more intensity from both the source and target image. Making the blend look much better for plain background source images.
Color2Gray
Gradient-domain data is also useful in converting colour images by making mixed gradient constraints on channels (such as channel Saturation and Value in HSV or even channel Saturation with the regular converted grey image to restore the intensity in saturation)