Project 2: Image Blending
Vibhu Ramani
September 28th, 2012
Steps:
- We are given 3 images, a source, a mask and a target. The mask is a binary image that defines which pixels of the source are transferred to the target.
- Since a simple cut and paste of the masked region gives a bad blend since the gradients of the source dont match the gradients of the target.
- Based on the gradients in the source image, but using the initial values from the target image we compute the value of the pixels under the mask.
- Each pixel under the mask would have to compute a value based on its neighbours. We need to solve a linear set of equations to compute these values.
- If 'v' is our final image, every pixel on it should satisfy:
v(i,j) - v(i-1, j) = s(i,j) - s(i-1, j)
v(i,j) - v(i+1, j) = s(i,j) - s(i+1, j)
v(i,j) - v(i, j-1) = s(i,j) - s(i, j-1)
v(i,j) - v(i, j+1) = s(i,j) - s(i, j+1)
Combining these we get:
4*v(i,j) - v(i-1, j) - v(i+1, j) - v(i, j-1) - v(i, j+1) = 4*s(i,j) - s(i-1, j) - s(i+1, j) - s(i, j-1) - s(i, j+1)
- All the values in the right are known and on the left side only if the pixel is in the masked region would its value be unknown else its known.
- Solving these equations gives us the value of the pixel in the final image.
- Since we need to solve equation for every pixel under the mask we use the format AX=B where A is A (NxN) coefficient matrix. B is a (Nx1) value matrix and X is a (Nx1) matrix with the variables to be determined.
- Using a sparse matrix in matlab we create A since A is huge dimensions wise. We use a map to keep track of the image coordinate and the equivalent variable
- 'A' is same across all color channels hence we create is only once, but calculate B for each of the color channel.
- For the border pixels not all neighbours are there hence only the equations for the valid nerighbours would be there and so the combined equation is different.
Results
Extra Credit
Mixing Gradients: Instead of picking up source gradient always if we also look at the target gradient for that pixel and use that to solve the equation, it takes into consideration if the target image has an object behind the mask and factors that.
This necessarily doesn't help always, cause if the source has a contant texture the targets background is picked up even though the texture on the source is opaque.