The formal blending constraints can be represented by this equation:
Where i is a pixel in the source region "S", and j is a 4-neighbor of "i", "s" represents the pixel intensities
at the source region, and "t" the pixel intensities at the target region.
The algorithm works by computing the solution to the least squares system defined above.
Using the provided alignment starter code, we first obtain the mask for the source image.
The next step in code is to define the pixel mapping given in the toy problem to easily
convert between the flattened version of the image and the 2D version. Then, looping over
the pixels in the 2D image, we add the constraints to the least squares problem for
each 4-neighbor of every pixel where the mask is set to True. We then compute the
gradient of the source image, and set the appropriate value in the b vector. The
scipy.sparse lil_matrix is used for making the large sparse matrix computation far
more efficient. The vector containing the solved pixel values are then reshaped back to the
2D image dimensions (unflattened) and an element-wise product is computed between the
boolean values in the mask variable and the blended pixels, and added to the element-wise
product of the negated mask and the target background. The computation is performed
separately for each of the three color channels.
Here are some more results for Poisson blending, including the before image where the pixels are copied directly over
so that it is clear which is the source and target image. Here is one of a sea monster attacking the Amalfi coast. It
is amazing to me that the glimmer of the light on the monster's tail blends so well with the target image.
Toy problem Result
The preliminary task was to reconstruct a small image using solely the gradient plus
one pixel intensity of the original image. Here is the original toy problem image,
followed by the reconstruction, for completeness.
Favorite blending result
Below is the source image for my favorite blending result - a field of cows. From this field, I first cropped out
one of the cows.
More Results
Extra Credit
For the extra credit, I attempted the "Mixed Gradients" task, changing the least-squares
problem to incorporate the gradient from the source or target image with the larger magnitude.
For this task, I took a different approach - instead of blending photographs, I
wanted to see the result of blending paintings. I clipped a sketch of an old
car and blended it into the famous Edward Hopper painting "Gas". Here is the side-by-side
comparison of directly copying the pixels vs. the mixed gradient blending method.