OpenCV has functionality built in for template matching. The template matching implementation is basically matching a subimage against a main image by sliding it across the entire image using one of the matching methods, in my implementations I used the normalized square difference matching method.

Below is a summarized excerpt for template matching within Quadrant 1.

const char* templateFilename = "F:\\Thesis_Research\\AutomatedDetector\\Test_Sets\\Quadrant_Images\\q1_faint.jpg";

// load template image 
tpl = cvLoadImage( templateFilename, CV_LOAD_IMAGE_COLOR );
// Check if the template file loaded
if( tpl == 0 )
     fprintf( stderr, "Cannot load template file %s!\n", templateFilename  );

//Search quadrant 1
SearchQuadrant(1, img->width/2, 0, img->width/2, img->height/2);

CvRect region;
region = cvRect(img->width/2, 0, img->width/2, img->height/2);

//Get the width and height for the result image
width  = region.width  - tpl->width  + 1;
height = region.height - tpl->height + 1;

//Create a new image for comparison result
res = cvCreateImage( cvSize( width, height ), IPL_DEPTH_32F, 1 );

//Check if the result image was actually created
if( res == 0 ) 
     fprintf( stderr, "Cannot create new image for comparison result!\n" );

//Perform template matching
cvMatchTemplate( m_quadrant1Image, tpl, res, CV_TM_SQDIFF_NORMED );


The resultant image obtained is a single-channel byte of floating point image. Next, iteration through the image array to get the lowest threshold value. For cvMatchTemplate, a perfect match will be 0 and a bad match will be large. But there must be a defined line between the non perfect matches and non matches. I set my threshold to 0.05, and I take the lowest point beneath that threshold.
In running template matcher on positive images, for example the below image the lowest threshold was about 0.00621


low_threshold = 0.00621

Below is the template matcher run on a negative image in order to determine a minimum threshold value of non cavity matches. But the negative image still returned a low value comparable to that of a positive detection of 0.00600 even though this was a negative image.

low_threshold = 0.00600

Tags: , ,

Leave a Reply