Monday, January 10, 2011

Merging images in Gimp

The problem
You have two (or more) photographs of the same objects (with different lighting/focus/time lapses, etc.) and you would like to merge them but the photographs are a little off. The end result of such an operation would be akin to this (click to see the larger version):
Merged images (dA link)

The correct way, obviously, is to do it right right from the start: that is using a Tripod and capturing the object from the exact same position. However, in some cases it is not possible. For example, when the photograph taken is a few months apart (like the one shown) or when you are in a rocky terrain.

The above image was produced using Gimp (version 2.6.10). Here, I will show you how to use the Measure tool and the Rotate tool to align the images (almost) perfectly. Another tool which is immensely useful before using any of these tools is the Scaling tool (and obviously the Move tool). For the more technically inclined, because this is needed often for medical images or detailed cartography, there are automated ways of doing this. For more details, read the Wikipedia article on Image Registration.

This tutorial assumes that you know about layers and the basics about controlling opacity.

The images I would want to merge are the following:
Original images to be merges (click to enlarge)
Both images were taken from the window of my room at Bourdonette, Lausanne. Though the images are off in rotation and translation, thankfully they are not zoomed in/out. However, the images do have a certain panning due to perspective change, which is difficult to compensate for.

I knew as soon as I put them together that the perfect way to merge these two would be to have the deep colored night sky on the right and the white falling snow on the right and the translation would need to happen somewhere in the middle. This gave me a hint where the images should match the best.

Choosing the point of rotation
Place the layers over each other, alter the opacity of the top layer, and look for a good reference point which lies inside the transition area. The points I chose are these:
The anchor points to align the photographs
(click to enlarge)

  1. They are in the transition area
  2. They are more or less in the middle of both the images
  3. They have a feature (a window) which I can identify.
Measuring the rotation needed
One of the issues in version 2.6.10 of Gimp is that while using the rotate tool interactively, the layers become opaque (their opacity becomes 100%) which makes alignment near impossible.
Images become opaque while rotating in Gimp (click to enlarge)

The work around here is using the Measure tool and finding the angle and distance between two points which should match in the final image. It is best to choose the reference points in the transition area too for the best results:
Measurement of the first align point from the point of rotation
(click to enlarge)

Measurement of the first align point from the point of rotation
(click to enlarge)
After this, the layers can be rotated by manually entering the angles and fixing the anchor point at the correct place. Now that the photographs are aligned, we can look at how to blend the two layers together.

Blending the layers together
This can be done easily using a Layer Mask. This is a powerful tool which allows one to change transparency for different parts of a layer instead of forcing a uniform alpha value (which can be done by simply changing the opacity of the layer).

A Layer Mask can be added by Right clicking a layer and selecting Add layer mask in the context menu:

Adding a layer mask
(click to enlarge)

After adding the layer mask to the layer on top:
  1. Select the layer mask by clicking it next to the layer (the green spot shown in the image above)
  2. Select the Blend tool.
    1. See this to get a primer on how to use gradients.
  3. Select the linear gradient from the tool options, and draw a gradient on the mask of the layer.
    1. If the gradient covers the image itself, then you probably have not selected the mask correctly. Applying the gradient should make parts of the image transparent (the darker the gradient, the more transparent the image should be at that part).
Now, you are almost done. You should have a image which looks like this:
Almost done (click to enlarge)
Play with different gradients concentrations and see which gives the most satisfying result. After that, crop the ugly parts away, export to either jpeg or png, and voila!
Final image (click to enlarge)

Of course, the most important thing is to have fun while you are at it!


No comments: