MATLAB Image Processing: Using Normalized Cross-Correlation for Thumbnail Matching

MATLAB image processing with normalized cross-correlation helped me solve a challenging thumbnail matching problem. I needed a way to accurately match and recreate thumbnails without markers while preserving the original cropping decisions.

Why MATLAB Image Processing Was My Solution

A few weeks ago I reconnected with MATLAB to solve a specific image processing challenge. For my travel blog, I used to geocode photos and display them on Google Maps with markers. Each thumbnail had a circle marker stamped in the corner referencing its map location.

I decided to improve my approach by dynamically adding these markers with CSS and JavaScript instead of permanently embedding them in the images. This created a cleaner, more flexible solution.

The MATLAB Image Processing Challenge

The main problem was removing these circle markers from existing thumbnails without manually re-cropping hundreds of photos. For landscape photos, this would be simple – batch resize from the originals. But portrait photos presented a challenge since each thumbnail was uniquely cropped from the full-size image.

How could I automatically determine the exact cropping area from the original photo that matched each thumbnail?

Normalized Cross-Correlation for Image Matching

The solution came through MATLAB’s normalized cross-correlation functionality. While MATLAB offers examples for matching non-resized cropped images, I needed something that could handle thumbnails at different scales.

My approach loops through different scaled versions of the cropped thumbnails and records the maximum peak of the normalized cross-correlation. The highest peak indicates the correct scale and position match.

The MATLAB Image Processing Implementation

I created a function called cropxcorr that takes two inputs:

  • Template: The thumbnail image (with marker)
  • A: The full-size original image

It returns a newly cropped image that matches the thumbnail but without the marker. For speed optimization, I implemented size stepping in 6-pixel intervals and reduced the full-sized photo dimensions to minimize iterations.

% MATLAB function for scaled image normalized cross-correlation
function C = cropxcorr(Template, A)
    % Implementation details for matching thumbnails at different scales
    % Find the best matching region in the original image
    % Return the correctly cropped version without markers
end

I also created a script to process multiple images, handling portrait images with cropxcorr and simply resizing landscape images.

MATLAB Image Processing Results

Below are examples showing the effectiveness of the MATLAB normalized cross-correlation approach:

Example 1: Portrait Image Processing

Original Thumbnail with Marker
Example 1: Original thumbnail with a circular marker in the corner
Full-sized Original Image
Example 1: Full-sized original portrait photograph showing a person standing in front of a scenic background
Generated Thumbnail without Marker
Example 1: New thumbnail generated with MATLAB that matches the original cropping but has no marker

Example 2: Another Portrait Match

Original Thumbnail with Marker
Example 2: Original thumbnail with a circular marker in the corner
Full-sized Original Image
Example 2: Full-sized original portrait photograph showing a landscape or architectural scene
Generated Thumbnail without Marker
Example 2: New thumbnail generated with MATLAB that matches the original cropping but has no marker

Example 3: Complex Image Match

Original Thumbnail with Marker
Example 3: Original thumbnail with a circular marker in the corner
Full-sized Original Image
Example 3: Full-sized original portrait photograph showing a detailed scene
Generated Thumbnail without Marker
Example 3: New thumbnail generated with MATLAB that matches the original cropping but has no marker

MATLAB Code Resources

If you’re facing a similar image processing challenge, you can adapt my MATLAB solution. The code is available for download:

  • cropxcorr.m – The main function for scaled normalized cross-correlation
  • image-loop.m – Script to process multiple images in a batch

For more information on image processing techniques, check out MATLAB’s Image Processing Toolbox documentation or Wikipedia’s article on cross-correlation.


Posted

in

by