# MATLAB Scaled Image Normalized Cross-Correlation

A few week’s ago I got reacquainted with an old friend MATLAB, to solve a computer image manipulation problem.

When I blog about my travels I liked to Geocode my photos and reference them on a Google Map with a marker (see here), on the photo thumbnail I had previously stamped a circle marker on the lower right corner referencing the Google Map marker (the examples below will explain it better). I decided to move away from this approach and dynamically add these circle markers with a CSS and JavaScript solution, as this was a lot cleaner.

The problem, how do I remove the circle markers from my thumbnails, without having to re-crop every single one of my photos. It is fine for landscape photos, all image programs could batch resize the thumbnail from the full size photo, but what about portrait photos, each thumbnail was a cropping of the full size photo. I could assume either the top, vertical centre or bottom of the photo, but that would most likely be different to the original thumbnail and may miss the detail of the full size photo.

The solution was Normalized Cross Correlation, MATLAB already had an example that worked well if the cropped image had not been resized, but I needed something that would allow for resized thumbnails.

My crude solution, loops from smallest to largest sized cropped thumbnails and records the maximum peak of the normalized cross correlation, the maximum would then correspond to the correct scaled image size. This worked well, though the only issue was speed, I managed to optimise the speed by stepping the size in 6 pixel intervals, I also decrease the size of the full sized photo to shorten the number of iterations.

I have called the function cropxcorr, input Template takes in the thumbnail image, input A takes in the full size image, returns C a cropped image mirroring the thumbnail image.

There is also a MATLAB script I ran to loop through the files, portrait images will be passed with cropxcorr, landscape images simply resized.

cropxcorr.m

image-loop.m

Example 1
Thumbnail with marker

Original full sized image

Generated thumbnail with no marker

Example 2
Thumbnail with marker

Original full sized image

Generated thumbnail with no marker

Example 3
Thumbnail with marker

Original full sized image

Generated thumbnail with no marker