Result 1.
First, in the algorithm I create the sparse matrix A. To do so, I find all of the non-zero (masked) elements in the mask and stuff them into a vector. I then find those elements not in the mask and stuff them into another. Three new vectors are then created, initialized as zeros, with enough space for each pixel and its neighbor, labeled i_in_mask, j_in_mask, and s_in_mask. They will be used to compute the sparse matrix. i will hold the pixel in question, j the value of the neighbor (if its a neighbor) or the current pixel, and s -1 or the number of neighbors for the current pixels.
I then loop through all of the pixels in the mask, getting the appropriate array indices from the linear indices of the number of pixels. I edge case for when a pixel does or does not have neighbors and set the necessary values as described in the paragraph above.
After the for loop executes, I trim all of the excesses off of the vectors using the counter through out the for loop keeping track not only of the number of pixels but also their neighbors if they have them, and construct the sparse matrix.
For each color channel, I first take the target image and reshape it into a one dimensional vector. Again, for each pixel in the mask I loop through, edge casing for when there are or are not any neighbors. At each pixel in b I subtract the appropriate neighbor's source value and add the source's current pixel value.
Finally, I solve for v in our system of linear equations via the command A\b. I reshape v to make it 2d once again and stuff it back into our output matrix, which I then return.
![]() ![]() ![]() ![]() |
![]() ![]() ![]() ![]() |
![]() ![]() ![]() ![]() |
![]() ![]() ![]() ![]() |
![]() ![]() ![]() ![]() |
![]() ![]() ![]() ![]() |
![]() ![]() ![]() ![]() |
As we can see, the results are passable. However, the luminance values override the images, hence in the last result the face looks slightly ghost-like because the source image is low-key and the target is high-key. It's also not object aware, hence in the 6th result the rainbow overtakes the palm trees because the mask isn't defined well enough. With the octopus image we can also see there is a slight blue boarder that doesn't quite blend and this is because the mask is again not well defined. A proper tracing in photoshop would likely get rid of this.