This assignment explores image compositing and in-painting using gradient domain processing.

Find two images, make a binary mask using any tool you like, and write matlab code to copy the pixels where the mask is 1 from one image into the other.

Here is two images:

We use `imshow(source)`

and `imfreehand()`

to make the binary mask.

The resulting mask is:

The code that inputs the data and allows user to select mask is data_input.m.

This part is simply to copy the pixels where the mask is 1 from one image into the other.

The result is:

The code for cloning is easy_clone.m.

In this part, we use alpha blending to integrate the two images. Various window sizes are considered.

The result is:

The code for alpha blending is alpha.m.

This toy problem is an implementation for gradient domain processing. We use the graients and the value of one point in the original image to recover the image.

The core part of the code is:

```
for color=1:nb
s = im(:,:,color);
e = 0;
for y=1:imh
for x=1:imw-1
e = e+1;
A(e, im2var(y,x+1))=1;
A(e, im2var(y,x))=-1;
b(e) = s(y,x+1)-s(y,x);
end
end
for y=1:imh-1
for x=1:imw
e = e+1;
A(e, im2var(y+1,x))=1;
A(e, im2var(y,x))=-1;
b(e) = s(y+1,x)-s(y,x);
end
end
e=e+1;
A(e, im2var(1,1))=1;
b(e)=s(1,1);
b = double(b(:));
v = A\b;
for y=1:imh
for x=1:imw
output(y,x,color) = v(im2var(y,x));
end
end
end
```

The result is:

The code for the toy problem is toy.m.

Finally, we use the gradient domain blending to insert a part of an image to the other one.

According to James Hays's slides, we use the the Laplacian to do this.

The core part of the code is:

```
for y=1:imh
for x=1:imw
if(bMask(y,x))
e = e+1;
sparse_count = sparse_count+1;
i(sparse_count) = e;
j(sparse_count) = im2var(y,x);
va(sparse_count) = 4;
sparse_count = sparse_count+1;
i(sparse_count) = e;
j(sparse_count) = im2var(y-1,x);
va(sparse_count) = -1;
sparse_count = sparse_count+1;
i(sparse_count) = e;
j(sparse_count) = im2var(y+1,x);
va(sparse_count) = -1;
sparse_count = sparse_count+1;
i(sparse_count) = e;
j(sparse_count) = im2var(y,x-1);
va(sparse_count) = -1;
sparse_count = sparse_count+1;
i(sparse_count) = e;
j(sparse_count) = im2var(y,x+1);
va(sparse_count) = -1;
b(e) = 4*s(y,x)-s(y-1,x)-s(y+1,x)-s(y,x-1)-s(y,x+1);
else
e = e+1;
sparse_count = sparse_count+1;
i(sparse_count) = e;
j(sparse_count) = im2var(y,x);
va(sparse_count) = 1;
b(e) = T(y,x);
end
end
end
```

The resulting mask is:

The code for the toy problem is gra_proc.m.

In this assignment, we tried several different ways to integrate a part of an image into another one.

Simple cloning is the easiest way to do that; however, the result would be sort of rough.

Alpha blending is a way to deal with the inserting boundaries, and the window size would determine the quality of the result. With a smaller window size, the rough boundaries are still rough; on the other hand, with a greater window size, the inserted image could be shadowed.

Gradient domain blending produced the most smooth result; however, the color of the inserted part were noticeably changed.

The code are provided in each section via a link to the corresponding part of the code.