AForge.NET Framework
2.2.5 version is available!

Exhaustive template and block matching

AForge.NET framework provides exhaustive template and block matching routines. These are the simplest
matching routines since they perform complete matching scan (comparing) of template with each candidate point
in source image. It does not make them suitable in video processing tasks, but may be still used in some
tasks of still image processing.

Exhaustive Template Matching

The routine implements exhaustive template matching algorithm, which performs complete scan of source image,
comparing each pixel with corresponding pixel of template.

// create template matching algorithm's instance
// (set similarity threshold to 92.5%)
ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching( 0.925f );
// find all matchings with specified above similarity
TemplateMatch[] matchings = tm.ProcessImage( sourceImage, template );
// highlight found matchings
BitmapData data = sourceImage.LockBits(
    new Rectangle( 0, 0, sourceImage.Width, sourceImage.Height ),
    ImageLockMode.ReadWrite, sourceImage.PixelFormat );
foreach ( TemplateMatch m in matchings )
{
    Drawing.Rectangle( data, m.Rectangle, Color.White );
    // do something else with matching
}
sourceImage.UnlockBits( data );

Exhaustive Template Matching

Exhaustive Block Matching

The routine implements exhaustive search block matching algorithm, which searches for points’
displacement between two source images – takes a template from one source image at specified
position and searches for a match in another image within search window of specified size.

// collect reference points using corners detector (for example)
SusanCornersDetector scd = new SusanCornersDetector( 30, 18 );
Point[] points = scd.ProcessImage( sourceImage );
// create block matching algorithm's instance
ExhaustiveBlockMatching bm = new ExhaustiveBlockMatching( 8, 12 );
// process images searching for block matchings
BlockMatch[] matches = bm.ProcessImage( sourceImage, points, searchImage );
// draw displacement vectors
BitmapData data = sourceImage.LockBits(
    new Rectangle( 0, 0, sourceImage.Width, sourceImage.Height ),
    ImageLockMode.ReadWrite, sourceImage.PixelFormat );
foreach ( BlockMatch match in matches )
{
    // highlight the original point in source image
    Drawing.FillRectangle( data,
        new Rectangle( match.SourcePoint.X - 1, match.SourcePoint.Y - 1, 3, 3 ),
        Color.Yellow );
    // draw line to the point in search image
    Drawing.Line( data, match.SourcePoint, match.MatchPoint, Color.Red );
    // check similarity
    if ( match.Similarity > 0.98f )
    {
        // process block with high similarity somehow special
    }
}
sourceImage.UnlockBits( data );

Exhaustive Block Matching