Image Alignment and Color Compositing

This post is about aligning images with Python.

We will be aligning images from Sergei Mikhailovich Prokudin-Gorskii(1863-1944). He took photographs at three exposures using a glass plate filters in order to separate the photograph into three color channels(red, green, and blue). The photographs are online at the library of congress.

First Approach: Brute Force #

This gif is an example of what what were are attempting to do in photoshop.
Alignment

Pseudocode

def align(channel_to_align, image_channel):
    for i in range(-15, 15):
        for j in range(-15, 15):
            # Move the image and score it
     return aligned_image

Results #

Smaller Images #

This example works well on smaller images.

1024px x 398px
Original
00056v.jpg

Unaligned Image
REAL_ALGO-unaligned-1.jpg

Aligned Image
algo-Orig.jpg

Larger Images #

There is very little difference to the unaligned image when using the same algorithm. The reason being, as images become larger the less of an impact our search has on the image as whole. Instead of aligning it in a -15, 15 pixel area we need to scale this with the size of the image in order to get similar results. The issue we run into is that speed becomes a major factor when working with large images.
REAL_ALGO-aligned-3.jpg

When scaling down the image the strategy still works well.
REAL_ALGO-aligned-0.jpg

Improving the Algorithm #

Image Pyramid #

Image pyramid help us scale down the image so we can analyze an image at multiple scales. Image Pyramid wiki

In order to create one in python we need two things.

  1. Blurring
  2. Image Reduction

The blur I chose to use is a Gaussian blur.

Creating a gaussian kernel. #

Gaussian Function Wiki

Using the equation for a 1 dimensional gaussian function you can create a gaussian kernel with your desired length and sigma.

For this I used a length=5 and sigma=1
[ 0.05399097 0.24197072 0.39894228 0.24197072 0.05399097]

Next in order to reduce the image you can choose every row and column to reduce the image by half after the blur.

Gaussian Blur #

Blurred
Image-Pyr-Blur-1.jpg

Unblurred
Image-Pyr-Half-1.jpg

Using the image pyramid #

When using the image pyramid, I decide on the number of levels to generate based on this equation.

 levels = int(np.floor(np.log2(np.sqrt(number of rows + columns+ 1)/125)) + 1))

Then I go to the highest level of pyramid, then I use the brute force solution. This gives a relative position of where to search when going down the image pyramid which speeds up the image alignment.

Performance #

Larger images #

In order to speed up the performance.

  1. Don’t do gaussian blur which leaves aliasing.

Aliasing
From left to right
Level 1 to Level 3

aligned.pngREAL_ALGO-b-1.jpg

  1. Take a smaller subset of the image. I take a smaller rectangle of the image and do the scoring that image. REAL_ALGO-aligned-0.jpg REAL_ALGO-b-1.jpg

Algorithm #

Level 3
REAL_ALGO-aligned-0.jpg

Level 2
REAL_ALGO-aligned-1.jpg

Level 1
REAL_ALGO-aligned-2.jpg:

Level 0
REAL_ALGO-aligned-3.jpg

Level 3
REAL_ALGO-aligned-0.jpg

Level 2
REAL_ALGO-aligned-1.jpg

Level 1
REAL_ALGO-aligned-2.jpg

Level 0
REAL_ALGO-aligned-3.jpg

Results

Unaligned
REAL_ALGO-unaligned-3.jpg

Aligned
REAL_ALGO-aligned-3.jpg


Unaligned
REAL_ALGO-unaligned-1.jpg

Aligned
REAL_ALGO-aligned-1.jpg


Unaligned
REAL_ALGO-unaligned-1.jpg

Aligned

REAL_ALGO-aligned-1.jpg #

Unaligned
REAL_ALGO-unaligned-1.jpg

Aligned
REAL_ALGO-aligned-1.jpg


Unaligned
REAL_ALGO-unaligned-1.jpg

Aligned
REAL_ALGO-aligned-1.jpg

 
10
Kudos
 
10
Kudos

Now read this

Start Ups

I was talking to one of my friends about becoming wealthy. The conversation went like this I just wanna get money first so i can start saving i mean i have savings but more savings She wanted to go to space one day The problem is you’re... Continue →