Alignment
We want to find the correct displacement offsets of the green and red channels with the blue channel. We compute the alignments for each pair of channel separately.
We first cut 10~20% of the margins so white & black frame and noisy edges area are removed from the channels.
Then, we search within a specified range (10~20 pixels) for the best possible displacement offset combinations. To decide which x, y rolling combination works better, we use a heuristic based on Sum of Squared Differences (SSD). The lower the ssd value, the better the alignment.
Most images can be done quit well, but Some images, like self_portrait, emir, melons, need larger offset range to find a better alignment.
I was cropping 10~20% of the pixels from the center of images to do alignment in a faster manner, but some images need to shift the location of this window a bit, since the area's color and brigtness was not good enough for ssd alignment to perform well.
Image Pyramid Processing
For small images, the naive alignment algorithm can search for best displacement vectors in 0.2s. However, with larger .tif images, searching can be very expensive, so we need an image pyramid algorithm to search from coarser (smaller, resized version) image to finer (larger size version) in order to reduce the search range at each level and improve performance.
We resize the image to the half, and recuisively align the new aligned version from coarser level at current level. As a result, we align current level image based on the best result from coarser alignment, so we don't need to research too far to get to same result.
Auto Cropping
Since alignments have rolled the image arround and original black and white outer frame, the output images does not look that good overall. I tried to pick the midder row/ col of the image to detect the boundery of these edges through finding the largest gradient change within 5~10% of the margin.
This produced good results on images. But a few images were over-cropped due to the function getting greatest gradient changes on other place and treating them as borders.
Contrast Adjustments
I changes the gamma of the explosure to improve contrast of some images. These images has been improved to be less blury, and some of them are more clear on some overexposure part of the image, such as the window area in workshop.tif, and house roof of melons.tif.